Xah Lee, 2006-09
This page provides some tips about using Emacs with Unix. If you find this page incomprehensible, please first be familiar with: Emacs Intermediate.
How to run a unix shell command?
To run single shell command, press “Alt+!” (Alt+x shell-command). This is under the menu “Tools‣Shell Command...”.
To remove the resulting split panes, press “Ctrl+x 1” (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 particular directory's content. Normally, you would switch to a x-term, 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 RET”. 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 “Alt+p”. (next is “Alt+n”). To start a second shell, type “Ctrl+u Alt+x shell”.
If you use shell often, you can create a alias (defalias 'sh 'shell) or a keyboard shortcut.
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 “real” system-console or 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 to those curious: besides the commands “shell” and “term”, there are also “terminal-emulator”, and “eshell”. The commands “term” and “terminal-emulator” seem to be similar but are independent. The “eshell” is similar in purpose to “shell”, but written entirely in elisp, so that, if you are on a non-unix 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
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.
See: File Management with Emacs.
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 “Alt+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 “xahlee.org” in the directory “/public_html”. Suppose your login name for that machine is “mary”.
Here's what you do. Type “Ctrl+x d RET” then move your cursor to the file you want to copy. Type “C”, then give the dir “/ftp:mary@xahlee.org:/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.
Reference: (info "(emacs)Remote Files").
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.
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 \;, by first prompting you in minibuffer, and let you modify it if you want, then it'll 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. For detail, See Interactively Find and 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).
Related essays:
Page created: 2006-07. © 2006 by Xah Lee.