If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

Tips on Long Term Emacs Productivity

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.

Everything is a Command

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 NameShortcutPurpose
execute-extended-commandAlt+xExecute a given command by name
keyboard-quitCtrl+gCancel 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 NameShortcutPurpose
describe-keyCtrl+h kFind out what command is associated a given shortcut (or menu)
describe-functionCtrl+h fFind 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 NameShortcutPurpose
describe-modeCtrl+h mRead the inline doc of the current mode. For example, find out what commands it provides.
apropos-commandCtrl+h aList 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.

Master Window Splitting

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” to 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”.

Master Dired

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 d” (or “Alt+x dired”), 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).

Master Buffer Switching

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:

CommandShortcutPurpose
next-bufferCtrl+x Ctrl+switch to next buffer
previous-bufferCtrl+x Ctrl+switch to previous buffer
list-buffersCtrl+x Ctrl+bshow all buffers
switch-to-bufferCtrl+x bswitch 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.

emacs ibuffer

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 ibuffer mark

Emacs's ibuffer mode's “Mark” menu.

Here's the ibuffer commands i use frequently:

ShortcutPurpose
mMark a buffer.
uUnmark a buffer.
*uMark unsaved buffers.
SSave marked buffer.
DClose marked buffers.
**Unmark all.
%mMark by mode name (e.g. all html files, all java files)
%fMark 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 

Remap Most Frequently Used Shortcuts

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.

Master emacs regex and Find-Replace commands

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 NameShortcutTargetDescription
isearch-forwardCtrl+scursor point to end (cycles back to file beginning)interactive search
query-replaceAlt+%region, or cursor point to endinteractive find and replace
query-replace-regexpCtrl+Alt+%region, or cusor point to endinteractive find and replace with regex pattern
dired-do-query-replace-regexpIn dired, “Qmultiple filesinteractive 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.

Get A Good Keyboard

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

Apple keyboard

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 Natural Multimedia keyboard

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.

PS This article is inspired by Stevey Yegge's blog article: Effective Emacs (2006) Source. I like to thank Steve for his article. There are a few of his tips i disagree strongly, see: Stevey Yegge's Effective Emacs.

2007-11
© 2005 by Xah Lee.