Working with GNU Emacs

Working with GNU Emacs

Text editors are a matter of personal preference, and many UNIX users swear by GNU Emacs. Although it’s intimidating at first, GNU Emacs is one of those software packages that grows on you; it has so many features that many users and programmers often perform all their tasks directly from within GNU Emacs.

A significant advantage of GNU Emacs is its availability on nearly every computer system imaginable, from MS-DOS PCs to any UNIX system. If a system doesn’t have GNU Emacs, you can get it from any one of many sites on the Internet. For your Linux system, you get GNU Emacs on the companion CD-ROMs; you can choose to install it as you install Linux on your system. If you’re just getting started with UNIX text editors, I recommend that you learn to use GNU Emacs and vi. That way, you acquire a skill that’s usable on any UNIX system.

Insider Insight 

Because GNU Emacs is so versatile and powerful, describing it in detail could easily take an entire book, and quite a few books about GNU Emacs are on the market. Most notably, O’Reilly & Associates publishes a GNU Emacs book, Learning GNU Emacs, 2nd Edition, by Debra Cameron, Bill Rosenblatt, and Eric S. Raymond, that you may find useful.

On a text terminal, GNU Emacs runs in text-mode, full-screen display. Under X, GNU Emacs runs in a window. Either way, the basic commands remain the same. The X version also enables you to position the cursor by using the mouse.

The following sections provide a brief introduction to the text-editing features of GNU Emacs.

Starting GNU Emacs

On the Linux console, you start GNU Emacs by typing emacs. If you’re running X, type emacs & in a terminal window to start GNU Emacs. This command launches GNU Emacs with its X-based interface and enables you to continue other work in the terminal window.

Insider Insight 

If you want to run Emacs in a terminal window itself—without the X-based graphical interface in a separate window—and edit a file, just type emacs -nw filename.

After it first starts, GNU Emacs displays a message in a window, as shown in Figure 11-2.

Click To expand
Figure 11-2: The Initial Window That the X Version of GNU Emacs Displays.

The initial GNU Emacs window also shows helpful information in the area where you normally edit the contents of a file. You can use the menus to get more Help on Emacs.

Before I tell you anything else, remember that to quit GNU Emacs, you must press C-x C-c (that’s Ctrl-x, followed by Ctrl-c). As you learn in the section “Typing GNU Emacs Commands,” later in this chapter, all GNU Emacs command keystrokes start with a control character or Escape (which the Emacs documentation refers to as Meta and is abbreviated as M). The left Alt key also works as the Meta key.

Insider Insight 

To remain consistent with the GNU Emacs notation, I use the notation C-x for Ctrl-x and M-x for Esc-x (where x is any character).

Learning GNU Emacs

Although I’ve used GNU Emacs extensively for years, I feel that I’ve barely scratched the surface as far as using its full capabilities. As is true of anything else, your best bet is to start with a small subset of GNU Emacs commands. Then, as you become more proficient with the software, you can gradually add to your repertoire of GNU Emacs commands and features.

GNU Emacs employs some basic concepts that, if you learn them, are helpful. Some of those concepts include the following:

  • As do other text editors (vi or ed), GNU Emacs uses a buffer to maintain the text that you enter and change. You must explicitly save the buffer to update the contents of the file.

  • Unlike vi and ed, GNU Emacs doesn’t require you to type any special command to enter text into the buffer. By default, anything that you type goes into the buffer.

  • GNU Emacs uses the concept of a cursor to indicate the current location in the file. As you type text, GNU Emacs inserts that text in front of the character on which the cursor rests.

  • GNU Emacs uses long, descriptive command names that it binds to (associates with) specific key sequences—these are the key bindings for the GNU Emacs commands. C-x C-c, for example, is bound to the GNU Emacs command save-buffers-kill-emacs.

  • All GNU Emacs key bindings start with a control character (for which you simultaneously press Ctrl and a character) or Escape.

  • GNU Emacs uses several modes, each of which provides a specific type of editing environment. (In C mode, for example, GNU Emacs indents the braces that enclose blocks of C programming language statements.)

  • In the GNU Emacs window, the last screen line is the minibuffer; it displays all commands and filenames that you type. The line second to the bottom is the mode line. On this line, GNU Emacs displays the name of the buffer and the current mode (the default mode being fundamental).

  • You don’t need to start GNU Emacs each time that you want to edit a file. Rather, you start GNU Emacs and then open one or more files for editing. You save and close files that you finish editing.

  • You can use many buffers in GNU Emacs, and you can cut and paste between buffers.

Typing GNU Emacs Commands

GNU Emacs uses an extensive set of commands in which each command has a very long descriptive name, as in the following example:

save-buffer save-buffers-kill-emacs scroll-up previous-line

Most of these commands, however, are bound to somewhat cryptic keystrokes. Otherwise, you’d need to actually type these long commands and wouldn’t get much editing done.

Although you can enter any of these descriptive commands in the minibuffer (at the bottom of the GNU Emacs window), the basic means of entering the commands is through special keystrokes. These keystrokes begin with one of the following characters:

  • A control character that you enter by simultaneously pressing the Ctrl key and the character. GNU Emacs documentation abbreviates each control character as C-x (x is a letter). This book uses the notation Ctrl-x to denote the control character that corresponds to the letter x. The GNU Emacs online Help writes Ctrl-v as C-v.

  • An Escape character. GNU Emacs abbreviates the Esc key, or the Meta key, as M. Thus you write Esc v (Escape, followed by the letter v) as M-v. Notice that, depending on the keyboard, the Meta key may also be the Alt key or some other key, such as the Windows key, on some Windows machines.

Most of the time, you enter the control commands, which require pressing the Ctrl key together with a letter. Ctrl-v or C-v, for example, causes GNU Emacs to move forward one screen.

The commands with an Esc prefix are easier to enter, because you press the keys in sequence: first the Escape key and then the letter. Esc v or M-v, for example, causes GNU Emacs to move backward one screen of text. To enter this command, press Esc first, and then press v. Although the Ctrl and Esc commands may sound complicated, you can learn a basic set very quickly.

Getting Help

The best source of information on GNU Emacs is GNU Emacs itself. For starters, GNU Emacs includes an online tutorial that teaches you its basics. To use the tutorial, type C-h t. GNU Emacs displays the initial screen of the tutorial in its window, as shown in Figure 11-3.

Click To expand
Figure 11-3: GNU Emacs Displays the Online Tutorial After You Press C-h t.

As the instructions near the bottom of Figure 11-3 show, the tutorial guides you through the steps and asks you to try GNU Emacs commands. If you’re new to GNU Emacs, go through the tutorial. Because the tutorial is hands-on, it gives you a good feel for GNU Emacs.

In addition to the tutorial, you can look up the key bindings for various GNU Emacs commands. To see the key bindings, type C-h b. GNU Emacs splits the window and displays a list of key bindings in the bottom half, as shown in Figure 11-4.

Click To expand
Figure 11-4: GNU Emacs Displays the Key Bindings in a Split Window After You Press C-h b.

Each line in the key binding shows the name of the key and the GNU Emacs command associated with that command. If you press the key, GNU Emacs executes the command that’s bound to that key.

Figure 11-4 illustrates GNU Emacs’ ability to split its window into two or more parts. After splitting, each part becomes a separate window with its own buffer.

In Figure 11-4, you can scroll the key-binding list by using the following keystrokes:

  • C-x o switches to the other window (the bottom window, which shows the Help buffer with its key-bindings list).

  • C-v scrolls the contents of the Help buffer.

Table 11-3 lists other GNU Emacs help commands.

Table 11-3: GNU Emacs help Commands



C-h c

Prompts you for a key sequence and briefly describes that sequence

C-h f

Prompts you for a GNU Emacs command name and describes that command

C-h k

Prompts you for a key sequence and describes what that sequence does

C-h l

Displays the last 100 characters typed

C-h m

Displays the current GNU Emacs mode

C-h s

Displays the syntax table for the current buffer

C-h v

Prompts for a variable name and describes that variable

C-h w

Prompts for a command and shows that command’s key binding

Reading a File

After you start GNU Emacs, you can read in a file by typing the C-x C-f command. To open the file /usr/share/emacs/21.2/etc/COPYING, for example, follow these steps in GNU Emacs:

  1. Press C-x C-f. GNU Emacs prompts you for a filename.

  2. Type the filename—/usr/share/emacs/21.2/etc/COPYING—and press Enter. GNU Emacs reads the file into a buffer and displays that buffer in the window.

To open a file in the current directory, you don’t need to type the full directory name; just type the filename. By default, GNU Emacs looks for the file in the current working directory.

Insider Insight 

You don’t need to type the full filename in GNU Emacs; just enter the first few characters of the filename and then press Tab. If the partial name uniquely identifies a file, GNU Emacs completes the filename for you. Thus, all you need to type is the first few characters of a filename—just enough to uniquely identify the file. You can use this shortcut feature to avoid typing long filenames.

Suppose that the current working directory contains the file Makefile, which is the only file with a name that starts with the substring Mak. To load that file in GNU Emacs, press C-x C-f, type Mak, and press Tab. GNU Emacs completes the filename and reads the file Makefile.

Moving around the Buffer

One of the first things that you need to do in GNU Emacs is move around the buffer. The cursor marks the current spot in the buffer; anything that you type goes into the buffer in front of the character under the cursor. Thus, to insert text into a file, you must read in the file, move the cursor to the desired spot, and type the text.

To move around the buffer, you need to move the cursor. You must use control keys to move the cursor in any direction. Following are the six basic cursor-movement commands:

  • C-b moves the cursor backward one character.

  • C-f moves the cursor forward one character.

  • C-n moves the cursor to the following line (while trying to maintain the same column position as in the current line).

  • C-p moves the cursor to the preceding line (while trying to maintain the same column position as in the current line).

  • C-a moves the cursor to the beginning of the current line.

  • C-e moves the cursor to the end of the current line.

If moving one character at a time is too slow, you can move one word at a time by using the following commands:

  • M-f moves the cursor forward one word.

  • M-b moves the cursor backward one word.

You can move in even bigger chunks through the buffer. The following two commands enable you to move one screen at a time:

  • C-v moves forward one screen.

  • M-v moves backward one screen.

You can use the following two other simple cursor-movement commands for really big jumps:

  • M-< (Esc followed by the less-than key) moves the cursor to the beginning of the buffer.

  • M-> (Esc followed by the greater-than key) moves the cursor to the end of the buffer.

Inserting and Deleting Text

Because GNU Emacs doesn’t have special command and insert modes as vi does, you just begin typing to insert text in GNU Emacs. GNU Emacs inserts the text in front of the cursor. GNU Emacs, of course, interprets control characters and Esc as the beginning of a command.

You can delete text in GNU Emacs in the following ways:

  • To delete the character in front of the cursor, press Del.

  • To delete the character on which the cursor rests, press C-d.

  • To delete the word after the cursor, press M-d.

  • To delete the word immediately before the cursor, press M-Del.

  • To delete from the cursor to the end of the line, press C-k. (GNU Emacs refers to this command as “kill the line.”)

    Insider Insight 

    Whenever you delete anything longer than one character, GNU Emacs saves it for you. Press C-y to retrieve the saved text.

You can undo a change by typing the C-x u command. Each time that you press C-x u, GNU Emacs performs the Undo operation for a previous command. To undo the effects of the last two commands, for example, press C-x u twice.

Searching and Replacing

Every text editor offers a search-and-replace capability, and GNU Emacs is no exception. The two most common search commands are as follows:

  • C-s string—incrementally searches forward for string.

  • C-r string—incrementally searches backward for string.

