Basic Emacs Commands

Thanks to Larry McCann for this excellent summary of basic Emacs commands.

Hopefully, this short commands summary will give the most useful Emacs commands to get you started. More detail can be found in the Emacs Reference Card, and still more information, ( probably more than you need ) can be found in the full GNU Emacs manual.

In Emacs documentation, you will see notation like 'C-f' or 'M-q'. These refer to the Control and Meta keys on your keyboard. The Meta key is usually the Escape key, and is typed by pressing and releasing it, and then typing the rest of the command. The Control key is usually labeled 'Control' or 'CTRL' and is held down while typing the other keys of the command.

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.

Buffer and File Commands

C-x C-f
(Hold down the Control key and type xf) Find file. Loads a file into the editing buffer.

C-x C-s
Save the buffer to the disk file it came from.

C-x C-w
Save the buffer to a specific file. You will be prompted for the name.

C-x k
(Hold down the Control key and type an x, then release the Control key and type a k) Kill the current buffer. (Makes it go away, but does not erase the file.)

C-x 4 f
(Hold down the Control key and type an x, then release the Control key and type a 4 then an f) Find a file in another window. This splits the Emacs window in two.

C-x 4 b
Change to another buffer in another window. This splits the Emacs window in two.

C-x b
Change to another buffer. You will be prompted for the buffer name.

C-x o
Switch to the other buffer in the same split screen.

C-x 1
Make the buffer that the cursor is in the only visible buffer. This does not delete the other buffers, it just hides them.

M-x revert-buffer
If you have made many changes to a buffer, and want to discard them completely and have the file the way it was before you started editing it, type this command. You will be prompted first to revert to the auto-save file; and if you give a negative response, you are asked if you want to revert to the original file.

Movement Commands

C-f
(Hold down the Control key, and type an f) Move forward one character.

M-f
(Press and release the Escape key, then type an f) Move forward one word.

C-b
Move backward one character.

M-b
Move backward one word.

C-n
Move to the next line.

C-p
Move to the previous line.

C-v
Scroll forward one screenful.

M-v
Scroll backward one screenful.

M-C-v
(Press and release the Escape key, then hold down the Control key while typing a v) Scroll forward one screenful in the other window of a split screen. Useful if you want to move in a help buffer or other temporary buffer while keeping the cursor in the current buffer.

C-a
Move to the beginning of the current line.

C-e
Move to the end of the current line.

M-<
Move to the beginning of the buffer.

M->
Move to the end of the buffer.

M-x goto-line
Asks for a line number, then go there. Eg. M-x goto-line 42 puts the cursor at line 42.

Text Commands

DEL
Backward delete character. Commonly called "backspace".

M-DEL
Backward-kill-word. Deletes the word before the cursor.

C-d
Delete the character under the cursor.

M-d
Delete the word under or following the cursor.

C-k
Kill (delete) the text to the right of the cursor on a line. C-k on a blank line kills the carriage return. Two successive C-k's kills the text and the return. The text killed goes into a kill-ring, and can be yanked back. Multiple kills without movement counts as one kill, and can be yanked back as a block.

zC-y
Yank back the last text killed.

M-y
"Yank-pop." To recover text that is not the most recently killed, you must first do a C-y to yank back the last-killed text, then you can do M-y to take the yanked text and replace it with the text from the previous kill. If you then do another M-y, you get the text from the third-to-last kill, etc.

Search and Replace Commands

C-s
Incremental search forward.

C-r
Incremental search backward.

Use C-s or C-r again to find the next (previous) occurrence.

M-%
Query-replace. You are prompted for the original text, and the text to replace it. For each occurrence of the original text, from the point where the cursor is to the end of the buffer, you are prompted for what to do with it. Type a space to replace this one, and go on to the next. Press DEL to pass this one and go on to the next. Press the Escape key to exit query-replace.

Other Interesting Commands

C-_
Undo.

C-x u
Undo.

C-h
Enter help mode. A first time user should type C-h t to get a tutorial. C-h a shows commands matching a string. C-h m describes the current mode. C-h i puts you into info-mode. C-h C-h C-h gives help about help, listing the help options.

C-g
Abort a partially typed or executing command. It also beeps.

C-l
Redraw the screen, centered at the cursor.

C-u
Allows you to invoke a numeric argument to a command. Typing C-u 42 C-f , for example, moves the cursor forward 42 places. As a shortcut, C-u C-f multiplies by 4; and C-u C-u C-f multiplies by 16, etc.

Another method of doing something several times is to prefix the command with ESC- (or M-) and some digit from 1 to 9. For example, M-5 M-d deletes the next 5 words to the right of the cursor.

M-x shell
Starts up a shell process in an emacs buffer. You can move around in the shell buffer just as you would in any other buffer. This is most useful as an alternative to using the UNIX command script in a non-emacs shell to capture program output.

M-x compile
Starts a compilation command in its own buffer. The default compile command is make -k or the command you did 'M-x compile'. The compilation errors go to a compilation buffer. If you have not saved the file, you are prompted to do so; since it is the file that is read, and not the emacs buffer.

C-x `
Parses the errors from a compilation under emacs. The cursor is moved through the buffer containing the source code. Note that this is the grave accent ( or 'back-tick' ), and not the single quote.