5.2.GNU Emacs

Real Programmers

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 Table5.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 SectionA.4.2, “More on GNU Emacs”.

Table5.1.Basic Emacs commands

Opening, Saving, and Exiting---

C-x C-f

Open (or create) file

C-x C-s

Save current buffer

C-x C-w

Save current buffer as...

C-x C-c

Exit Emacs
Getting Help---

C-h ?

Emacs help

C-h t

Emacs tutorial
Keys Found in Other Editors---

Arrow keys

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

C-u [number]

Repeat command to follow (default value is 4)

Table5.2.Moving the cursor in Emacs

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

Table5.3.Manipulating text in Emacs

Deleting Text---


Delete from cursor to end of line


Delete from cursor to end of current word


Delete character
Selecting, Cutting, and Pasting---

C-x h

Select all


Set mark (for text selection)


Cut selected text (within Emacs)


Copy selected text (within Emacs)


Paste text

Table5.4.Additional Emacs commands

Search and Replace---


Incremental search


Find and replace
Windows and Buffers---

C-x 2

Split screen in two

C-x 1

Recombine screen into one

C-x b

Switch to another buffer

C-x C-b

Bring up buffer list

C-x k

Kill (close) buffer
Just For Fun---

M-x tetris

Play Tetris

M-x doctor

Emacs psychotherapist

[41] Debra Cameron et al., Learning GNU Emacs, 3rd ed. (Sebastopol: O'Reilly, 2003), ix.

[42] From the Emacs built-in tutorial.

Back to Guide main page