If you press C-s to search forward, GNU Emacs prompts you for a search string in the minibuffer (the last line in the GNU Emacs window). As you enter the characters for the string, GNU Emacs jumps to the first occurrence of the string that you’ve typed so far. As soon as you finish typing the search string, GNU Emacs positions the cursor at the end of the next occurrence of the search string (providing, of course, that GNU Emacs finds the string).

To find the next occurrence of the string, press C-s again. To end the search, press Enter. You also can halt the search by using cursor-control commands, such as C-f or C-b.

Searching in the reverse direction works similarly; just press C-r instead of C-s.

GNU Emacs also enables you to replace an occurrence of one string with another. The two basic commands for replacing strings are replace-string and query-replace. The replace-string command replaces all occurrences of one string with another. The query-replace command works similarly, but GNU Emacs prompts you each time that it’s about to replace a string, enabling you to decide which strings it actually replaces.

The query-replace command is bound to the M-% (Esc, followed by %) key sequence. To perform a query-replace operation, first press M-%. GNU Emacs displays the following prompt in the minibuffer:

Query replace:

Enter the string that you want to replace—for example, 2003—and then press Enter. GNU Emacs prompts you for the replacement string, as follows:

Query replace 2002 with:

Enter the replacement string—say, 2004—and press Enter. GNU Emacs moves the cursor to the next occurrence of the string to replace and displays the following prompt in the minibuffer:

Query replacing 2003 with 2004: (? for help)

Type y or press the Spacebar to enable GNU Emacs to replace the string. Otherwise, press n or Del to stop GNU Emacs from replacing the string. In either case, GNU Emacs moves to the next occurrence of the string and repeats the prompt. If no more strings are left, GNU Emacs displays a message in the minibuffer informing you how many occurrences of the string it replaced.

If you type a GNU Emacs command by using M-x, you can press the Spacebar or the Tab key for command completion. To enter the replace-string command, for example, you may start by typing repl and then pressing the Spacebar. That action causes GNU Emacs to display replace- and then pause. Type s and press the spacebar again. GNU Emacs displays replace-string. You then can use the command by pressing Enter. Try it to see what I mean.

Copying and Moving

Another common editing function is copying blocks of text and moving that text to another location in the buffer. The first step in working with a block of text is to define the block.

GNU Emacs defines a block as the text between a mark and the current cursor position. You can think of the mark as a physical marker that GNU Emacs places in the buffer to mark a location. To set the mark, move the cursor to the beginning of the block and then press C-@ or C-Space (pressing the Ctrl key together with the Spacebar). GNU Emacs sets the mark at the current location and displays the following message in the minibuffer:

Mark set

After the mark is set, GNU Emacs treats the text between the mark and the current cursor location as a block. To copy the block, type M-w. That command copies the block to an internal storage area without deleting the block from the current buffer.

If you actually want to cut the block of text, type C-w. That command deletes the block of text from the buffer and moves it to an internal storage area.

To insert the cut (or copied) text at any location, move the cursor to the insertion point, and type C-y. That command causes GNU Emacs to paste the previously cut text in front of the cursor.

Saving Changes

After you edit a buffer, you must write those changes to a file to make them permanent. The GNU Emacs command for saving a buffer to its file is C-x C-s. This command saves the buffer to the file with the same name.

To save the buffer in another file, type the command C-x C-w. GNU Emacs prompts you for a filename. Type the filename, and press Enter to save the buffer in that file. Unlike many DOS or Windows word processors, GNU Emacs doesn’t automatically add a file extension. You must provide the full filename.

Running a Shell in GNU Emacs

GNU Emacs is versatile enough to enable you to access anything in Linux from within a GNU Emacs session. One way to access anything in Linux is to run a shell session. You can do so by using the GNU Emacs command shell.

Insider Insight 

If you’re in the mood for a lighthearted quote, type M-x yow, and Emacs displays a quote from Zippy the Pinhead in the minibuffer. Each time that you type this command, Emacs displays a different quote.