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. 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 contains 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 (e.g. list-matching-lines, grep-find, grep, rgrep), by emacs's various diff commands (diff, ediff, diff-backup, etc.), 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.

For a tutorial about dired, see Emacs: File Manager, dired .

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
emacs ibuffer mode

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

⑤ Remap Most Frequently Used Keys

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

[see Emacs Command Frequency Statistics]

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, use: Emacs: Xah Fly Keys or 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 to it. Here's example of setting F8:

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

[see Emacs: How to Define Keys]

⑥ 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 commands use regex. For example:

Mastering emacs regex will be a good investment.

For tutorial on most important tips of emacs's regex, see: Emacs: Regex Tutorial.

⑦ 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 73760
Microsoft Sculpt Ergonomic Keyboard. Buy at 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.)

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 At sites.google.com , accessed on 2010-01-20 ]. I like to thank Steve for his article. There are a few of his tips i disagree, see: Criticism of Stevey Yegge's “Effective Emacs”

Emacs Quick Start

  1. Where to Download?
  2. Emacs First Day
  3. Basic Emacs Keys
  4. Basic Tips
  5. Less-known Tips
  6. Fun with Emacs
  7. Emacs on Windows
  8. Build Emacs on Linux
  9. Build Emacs from git

Manage Windows

  1. Split Windows
  2. Save Split Windows Config
  3. Make Buffer Flow Side-by-Side

File

  1. Emacs Bookmark
  2. Open Recently Opened
  3. Open File Fast
  4. Open Last Closed File
  5. Open File Path Under Cursor
  6. Copy File Path

Buffer

  1. List Buffers
  2. Switch Buffer
  3. Ido Mode
  4. New Empty Buffer
  5. Next/Previous User Buffer

Dired

  1. File Manager, dired
  2. Batch Rename Files
  3. Dired Sort File by Time, Size
  4. Rename File, Space to Hyphen/Lowline

On Current File

  1. Run Current File
  2. Delete Current File
  3. Backup Current File
  4. Convert File Line Ending
  5. Open File in External App

Find Replace

  1. Search Text in Buffer
  2. isearch Current Word
  3. Isearch Whitespace Regex
  4. Highlight Text in Buffer
  5. List Delete Highlight Sort Lines
  6. Search Text in Directory
  7. Find Replace in Buffer
  8. Find Replace in Directory
  9. xah-find.el, Find Replace in Pure Elisp
  10. Regex Tutorial

Copy/Paste

  1. Copy/Paste and kill-ring History
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Show kill-ring
  6. Copy to Register
  7. 1 Key to Copy/Paste from Register
  8. Copy Append
  9. Sync Copy/Paste Clipboards with Linux X11

Unicode

  1. Unicode Basics
  2. Unicode Tutorial
  3. Emacs File Encoding FAQ
  4. Best Unicode Fonts for Programer
  5. Elisp: Unicode Escape Sequence
  6. Xah Math Input Mode
  7. Xah Unicode Browser Mode

Font

  1. Font Setup
  2. Proportional Font
  3. Cycle Fonts by Command
  4. Set Line Spacing

Whitespace

  1. Clean Empty Lines
  2. Delete Trailing Whitespace
  3. Delete Whitespace around Cursor
  4. Tabs, Space, Indentation Setup
  5. Newline Representation ^M ^J ^L
  6. Make Whitespaces Visible

Rectangle Edit

  1. Edit Column Text, Rectangle Commands
  2. Insert Alphabets A to Z
  3. Copy Rectangle Region to kill-ring
  4. Align Text
  5. picture-mode, ASCII Diagram

Line Wrap

  1. Hard Wrap Lines
  2. Toggle Line Wrap
  3. Reformat Lines for Source Code
  4. Visual Line Mode, Wrap Lines
  5. Novel Reading Mode
  6. toggle-word-wrap

Shell

  1. Run Shell in Emacs
  2. Why Run Shell in Emacs?
  3. Difference between shell, term, eshell
  4. eshell
  5. View Linux man Page

View Things

  1. Spell Checking
  2. View Info Page
  3. View Image File
  4. View Image Thumbnails
  5. Eww Web Browser

Org Mode

  1. Org Mode Markup Cheatsheet
  2. org-mode Tutorial
  3. Source Code Markup
  4. Org Mode for Todo
  5. Org Mode Customization
  6. Elisp: Parse Org Mode

HTML

  1. Emacs HTML Tips
  2. Insert HTML Tag
  3. Add Paragraph Tags
  4. Color CSS Color Values
  5. URL to Link, Simple
  6. URL to Link, with Date
  7. Link to Dead Link
  8. Citation to Link
  9. Word to Wikipedia Link
  10. Wikipedia URL to Link
  11. Amazon URL to Link
  12. Google Map URL to Link 🌐
  13. Chinese Char to Ref Link
  14. Image Path to Link
  15. Extract URL
  16. URL Percent Decode/Encode
  17. Replace HTML Entities
  18. Lines to List
  19. Lines to Table
  20. Color Source Code
  21. update-title
  22. CSS Compressor
  23. Markup Function Names
  24. Make Ruby Annotation
  25. Move Image File
  26. Xah HTML Mode

Emacs Efficiency

  1. Keyboard Macro
  2. Completion
  3. Abbrev Mode
  4. Interactive Abbrev
  5. Use Alias for Fast M-x
  6. Jump to Previous Mark
  7. Repeat Last Prompted Command
  8. Effective Windows Management
  9. Tips on Long Term Emacs Productivity

Misc

  1. Using magit for git
  2. Emergency vi
  3. Command to Open Menu
  4. Edit Tables
  5. Emacs Chinese Input
  6. GUI vs Terminal

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc