Tips on Long Term Emacs Productivity

By Xah Lee. Date: . Last updated: .

I have used emacs daily since 1998, on average perhaps 5 hours a day. I also program emacs lisp since 2005. On average, perhaps 10 hours per week, and have created some major modes 〔➤see Xah Emacs Packages〕. Here are 7 general emacs tips that's the most important in emacs productivity to me.

① Everything is a Command

In emacs, every keystroke is bound to a command (practically speaking). For example: e runs self-insert-command, runs next-line, 【Alt+x】 runs execute-extended-command. Any key or key combination or key sequence you press is ultimately a command call. There are some 3k commands in emacs out of the box. Most commonly used commands have a keyboard shortcut. For example, moving the cursor, opening file, copy paste, close a file, search text.

The following will let you execute any command, or cancel it.

Command NameKeyPurpose
execute-extended-commandAlt+xExecute a 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 key, or a key of a given command name.

Command NameKeyPurpose
describe-keyCtrl+h kShow the command name of a key (or menu)
describe-functionCtrl+h fShow the key of a command (and other info about the command/function)

The following will let you find out any commands or keys of a major mode, and any command that you forgot the name.

Command NameKeyPurpose
describe-modeCtrl+h mRead the doc string of the current mode. For example, find out what commands it provides.
apropos-commandCtrl+h aList all commands whose name contain a given string.

The essence of emacs is about commands and their keys. Most frequently used commands have a key by default. By mastering the above 6 commands, you will be able to find out any commands or keys, in about any use of emacs.

② Master Window Splitting

Use window splitting commands extensively, and give them easy keys.

In emacs, the window is split into panes frequently. It may be split by a shell-command's output, by various text matching commands (⁖ list-matching-lines, grep-find, grep, rgrep), by emacs's various diff commands (diff, ediff, diff-backup, …), by viewing man page manual-entry, by list-buffers, by viewing doc string (For example, describe-function), by calc or calendar, or by lisp error messages. In emacs-generated panes, usually you can type q to close the pane if your cursor is there, but not always. Most of the time the cursor stays in the original pane, but some command will place cursor in the new pane.

The default key 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】. These keys are cumbersome.

Here's a example of setting up easy keys for splitting windows:

(The suggested Alt key choices above are from ErgoEmacs Keybinding.)

;; easy keys to split window. Key based on ErgoEmacs keybinding
(global-set-key (kbd "M-3") 'delete-other-windows) ; expand current pane
(global-set-key (kbd "M-4") 'split-window-below) ; split pane top/bottom
(global-set-key (kbd "M-2") 'delete-window) ; close current pane
(global-set-key (kbd "M-s") 'other-window) ; cursor to other pane

By using easy keys, you'll save a lot time opening or closing split panes. Also, now you can split and unsplit panes whenever you want and frequently. For example, i frequently have one pane showing dired and another pane for a different dir, or file content, or 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-forwardCtrl+s】 to the location and edit, while viewing the original position in the other pane. When done, just close the new pane, and your cursor is at the original position.)

③ Master Dired

In coding, almost every hour you need to look at different files or directories, especially if you are sys admin. You'll need to do copying, deleting, renaming files or directories. Emacs provides a file management mode, called dired.

Dired is very useful. Once you master it, you will almost never use a graphical desktop nor command line shell. The only time i need to switch to OS's graphical desktop is when dealing with special files such as {video, sound, images, …}. 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, …}. Doing sys admin on Solaris for 4 years, i almost always do these operations using dired or shell-command within emacs. 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, using the same dired interface and commands. For a tutorial about dired, see File Management with Emacs.

Also, in combination of dired, you should master the command shell-commandAlt+!】 and shell. They compliment dired very well, especially in combination of easy keys for window spliting. 〔➤see Emacs Shell Tutorial〕.

④ Master Buffer Switching

In emacs, every file is represented in a “buffer”. Practically, it means a single window is used to represent several different files or work area, but only one is shown. (similar to tabbed window in web browser, but without showing the tabs.)

A typical emacs user will have tens of buffers in a session. Many emacs users don't bother closing files, so they often have hundreds buffers open. Master how to manipulate buffers will benefit you greatly.

Use ibuffer, ido-mode

In emacs 22 (released in 2007), there's a new mode called ibuffer and ido-mode. ibuffer is a major improvement of the classic “Buffer menu” mode, with syntax coloring for file types. ido-mode provide you automatic name completion.

emacs ibuffer
A screenshot of ibuffer mode.

For details, see: Emacs: List/Switch Buffers.

(If you are looking for commands to switch user buffers, they are moved to Emacs: Next/Previous User Buffer)

⑤ Remap Most Frequently Used Keys

The cursor moving commands are statistically the most frequently used keys. You use them every few seconds.

Emacs's default cursor moving keys 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, Ctrl key is typed by the weak pinky finger. The Alt under thumb is much easier to type. So, remap keys so that Alt with a home-row keys move 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: ErgoEmacs Keybinding.

The execute-extended-commandMeta+x】 is frequently used. Best to give it a single key. On Linux, by default it's the ▤ Menu key. If you don't have that key, or if you are on Windows or Mac, you can set F8 or Caps Lock to it. Here's example of setting F8:

(global-set-key (kbd "<f8>") 'execute-extended-command)

If you want Caps Lock, you'll need external utility. see: System-wide ErgoEmacs Keybinding for Windows, Mac, Bash.

⑥ Master Find Replace and Emacs Regex

Searching text and find replace is the heart of text processing. I use it many times every hour. The following are the most useful search or find replace commands:

Command NameKeyTargetDescription
isearch-forwardCtrl+scursor point to end (cycles back to file beginning)interactive search
query-replaceAlt+%text selection, or cursor point to endinteractive find/replace
query-replace-regexpCtrl+Alt+%text selection, or cusor point to endinteractive find replace with regex pattern
dired-do-query-replace-regexpIn dired, Qmultiple filesinteractive find replace with regex pattern on multiple files

For detail about using these commands, in particular, control the case-sensitivity in find or replacement, see: Emacs: Find and Replace Commands.

Many emacs find replace commands uses 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 newline 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 Ctrl 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. The distance from F to left Alt, and distance from J to the right Alt, should be the same.

Bad Keyboard

Apple keyboard
The Apple Keyboard as of 2006. Note the distance of the right side's modifier keys. It is not possible, to comfortably use the right thumb to hold the right ⌘ command key while the index finger remains on the J.

Good Keyboard

Microsoft sculpt ergonomic keyboard 41754
Microsoft Sculpt Ergonomic Keyboard. amazon

The keys are split and oriented for each hand. The {Ctrl, Alt} are very large and symmetrically positioned with respect to each hand's thumb. Also, note that the Alt is almost directly below the F. This means your thumb does not need to curl-in very much. (compare it with the Apple keyboard.)

Microsoft Sculpt Ergonomic Keyboard review

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. See:

PS This article is inspired by Stevey Yegge's blog article: 〔Effective Emacs @〕. I like to thank Steve for his article. There are a few of his tips i disagree, see: Emacs: Criticism of Stevey Yegge's 〈Effective Emacs〉.

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs