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 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

CategoryCommandAction
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/Backspace

Delete one character at a time
Commands on Commands---
~

C-g

Quit command being typed (may need to press repeatedly)
~

C-_

Undo previously typed command
~

C-u [number]

Repeat command to follow (default value is 4)

Table 5.2. Moving the cursor in Emacs

CategoryCommandAction
Moving to Start and to End---
~

M-<

Move to start of buffer
~

M->

Move to end of buffer
~

C-a

Move to start of line
~

C-e

Move to end of line
Moving Up and Down (Across Lines)---
~

M-v

Move up by one screen
~

C-v

Move down by one screen
~

C-p

Move to previous line
~

C-n

Move to next line
Moving Forward and Backward (Within a Line)---
~

M-b

Move backward by one word
~

M-f

Move forward by one word
~

C-b

Move backward by one character
~

C-f

Move forward by one character

Table 5.3. Manipulating text in Emacs

CategoryCommandAction
Deleting Text---
~

C-k

Delete from cursor to end of line
~

M-d

Delete from cursor to end of current word
~

C-d

Delete character
Selecting, Cutting, and Pasting---
~

C-x h

Select all
~

C-spacebar

Set mark (for text selection)
~

C-w

Cut selected text (within Emacs)
~

M-w

Copy selected text (within Emacs)
~

C-y

Paste text

Table 5.4. Additional Emacs commands

CategoryCommandAction
Search and Replace---
~

C-s

Incremental search
~

M-%

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