Xah Lee, 2006-09, 2009-06, …, 2010-09, 2011-03-14
This page is a tutoriar on using Emacs with the operating system's command line shell. On linuxes, this is usually bash. On Windows, this is cmd.exe by default, or can be PowerShell. (See: PowerShell Tutorial and PowerShell Modes.)
How to call a shell command?
To run single shell command, call “shell-command” 【Alt+!】. This is under the menu 〖Tools▸Shell Command…〗.
To remove the resulting split panes, call “delete-other-windows” 【Ctrl+x 1】.
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 shell command to the current selection?
Select a region, then call “shell-command-on-region” 【Alt+|】. For example, select a region, then press 【Alt+| wc -w Enter】. This will return the number of words in your selection. (you can also use a elisp command, see: Emacs Lisp: Count Words, Count Chars, Count Region.)
You can have the result replace the selected region. To do that, press 【Ctrl+u】 before the 【Alt+|】.
How to run a shell inside emacs?
Start the shell by 【Alt+x shell】. To run the previous command, press 【Ctrl+↑】.
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)
How to run a second shell inside emacs?
Type 【Ctrl+u Alt+x 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.
In shell-command 【Alt+!】, pressing tab doesn't do command completion?
Get this lisp code by Tsuchiya Masatoshi at http://namazu.org/~tsuchiya/elisp/shell-command.el
On Windows, by default, when you call “shell”, it starts “cmd.exe”.
How to have unix shell commands on Windows?
Type 【Alt+x eshell】.
The “eshell” is similar in purpose to “shell”, but written entirely in elisp. It supports most unix commands such as cd, ls, cp, mkdir, etc. (See: Emacs: What's eshell? eshell vs Shell Difference?)
How to make shell talk to cygwin bash, msys, or PowerShell?
You can install ErgoEmacs or Lennart's EmacsW32. With them, you can type 【Alt+x msys-shell】 to start msys bash. ErgoEmacs also have the command “powershell” which starts a PowerShell interface.
You can install PowerShell interface yourself. See: Emacs PowerShell Modes.
My shell doesn't work in Windows?
Most problem is probably because the environment variable isn't setup correctly. See: Emacs and Microsoft Windows Tips.
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?
Call “info” 【Ctrl+h i】. 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.
See: File Management with Emacs (dired tutorial).
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.
Call “dired” 【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”.
Once in dired, 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.
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, call shell-command 【Alt+!】 then give your grep command, e.g. grep -r elephant *txt.
Or, in Emacs, you can call “list-matching-lines”, “delete-matching-lines”, “delete-non-matching-lines”. See: List Matching Lines and Delete Matching Lines in Emacs.
How to grep all files in a dir?
To call the unix grep command, call shell-command 【Alt+!】 then give your grep command, e.g. grep -r elephant ~/mary/Documents
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.
There's also a “rgrep” and “lgrep”. Try them.
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.
Call “find-dired”, then mark the target files. Then, call “dired-do-query-replace-regexp” to do the actual find & replace. Then, use ibuffer to save and close files in batch. For detail, See: Interactively Find & Replace String Patterns on Multiple Files .
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.
While running emacs in a text terminal, how to invoke the text-based menu?
Type 【Alt+`】 (tmm-menubar).
How to edit a file that requires root privilege?
When you get the file path prompt, type /sudo:: in front of it. e.g. /sudo::/etc/bashrc. This will load up tramp.
A more basic, but inconvenient solution, is to press 【Alt+!】 (“shell-command”), then type “sudo chown ‹file name›”. Then, edit the file, then change the owner back. See also: http://www.emacswiki.org/emacs/SudoSave.
If you need to work as sys admin a lot, you can always start another emacs instance by “sudo emacs”. Make the background color reddish, so you know that's the dangerous one. Use “set-background-color” to set the color.