Return to the Section 101 Homepage or the Introduction to Emacs and the Shell.


Emacs Reference Sheet

Notation

* C-x indicates that x should be pressed while holding down the Control key.
* M-x indicates that x should be pressed while holding down the Meta key (The diamond, Alt, Esc, or Windows key, depending on the system).
* C-x, C-y indicates that Control and x should be pressed and then Control and y should be pressed (in sequence).

Buffers

All text in Emacs is edited in a buffer. Each file has its own buffer. At the bottom of the window is the "mini buffer", where Emacs may present status information or ask for additional information when you enter a command.

Basic Commands

These are the most essential commands needed to use Emacs:
* C-x, C-f - Open the specified file. If the file does not exist, it will be created.
* C-x, C-s - Save the buffer.
* C-x, C-c - Close emacs.
* C-g - "Cancel"; If stuck, hit this (more than once if necessary) to return to normal editing mode.

Why Learn More Commands?

Although many things can be done with the menus, they can often be done much more quickly and with less annoyance using a keyboard shortcut. At first this may not be true; It may take a while for the action to sink in and become automatic. But once this happens, many people find that they can fly through text. Also, its nice to be able to lean back and only use the mouse occasionally.

Further, there are many commands (Generally they are more advanced) that cannot be accessed through the menus. To use these commands, one must use the keyboard.

Finally, it is possible to customize the keyboard interactions, while it is not possible to change the menus.

More Commands

Moving Around -
* C-a - Move to the beginning of the line.
* C-e - Move to the end of the line.
* M-f - Move forward one word.
* M-b - Move backward one word.
* M-< - Move to the beginning of the buffer. The "<" character requires that shift be held down.
* M-> - Move to the end of the buffer. Again, shift must be held down.

Searching and Replacing -
* C-s - Search forward.
* C-r - Search backward (reverse).
* M-% - Search and replace. Begins at cursor (so matches before the cursor wont be seen); Move the cursor to the beginning of text before invoking command in order to replace all matches. At each match, hit "y" to replace or "n" or "enter" to leave unchanged.

Compiling -
* C-x, C-e - Compile. To compile Java, enter javac -g fileName.
* C-` (Backtick) - Move cursor to line of next error.

Buffers

Many buffers can be open at once. To select the buffer to work on, either choose it from the Buffers menu or:
* C-x, b - To switch buffers. Will prompt for the name of the buffer, which will be the name of the file (Assuming that file is open).
* C-x, C-b - To list all open buffers.
* C-x, k - To close the current buffer.

Windows

* C-x, 2 - To split the current window in half vertically.
* C-x, o - To cycle between sub windows.
* C-x, 0 (Zero) - To remove the current sub window.
* C-x, 1 - To remove all but the current sub window.

Copying and Pasting

In Emacs, "killing" is synonymous with copying and "yanking" is synonymous with pasting.

Pasting is fairly straightforward: It simply inserts the copied text beginning at the current position of the cursor.
C-x, y - Paste.

There are many ways to copy text. Often, these methods are also good for removing text (Even if you dont intend to paste it somewhere else).
C-k - Kill the rest of the line (All the way to, but not including, the newline), starting at the cursor. Using C-k on a line with only whitespace will remove the entire line.

To copy a region, that region must first be selected. This can be done with the mouse, or with the keyboard:
First move the cursor to the start of the region and hit C-space. This sets the "point". Then move the cursor to the end of the region. Finally, hit C-w to cut the text (Copy and remove it) or M-w to just copy the text.

Undo

Emacs combines undo and redo into a single command by keeping a single list of actions.
C-/ - Undo. Each time you hit this, it will undo an item in the list. But, as soon as you do something other than undo (Such as move to the next line), all of those undo's become part of the list. Hitting C-/ at this point will undo the last item in the list, which happens to be an undo; You will now be redo'ing.

Tab Autocompletion

Among its many convenience features, Emacs offers tab automcompletion. That is, it will try to guess the answer to some question it asks in the mini-buffer based on a few characters you type. For example, if you want to open a buffer, you might type C-x, b and then "no" and then hit tab. Supposing you have a buffer with the name "notes.txt" open, Emacs will fill in the complete name. Now you can hit enter and switch to that buffer.

Sometimes Emacs is unable to autocomplete because it doesnt have enough information (specifically, there is more than one prefix match on the text you have entered). In this case, you can hit "tab" twice and a list of options will be displayed. Type a few more characters (enough to distinguish your choice from the others) and then try hitting tab again to autocomplete.

Autocompletion can be used when opening a file, switching buffers, and so on.

Some Shells, such as Bash and the K shell, have tab autocompletion. Some also support Emacs commands.

Extra Resources

Where to Learn Even More Commands

There are many other very useful commands. Here are some suggestions on places to find out about them:
* Search for a specific command - Search the Web for a command that will solve a particular problem you are having.
* Ask someone else.
* Read tutorials - There a lot of good tutorials and lists of useful commands on the Web.
* Read the Emacs manual - Available through C-x, i.
* Accidentally discover them - Sometimes a typo leads to the discovery of a new command.

If you get good, you might want to see if you can operate without the menu bar. Use M-x menu-bar-mode to hide it.



Return to the Section 101 Homepage or the Introduction to Emacs and the Shell.