Emacs: Master Tips
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 let you execute any command, or cancel it.
execute-extended-command
【Alt+x】- Execute a command by name.
keyboard-quit
【Ctrl+g】- Cancel any key sequence or command in progress.
The following let you find out any command's name of a given key, or a key of a given command name.
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 let you find out any commands or keys of a major mode, and any command that you forgot the name.
describe-mode
【Ctrl+h m】- Read the doc string of the current mode. For example, find out what commands it provides.
apropos-command
【Ctrl+h a】- List 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
), by diff commands (e.g.
diff
.), by
list-buffers
, by viewing doc string (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:
- Ctrl+3
- unsplit. (3 because it's one of the most easy number key).
- Ctrl+4
- split into top/bottom.
- Ctrl+5
- close current pane.
- Ctrl+o
- move cursor to the next pane.
(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.
(e.g.
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.
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 Operating System'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, etc}.
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-command
【Alt+!】 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.
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-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 to it. Here's example of setting F8:
(global-set-key (kbd "<f8>") 'execute-extended-command)
〔see Emacs Keys: Define Key〕
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:
isearch-forward
【Ctrl+s】- interactive search.
query-replace
【Alt+%】- interactive find/replace.
query-replace-regexp
【Ctrl+Alt+%】- interactive find replace with regex pattern.
dired-do-query-replace-regexp
(In dired, Q)- 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: Emacs: Find Replace in Current File.
Many emacs commands use 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.
For tutorial on most important tips of emacs's regex, see: Emacs: Regular Expression.
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.
see:
PS This article is inspired by Stevey Yegge's blog article: 〔Effective Emacs At https://sites.google.com/site/steveyegge2/effective-emacs , 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