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. 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
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 is a minimal self-sustaining set.

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, “Alt+0” to get rid of the current pane, and “Alt+o” 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-0") 'delete-window) ; was digit-argument
(global-set-key (kbd "M-o") 'other-window) ; was prefix

In emacs, the window is split into panes frequently. It may be split by a shell-command's output, by various emacs grep command (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 exit the pane, but not always. And, some of the split panes put your cursor in the pane, but not all.

By mastering the above 4 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.)

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”). Once in dired, you can look at the the graphical menu “Operate”, “Mark”, “Regexp”, “Immediate”, “Subdir” to see what you can do. Alternatively, type “C-h m” (describe-mode) to read its inline doc. For a guided 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, especially if you are using a unix-like system. (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 tabs, as the tabs in browsers, or IDEs, which basically means a single window is used to represent several different files or work area, but only one is shown.

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

above: A screenshot of ibuffer mode.

In ibuffer mode, those purple are emacs's buffers, those blue are dired, those red with a “>” in front are marked 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 “RET” to switch to 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

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

Here's the ibuffer commands i use frequently:

ShortcutPurpose
*uMark unsaved buffer.
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 and previous-user-buffer that skips emacs's buffers, and you might define next-emacs-buffer, previous-emacs-buffer that cycles thru just the emacs's buffers. Here's the code: modern_operations.el.

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 

If you want emacs to show tabs for each buffer like a modern application would have shown it, you can install the tabbar-mode (2003) by David Ponce, at http://sourceforge.net/projects/emhacks/. For some discussion of usage, see: http://www.emacswiki.org/cgi-bin/wiki/TabBarMode.

emacs tabbar mode

above: The tabbar mode.

The default color for background tabs are dim'd and difficult to read. To change it like the screenshot above, type “Alt-x customize-group” then “tabbar”. Then, scroll to “Tabbar Selected Face”, press Show Face, give the background a value of “bisque”. Do the same for “Tabbar Default Face” and give the Foreground value of “blue”. When done, click “Save for Future Sessions” at the top then close the buffer.

By default, the tabbar mode group buffers into categories, so the current set of tabs shown are buffers having the same mode as the current buffer. To have it show all user buffers as tabs, put the following code in your “.emacs”:

(defun my-tabbar-buffer-groups (buffer)
  "Return the list of group names BUFFER belongs to.
This function is a custom function for tabbar-mode's tabbar-buffer-groups.
This function group all buffers into 2 groups:
Those user buffer, and those emacs buffer.
Emacs buffer are those starting with “*”."
  (with-current-buffer (get-buffer buffer)
    (if (string-equal "*" (substring (buffer-name) 0 1))
        '("emacs-buffer")
        '("user-buffer"))))

(setq tabbar-buffer-groups-function 'my-tabbar-buffer-groups)

(global-set-key (kbd "C-S-<tab>") 'tabbar-backward)
(global-set-key (kbd "C-<tab>") 'tabbar-forward)

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 just about every hour. (Many find-replace commands has a regex version) The following are the most useful search or find-replace commands:

Command NameShortcutTargetDescription
isearch-forward“Ctrl+s”cursor point to end (cycles back to file beginning)interactive search
query-replace“Alt+%”region, or cursor point to endinteractive find and replace
query-replace-regexp“Ctrl+Alt+%”region, or cusor point to endinteractive find and replace with regex pattern
dired-do-query-replace-regexpIn dired, “Q”multiple 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, rgrep, dired-do-rename-regexp (Q), apropos-command (Ctrl+h a). Mastering emacs regex will be a good investment. Also, you should know how to enter TAB or Return character in emacs regex. (“\t” or “\n” will work only in lisp code.) 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

above: The Apple keyboard as of 2006. Note the ridiculous distance of the right side's modifier keys. It is not possible, to use the right thumb to press the alt key while the index finger remains on the J.

GOOD

Microsoft Natural Multimedia keyboard

above: The Microsoft Natural Multimedia 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 Natural 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.


Steve Yegge's Effective Emacs

This article is inspired by Steve Yegge's Effective Emacs (2006): http://steve.yegge.googlepages.com/effective-emacs. I like to thank Steve for his article.

However, I disagree with some of his tips strongly. In particular, his tips about Swapping Cap-lock and Control key, Invoking M-x without the Alt key, the “Lose the UI”. In the following, i give a brief explanation why i disagree.

Swapping Cap-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 use both Control key lessens the repetitive burden. Swapping Cap-lock and Control can be a good solution on laptop keyboards. I think better is to actually swap Control and Alt. For detail, see How To Avoid The Emacs Pinky Problem and Why Emacs's Keyboard Shortcuts Are Painful.

By the same reasoning above, i don't find the advice of “Invoking M-x without the Alt key” by remapping to a Control key combination, a very good one. Personally, i remap M-x to M-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 emacs career, from 1998 to 2004 inclusive, 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 it gives a listing of 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 hardly ever use. 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 doesn't save much screen space. 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”.)

Page created: 2007-11.
© 2005 by Xah Lee.
Xah Signet