Real Programmers
Described as “a complete working environment,” GNU Emacs has all of the functionality that you could ever want from a text editor.[41] It is also, in my opinion, considerably easier to use than vi. You may wish to try both vi and Emacs and see which one you prefer. You can start Emacs by typing emacs or emacs file_to_edit
(such as emacs lab0.c
) at the prompt.
Emacs makes frequent use of the Control (Ctrl) and Meta keys, but don’t bother looking for the Meta key on your keyboard: I don’t know of any modern keyboards that have one. Instead, the Escape (Esc) key functions as the Meta key. In most configurations of Emacs, the Alt key also works as a Meta key. To type a command such as C-x C-c
, press Ctrl and X, let go of the Ctrl and X keys, then press Ctrl and C.
Instead of working directly with files, when you use Emacs, you work with temporary buffers that affect the file on disk only when you save the buffer. When you create a file with C-x C-f
, that file isn't saved to disk (that is, actually created) until you make a change in the initially blank buffer and then save it with C-x C-s
.
Any text that you cut, copy, or delete (using the commands in Table 5.3, “Manipulating text in Emacs” below) is moved to the kill ring, where Emacs stores such text until you paste it (or yank it, as it is also called) using C-y
. Do not confuse the kill ring with the clipboard that exists in Windows or in Mac OS X: text in the kill ring can only be used within Emacs. Graphical versions of Emacs, however, provide mechanisms for working with the system clipboard via the pull-down menu.
The C-u
shortcut allows you to repeat a command for a given number of times. For example, to scroll down to the 50th line of a buffer when the cursor is initially at the top of the buffer, type C-u 5 0 C-n
(without pressing Enter).
If you accidentally type the start of some command in Emacs, you can use the C-g
shortcut to discard the partially typed command. C-g
can also be used to interrupt a running Emacs command.[42]
You can use M-x
to type in longer commands. For example, to switch into C++ editing mode, type M-x c++-mode
and then press Enter. As with the shell, however, you can use Tab completion by typing M-x c+
and then pressing the Tab key. You can also use Tab completion with file names when you are trying to open files with C-x C-f
.
References to further reading on Emacs can be found in Section A.4.2, “More on GNU Emacs”.
Table 5.1. Basic Emacs commands
Category | Command | Action |
---|---|---|
Opening, Saving, and Exiting | --- | |
~ |
| Open (or create) file |
~ |
| Save current buffer |
~ |
| Save current buffer as... |
~ |
| Exit Emacs |
Getting Help | --- | |
~ |
| Emacs help |
~ |
| Emacs tutorial |
Keys Found in Other Editors | --- | |
~ |
| Move cursor by one character/line |
~ |
| Delete one character at a time |
Commands on Commands | --- | |
~ |
| Quit command being typed (may need to press repeatedly) |
~ |
| Undo previously typed command |
~ |
| Repeat command to follow (default value is 4) |
Table 5.2. Moving the cursor in Emacs
Category | Command | Action |
---|---|---|
Moving to Start and to End | --- | |
~ |
| Move to start of buffer |
~ |
| Move to end of buffer |
~ |
| Move to start of line |
~ |
| Move to end of line |
Moving Up and Down (Across Lines) | --- | |
~ |
| Move up by one screen |
~ |
| Move down by one screen |
~ |
| Move to previous line |
~ |
| Move to next line |
Moving Forward and Backward (Within a Line) | --- | |
~ |
| Move backward by one word |
~ |
| Move forward by one word |
~ |
| Move backward by one character |
~ |
| Move forward by one character |
Table 5.3. Manipulating text in Emacs
Category | Command | Action |
---|---|---|
Deleting Text | --- | |
~ |
| Delete from cursor to end of line |
~ |
| Delete from cursor to end of current word |
~ |
| Delete character |
Selecting, Cutting, and Pasting | --- | |
~ |
| Select all |
~ |
| Set mark (for text selection) |
~ |
| Cut selected text (within Emacs) |
~ |
| Copy selected text (within Emacs) |
~ |
| Paste text |
Table 5.4. Additional Emacs commands
Category | Command | Action |
---|---|---|
Search and Replace | --- | |
~ |
| Incremental search |
~ |
| Find and replace |
Windows and Buffers | --- | |
~ |
| Split screen in two |
~ |
| Recombine screen into one |
~ |
| Switch to another buffer |
~ |
| Bring up buffer list |
~ |
| Kill (close) buffer |
Just For Fun | --- | |
~ |
| Play Tetris |
~ |
| Emacs psychotherapist |