Xah Lee, 2008-05-29, …, 2012-01-01
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 Modes.). Here are 7 general emacs tips that's the most important in emacs productivity to me.
In emacs, every keystroke is bound to a command (practically speaking). For example: e runs self-insert-command, → runs forward-char, 【Alt+x】 runs execute-extended-command. Any key or key combination or key sequence you press is ultimately a command call. There are ~3k commands in emacs out of the box (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.
The following will let you execute any command, or cancel it.
| Command Name | Key | Purpose |
|---|---|---|
| execute-extended-command | 【Alt+x】 | Execute a 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 key, or a key of a given command name.
| Command Name | Key | Purpose |
|---|---|---|
| describe-key | 【Ctrl+h k】 | Show the command name of a key (or menu) |
| describe-function | 【Ctrl+h f】 | Show 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 Name | Key | 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 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.
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, …), by viewing man page manual-entry, by list-buffers, by viewing inline doc (e.g. 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:
;; 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-vertically) ; split pane top/bottom (global-set-key (kbd "M-s") 'other-window) ; cursor to other pane (global-set-key (kbd "M-0") 'delete-window) ; close current pane ; note: syntax for F5 key is (kbd "<f5>")
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 with easy keys. 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-forward 【Ctrl+s】 to the location and edit, while viewing the original position in the other pane.)
(The suggested Alt key choices above are from ErgoEmacs Keybinding.)
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 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 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-command 【Alt+!】 and shell. They compliment dired very well, especially in combination of easy keys for window spliting. (See: Emacs Shell Tutorial).
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, you can think of it as “tabbed window”. It 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. Many emacs users don't bother closing files, so they often have hundreds buffers open. Master how to manipulate buffers will benefit you greatly.
In emacs 22 (released in 2007), there's a new mode called ibuffer. (start it by 【Alt+x ibuffer】) ibuffer is a major improvement of the classic “Buffer menu” mode, while practically 100% compatible. It includes color-differentiated buffer listing, and few more powerful regex commands that manipulate buffers in batch. I've been using ibuffers since 2006.
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 keys are very similar to dired. Look at the graphical menu 〖View〗 and 〖Mark〗 for list of commands.
Here's the ibuffer commands i use frequently:
| Key | Purpose |
|---|---|
| m | Mark |
| u | Unmark |
| *u | Mark unsaved |
| S | Save marked buffer |
| D | Close marked buffers |
In ibuffer, the key sequence 【* u S D】 will save all unsaved files and close them. This is particularly useful after you've done a find & replace on hundreds of files with dired-do-query-replace-regexp. (See: Interactively Find & Replace String Patterns on Multiple Files.)
Because ibuffer completely replaces the functionality of list-buffers command, you can alias the command name list-buffers to ibuffer. Like this:
(defalias 'list-buffers 'ibuffer)
With the above, the key for list-buffers 【Ctrl+x Ctrl+b】 will also become for ibuffer.
Emacs often generates a lot internal buffers that users are not interested in cycling thru. e.g.: {*scratch*, *Messages*, *shell*, *Shell Command Output*, *Occur*, *Completions*, *Apropos*, *info*, …}. 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.
(defun next-user-buffer () "Switch to the next user buffer. User buffers are those whose name does not start with *." (interactive) (next-buffer) (let ((i 0)) (while (and (string-match "^*" (buffer-name)) (< i 50)) (setq i (1+ i)) (next-buffer) ))) (defun previous-user-buffer () "Switch to the previous user buffer. User buffers are those whose name does not start with *." (interactive) (previous-buffer) (let ((i 0)) (while (and (string-match "^*" (buffer-name)) (< i 50)) (setq i (1+ i)) (previous-buffer) ))) (defun next-emacs-buffer () "Switch to the next emacs buffer. Emacs buffers are those whose name starts with *." (interactive) (next-buffer) (let ((i 0)) (while (and (not (string-match "^*" (buffer-name))) (< i 50)) (setq i (1+ i)) (next-buffer) ))) (defun previous-emacs-buffer () "Switch to the previous emacs buffer. Emacs buffers are those whose name starts with *." (interactive) (previous-buffer) (let ((i 0)) (while (and (not (string-match "^*" (buffer-name))) (< i 50)) (setq i (1+ i)) (previous-buffer) )))
;; sample easy keys (global-set-key (kbd "<f5>") 'find-file) ; Open file or dir (global-set-key (kbd "<f6>") 'kill-this-buffer) ; Close file (global-set-key (kbd "<C-prior>") 'previous-user-buffer) ; Ctrl+PageUp (global-set-key (kbd "<C-next>") 'next-user-buffer) ; Ctrl+PageDown (global-set-key (kbd "<C-S-prior>") 'previous-emacs-buffer) ; Ctrl+Shift+PageUp (global-set-key (kbd "<C-S-next>") 'next-emacs-buffer) ; Ctrl+Shift+PageDown
In a web browser, often you have tens of tabs open. The 【Ctrl+PageUp】 【Ctrl+PageDown】 keys lets you quickly fly-thru all your tabs to the one you want. Same idea here in emacs. (If you want different keys, see: Emacs: How to Define Keyboard Shortcuts.)
(if you are using ErgoEmacs Keybindings, the functions are already there. The keys for prev/next user buffer are {【Ctrl+PageUp】, 【Ctrl+PageDown】}, 【Ctrl+o】 for open, 【Ctrl+w】 for close.)
iswitchb-buffer is bundled with emacs 23.2 or earlier. It provides smart name completion feature when you want to switch buffer by name (switch-to-buffer). You can turn it on with:
(iswitchb-mode 1)
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-command 【Meta+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.
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 | Key | Target | Description |
|---|---|---|---|
| isearch-forward | 【Ctrl+s】 | cursor point to end (cycles back to file beginning) | interactive search |
| query-replace | 【Alt+%】 | text selection, or cursor point to end | interactive find & replace |
| query-replace-regexp | 【Ctrl+Alt+%】 | text selection, or cusor point to end | interactive find & replace with regex pattern |
| dired-do-query-replace-regexp | In dired, Q | multiple files | interactive 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: Find & Replace with Emacs.
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.
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
GOOD
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 Ctrl 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) @ sites.google.com. I like to thank Steve for his article. There are a few of his tips i disagree strongly, see: Stevey Yegge's Effective Emacs.
blog comments powered by Disqus