Emacs and Unix Tips

Xah Lee, 2006-09, 2009-06

This page provides some tips about using Emacs with common Unix shell tools. Most of the tips here also applies to the command line shell in Microsoft Windows. See bottom of page for detail.

Unix Shell

How to run a unix shell command?

To run single shell command, press “Alt+!” (or Alt+x shell-command). This is under the menu “Tools‣Shell Command...”.

To remove the resulting split panes, press “Ctrl+x 1” (or Alt+x delete-other-windows).

It is very useful to run shell commands while in emacs. For example, suppose you are writing a document or programing, and you need a list of a directory's content. Normally, you would switch to a terminal app, do “ls”, copy the result (probably with the mouse), then switch back to emacs, then paste. But with emacs's shell facilities, you can do all these within emacs.

How to apply a unix command to the current selection?

Select a region, then press “Alt+|” (or “Alt+x shell-command-on-region”). For example, select a region, then press “Alt+| wc -w Enter”. This will return the number of words in your selection.

You can have the result replace the selected region. To do that, press “Ctrl+u” before the “Alt+|”.

How to run a shell within emacs?

Start the shell by “Alt+x shell”. To run the previous command, press “Ctrl+”. To start a second shell, type “Ctrl+u Alt+x shell”.

If you use shell often, you can create a alias or a keyboard shortcut, by placing the following in your emacs init file:

(defalias 'sh 'shell)
(global-set-key (kbd "<f5>") 'shell)

Can i run ssh inside emacs?

You can run ssh inside emacs with emacs's terminal emulator. To start it, press “Alt+x term”. Then, you can run for example telnet, ssh, top, vi. You can even start another emacs. To exit term, press “Ctrl+d”.

In practice, it's better to run these processes outside of emacs in a dedicated terminal-emulator software. Because, once inside a terminal emulator inside emacs, normal emacs keystroke convention won't work because you told emacs to pretend it is a terminal emulator. This makes the user interface complicated.

Note: beside “shell” and “term”, there are also “eshell”. The “eshell” is similar in purpose to “shell”, but written entirely in elisp, so that, if you are on MS Windows system, you can still run unix-like commands with emacs.

In shell-command (M-!), pressing tab doesn't do command completion?

Get this lisp code by Tsuchiya Masatoshi at http://namazu.org/~tsuchiya/elisp/shell-command.el

Viewing “man” Pages and “info”

How to read man page in emacs?

Alt+x man”. If you want it in color, use “Alt+x woman”.

The “man” relies the unix “man” utility, while “woman” is completely written in elisp.

How to read GNU “info” manuals inside emacs?

Press “Ctrl+h i” (Alt+x info). Use mouse or arrow keys to move cursor to a link. Press enter or click to enter that node. Press u for going to the parent node. n for next node, p for previous node, and press l (lower case L) for the last node you visited (like the Back button in browsers). Press TAB to move cursor to the next link. Press q to quit info.

Directory Navigation and Managing Files

See: File Management with Emacs.

FTP and Remote File Editing

You can also transfer files between different machines connected by a network. The way to do this in emacs is consistent with the way it does directory navigation.

Type “Ctrl+x d” and type a directory you want to work with. In this directory, suppose myfile.html is the one you want to upload across the network to the machine on “example.com” in the directory “/public_html”. Suppose your login name for that machine is “mary”.

Here's what you do. Type “Ctrl+x d Enter” then move your cursor to the file you want to copy. Type “C”, then give the dir “/ftp:mary@example.com:/public_html”. You'll be asked for a password, then it'll be copied over.

Once you logged in to the ftp server, you can actually edit and save files on the remote server, bypassing all the upload/download pain.

(info "(emacs)Remote Files")

“find xargs grep” with Emacs!

Although it is very convenient to call unix shell command or run a shell inside emacs, but often you want to use Emacs's elisp version instead, because emacs versions highlight and link the result output, allowing you to see better or jump to files directly.

Some of these commands are written entirely in elisp, some work as a wrapper to the unix command tool. Here are some examples.

How to grep a file?

To call the unix grep command, do “ Alt+! grep xyz filename”.

Or, in Emacs, you can use “Alt+x list-matching-lines” or delete-matching-lines. “list-matching-lines” starts from the beginning of file (regardless if there's a region). “delete-matching-lines” works on the region if there is one, else it starts at the cursor point. There's also “delete-non-matching-lines”.

How to grep all files in a dir?

To call the unix command: “Alt+! grep -r mySearchStr dir_name”.

To call emacs version, type: “Alt+x grep”.

The difference between calling the unix grep inside emacs and calling emacs's grep, is this: the result of Emac's grep will be nicely colored and linked. Your search string are highlighted, and you can jump to the file by clicking on the file name or press return when the cursor is on the file name.

How to list files that contain a particular string? (grep -l)

Alt+x grep-find”.

The grep-find command will invoke find . -type f -exec grep -nH -e MySearchStr {} /dev/null \; or grep -r <C> -nH -e . --include=, depending on which distro or version of emacs. Emacs will promp the above command in the minibuffer, then you can edit it to modify your search word, or type name extension to search. When you execute the command, emacs will place the output in a formated and linked way in another pane.

If you use the command often, you can creat a alias or keyboard shortcut. Here's the code to put in your emacs init file:

(defalias 'gf 'grep-find)
(global-set-key (kbd "<f8>") 'grep-find) ; the F8 key

How to do find replace all files by a regex?

Normally, you would hack up a shell script, or use a combination of find xargs sed awk perl. Or, you can do it with Emacs. The advantage with Emacs is that you can do it interactively. For example, if you want the replacement happen on a case-by-case basis.

Basically, you want to use find-dired to mark the target files. Then, use dired-do-query-replace-regexp to do the actual find and replace. Then, use ibuffer to save and close files in batch. For detail, See Interactively Find and Replace String Patterns on Multiple Files .

“find” with Emacs's “dired”!

How to have the unix “find” result shown in dired mode?

Alt+x find-dired”.

Explanation: In unix, there's a very useful command that list files in a directory with a particular property. For example, if you want to list all files ending in “.html” in a directory and all subdirectories under it, you can do find /Users/jane/ -name "*.html" -print.

However, the result is a textual output. Let's say you want to run a word count “wc” command on all such files. You can use “find”'s option “-exec”, or in combination with “xargs” command. For example: find . -name "*.html" -print | xargs -l -i wc {}.

However, sometimes you want to do several complicated things with this set of files, and you want to do them interactively. For example, some of such files you want to word count, some of them you want to run another command on, and some of them you need to rename, and what to do depends on the previous commands. In this case, it will be useful, to have this list of files shown in emacs's dired mode, then you can use all emacs dired power to manipulate these files.

Misc

While running emacs in a text terminal, how to invoke the text-based menu?

Type “Alt+`” (tmm-menubar).

How to work emacs with Windows's command shell cmd.exe?

If you are on Windows, i highly recommended using EmacsW32 distro. With EmacsW32, it provides you with commands cmd-shell, cygwin-shell, msys-shell. The cmd-shell runs cmd.exe, the cygwin-shell runs cygwin shell, and msys-shell runs MinGW shell. (both Cygwin and MinGW are ports of unix shell tools to Windows). You can set which of these runs when you call “shell”. For some tips, see: Emacs and Microsoft Windows Tips.


Related essays:

2006-07
© 2006 by Xah Lee.