Xah Lee, 2008-05-29
I have used emacs daily since 1998. Typically, i spent several hours inside emacs, everyday, for the past 10 years.
Here are 7 general emacs tips i felt that's the most important in emacs productivity, among all other emacs tips and tricks of my decade-long experience. If you use emacs only occasionally, these tips may not be very meaningful because they are general and does not solve any specific problems. If you have used emacs over a year, or is a dedicated emacs user, you might find these tips helpful.
In emacs, every keystroke is bound to a command (practically speaking). For example, when you type a key “e”, emacs actually runs the command self-insert-command. In other words, any key or key combination or key sequence you press is bound to a command. There are about 3000 commands in emacs (not counting proper elisp functions). Most commonly used commands have a keyboard shortcut. For example, moving the cursor, opening file, copy paste, close a file, search text. To execute a command by name, type “Alt+x” followed by the command name.
The following will let you execute any command, or cancel it.
| Command Name | Shortcut | Purpose |
|---|---|---|
| execute-extended-command | Alt+x | Execute a given command by name |
| keyboard-quit | Ctrl+g | Cancel any key sequence or command in progress |
The following will let you find out any command's name of a given shortcut, or a shortcut of a given command name.
| Command Name | Shortcut | Purpose |
|---|---|---|
| describe-key | Ctrl+h k | Find out what command is associated a given shortcut or menu |
| describe-function | Ctrl+h f | Find out what shortcut(s) is associated to a given function, and what the function do |
The following will let you find out any commands or shortcuts a mode provides, and any command that you forgot the name.
| Command Name | Shortcut | Purpose |
|---|---|---|
| describe-mode | Ctrl+h m | Read the inline doc of the current mode. For example, find out what commands it provides. |
| apropos-command | Ctrl+h a | List all commands who's name contains a given string. |
Any operation in emacs is ultimately a command, and most frequently used ones have a keyboard shortcut. By mastering the above commands, you will be able to find out any commands or shortcuts, in about any use of emacs.
Emacs provides a lot more other self-help commands, but the above are the most useful ones.
Use split-window-vertically extensively, and give it a easy shortcut, such as “Alt+2” for spliting into 2 top and bottom panes, “Alt+1” to expand the pane the cursor is on, and “Alt+s” for moving cursor to the next pane.
;;; WINDOW SPLITING (global-set-key (kbd "M-2") 'split-window-vertically) ; was digit-argument (global-set-key (kbd "M-1") 'delete-other-windows) ; was digit-argument (global-set-key (kbd "M-s") 'other-window) ; was center-line
In emacs, the window is split into panes frequently. It may be split by a shell-command's output, by various text matching commands (e.g. list-matching-lines, grep-find, grep, rgrep), by emacs's various diff commands (diff, ediff, diff-backup, ...), by viewing man page (“Alt+x manual-entry”), by list-buffers, by viewing inline doc (e.g. describe-function), by calc or calendar, or by lisp error messages. In many of the split pane situation, you can type “q” close the pane the cursor is on, but not always. And, some of the split panes put your cursor in the pane, but not all.
By mastering the above 3 generic window splitting commands, with easy-to-press shortcuts, you'll save a lot time opening or closing split panes. Also, by having a easy shortcut, now you can split and unsplit panes whenever you want and frequently. For example, i constantly have one pane showing dired and another pane showing a file content, or one pane of shell. A top/bottom split window is extremely useful when coding. (for example, when i need to edit part of a file based on the current location, i often do a split pane first, then use isearch-forward (Ctrl+s) to the location and edit, while viewing the original position in the other pane.)
Note: the default shortcut to split pane is “Ctrl+x 2”. Expanding the current pane is “Ctrl+x 1”. Switch cursor to the other pane is “Ctrl+x o”. Splitting side by side is: “Ctrl+x 3”.
In coding, almost every hour you need to look at different files or directories, or do copying, deleting, renaming files or directories. Emacs provides a file management mode, called “dired”. (“dired” is acronym for DIRectory EDit. “Directory Editing” is 1980's term for file management.)
Dired is very useful. Once you master it, you will almost never use a graphical desktop nor command line OS shell. The only time i need to switch to OS's graphical desktop is when dealing with special files such as video, sound, images, etc. (emacs can also view image files now.) Emacs dired together with emacs's other commands is also more convenient than unix command line utilities such as ls, cp, mv, mkdir, rmdir, chmod, chown, cat, less, touch, grep, find/xargs, etc. As a sys admin of Solaris for 4 years, i almost always do these operations using dired or emacs's equivalent. Also, emacs uses the same interface of dired for ftp/sftp. So, for example, you can copy, move, delete, change file owner/perm, or edit files on remote servers (may it be Windows, OS X, or Linux), using the same dired interface and commands.
To activate dired, type “Ctrl+x Ctrl+f” (or “Alt+x find-file”), then give a directory path. Once in dired, you can look at the new menu “Operate”, “Mark”, “Regexp”, “Immediate”, “Subdir” to see what you can do. For a tutorial about dired, see File Management with Emacs.
Also, in combination of dired, you should master the command “shell-command” (Alt+!) and “shell”. They compliment dired very well. (For more detail about using shell commands, see Emacs and Unix Tips).
In emacs, every file is represented in a “buffer”. The term “buffer” means a temp area of storage in computer science. From a application user's perspective, it might be better to think of it as “tabbed window” or “workspace”, as these terms are used web browsers or IDEs. It basically means a single window is used to represent several different files or work area, but only one is shown. Emacs's “buffers” feature can be thought of as tabbed window, it's just that it doesn't actually have the tab widgets as visual clue.
A typical emacs user will have tens of buffers in a session. Master how to manipulate buffers will benefit you greatly.
Here's the classic commands related to buffers:
| Command | Shortcut | Purpose |
|---|---|---|
| next-buffer | Ctrl+x Ctrl+→ | switch to next buffer |
| previous-buffer | Ctrl+x Ctrl+← | switch to previous buffer |
| list-buffers | Ctrl+x Ctrl+b | show all buffers |
| switch-to-buffer | Ctrl+x b | switch to a specific buffer |
In emacs 22 (released in 2007), there's a new mode called ibuffer. (start it by “Alt+x ibuffer”) “ibuffers” is a major improvement of the classic “Buffer menu” mode. It includes color-differentiated buffer listing, and few more powerful regex commands that manipulate buffers in batch. I've been using ibuffers since about 2006 with emacs 22 beta, and found it completely replace the “Buffer Menu” mode used by list-buffers.
A screenshot of ibuffer mode.
In ibuffer mode, those purple are emacs generated buffers (typically, of logs, messages, shell outputs, etc.), those blue are directories, those red with a “>” in front are marked buffers, those with a “*” are unsaved buffers. You can do operation in batch to the marked ones, such as save all unsaved files, close all files of a given dir, close all Java files, etc. The concept and shortcuts are pretty much the same as in dired. Move your cursor to a file name and press “Return” to open it. To mark a buffer, press “m”. To close a buffer, press “D”. There are over ten commands for marking; you can have a look at the graphical menu “View” and “Mark”, once you are in ibuffer mode.
Emacs's ibuffer mode's “Mark” menu.
Here's the ibuffer commands i use frequently:
| Shortcut | Purpose |
|---|---|
| m | Mark a buffer. |
| u | Unmark a buffer. |
| *u | Mark unsaved buffers. |
| S | Save marked buffer. |
| D | Close marked buffers. |
| ** | Unmark all. |
| %m | Mark by mode name (e.g. all html files, all java files) |
| %f | Mark by file path (e.g. all files in a dir) |
In ibuffer, the key sequence “*uSD” will save all unsaved files and close them. This is particularly useful after you've done a find-replace operation on tens of files from dired using dired-do-query-replace-regexp (shortcut “Q”). (see Interactively Find and Replace String Patterns on Multiple Files)
You might want to give ibuffer a easy-to-type shortcut. Since it completely replaces the functionality of list-buffers command, you might alias it to ibuffers.
(defalias 'list-buffers 'ibuffer)
Emacs often generates a lot internal buffers that users are not interested in cycling thru. For example: *scratch*, *Messages*, *shell*, *Shell Command Output*, *Occur*, *Completions*, *Apropos*, *info*, etc. You might define your own next-user-buffer that skips emacs's buffers, and you might define next-emacs-buffer that cycles thru just the emacs's buffers. This is implemented in Ergoemacs.
Also, the default shortcut for next-buffer (Ctrl+x Ctrl+→) involves multiple keystrokes, you might define a single-key shortcut for it. Here's some example code:
;; sample easy shortcuts (global-set-key (kbd "<f5>") 'find-file) ; Open file or dir (global-set-key (kbd "<f6>") 'ibuffer) ; list buffers (global-set-key (kbd "<f7>") 'previous-user-buffer) (global-set-key (kbd "<f8>") 'next-user-buffer) (global-set-key (kbd "S-<f7>") 'previous-emacs-buffer) ; Shift+f7 (global-set-key (kbd "S-<f8>") 'next-emacs-buffer) ; Shift+f8 (global-set-key (kbd "<f9>") 'kill-this-buffer) ; Close file
The cursor moving commands are the most frequently used shortcuts. You use them every few seconds.
Emacs's default cursor moving shortcuts are “Ctrl+f”, “Ctrl+b”, “Ctrl+n”, “Ctrl+p”. The keys f, b, n, p are scattered around the keyboard and are not under the home row. Also, Control key is typed by the weak pinky finger. The Meta key (the Alt under thumb) is much easier to type. So, remap keys so that Alt with a home-row key moves the cursor.
;; make cursor movement keys under right hand's home-row. (global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line (global-set-key (kbd "M-l") 'forward-char) ; was downcase-word (global-set-key (kbd "M-i") 'previous-line) ; was tab-to-tab-stop (global-set-key (kbd "M-k") 'next-line) ; was kill-sentence (global-set-key (kbd "M-SPC") 'set-mark-command) ; was just-one-space (global-set-key (kbd "M-a") execute-extended-command) ; was backward-sentence
For more extensive remapping, see: A Ergonomic Keyboard Shortcut Layout For Emacs.
Searching text and find-replace text is tremendously useful. I use it many times every hour. The following are the most useful search or find-replace commands:
| Command Name | Shortcut | Target | Description |
|---|---|---|---|
| isearch-forward | “Ctrl+s” | cursor point to end (cycles back to file beginning) | interactive search |
| query-replace | “Alt+%” | region, or cursor point to end | interactive find and replace |
| query-replace-regexp | “Ctrl+Alt+%” | region, or cusor point to end | interactive find and replace with regex pattern |
| dired-do-query-replace-regexp | In dired, “Q” | multiple files | interactive find and replace with regex pattern on multiple files |
For detail about using these commands, in particular, how to control whether the search is case-sensitive, or whether the replacement is case-sensitive, see: Find and Replace with Emacs.
Many emacs find-replace commands uses a regex. Emacs has many other commands that uses regex. For example, list-matching-lines, delete-matching-lines, highlight-regexp, grep, find-dired, dired-do-query-replace-regexp, dired-do-rename-regexp, apropos-command. Mastering emacs regex will be a good investment. Also, you should know how to enter TAB or Return character when using emacs regex interactively. (“\t” or “\n” does not work.) For a short tutorial on most important tips of emacs's regex, see: Text Pattern Matching in Emacs.
You switch to different applications all day. Web browsers, emacs, terminal, Desktop, music player, image editor, ...etc. Their usage and interface changes, but there is one thing that does not change: Your keyboard.
Your keyboard is a intimate item. You touch it every minute. Emacs in particular, use modifier keys extensively. This may sound silly, but a good keyboard is one of the most important thing in productivity with emacs.
A good keyboard for emacs, should be one with large Alt and Control keys, and they should be available on both sides (one set for each hand, just like Shift key), and the right side's set should be positioned symmetrically (that is, the following two distances should be the same: F to left Alt; J to the right Alt).
BAD
The Apple keyboard as of 2006. Note the ridiculous distance of the right side's modifier keys. It is not possible, to comfortably use the right thumb to hold the right Alt key while the index finger remains on the J.
GOOD
Microsoft's ergonomic keyboard. Note, the keys are split and oriented for each hand. And, the Ctrl, Alt are very large and symmetrically positioned with respect to each hand's thumb. (See A Review of Microsoft Ergonomic Keyboards)
Doesn't matter whether you like ergonomic keyboards, you should take a closer look at your keyboard, and see if it works with emacs well. Also, it's good to develop good habits when pressing the Control key. You should avoid pressing it with pinky, and should learn using alternate hand for Control-key combination, like you would with Shift key. For detail, see: How To Avoid The Emacs Pinky Problem.
This article is inspired by Stevey Yegge's Effective Emacs (2006): http://steve.yegge.googlepages.com/effective-emacs. I like to thank Steve for his article.
I disagree with 3 of his tips strongly. They are: Swapping Caps Lock and Control key, Invoking Meta+x without the Alt key, the “Lose the UI”. In the following, i give a brief explanation of why.
Swapping Caps Lock and Control is not a good solution because it puts all the burden on the left pinky. The Control key on most PC keyboard is at the corner, and is very easy to press with palm. Also, there are 2 Control keys, on both sides. One should use them both like how Shift key is used, by using one hand for the modifier key and the other hand for the letter key. Using both hands avoids the awkward pinky-stretch, and using both left and right Control keys lessens the repetitive burden on one hand. Swapping Caps Lock and Control can be a good solution on laptop keyboards. I think better is to actually swap Control and Alt. For detail, see Why You Should Not Swap Caps Lock With Control.
By the same reasoning above, i don't find the advice of “Invoking Meta+x without the Alt key” by remapping to a Control key combination, a very good one. Personally, i remap Meta+x to Meta+a, since the “a” key is in the home row, and Meta (the Alt key under thumb) is easier to press than Control.
Steve advices users to “Lose the UI”. In general, i also work in emacs exclusively using keyboard. My first 6 years of emacs from 1998 to 2004 are exclusive with text-terminals only (thru telnet/ssh on remote servers). Only after 2004, i started to use emacs in a graphical setting, under Mac OS X. I think graphical user interface is very helpful, because the menu lets a user to see the most useful commands, and can serve as a reminder or cheat-sheet. For example, i've been using dired for over 10 years. Some of dired features i've never used. For example, looking at the dired “Operate” menu, i see that i've actually never used the shortcuts S, H, B, L, T, C-t D, C-t a, C-t t, C-t r. Under its “Mark” menu, i never used “* /”, “.”, “* *”, “M-{”, “M-}”. Similarly, there are many commands i've never used, or aware, that are listed under its Regexp, Immediate, Subdir menus. Maybe i've been missing out something, but emacs has 3000+ commands and lots of modes. Few people need to master all features. But occasionally, i can browse the graphical menu and find out the command that i kept forgetting to use, or see what most important commands are available for a new mode i just installed. I think graphical menus are quite useful in this aspect. Losing them saves at most 2 lines of space, and today's monitors are too big and cheap to be of concern. I would agree that tool bar (the one with icons for opening file, copy/cut/paste, printing, help) is not very useful. (you can take off the tool-bar by using the menu “Options‣Show/Hide‣Tool-bar” then “Options‣Save Options”.)