Basic Editing Commands

This section takes a more detailed look at the fundamental emacs editing commands. It covers straightforward editing of a single file in a single window.

Keys: Notation and Use

Mainstream emacs uses the 128-character ASCII character set. ASCII keyboards have a typewriter-style SHIFT key and a CONTROL key. Some keyboards also have a META (diamond, OPTION, or ALT) key that controls the eighth bit. It takes seven bits to describe an ASCII character; the eighth bit of an eight-bit byte can be used to communicate other information. Because so much of the emacs command set is in the nonprinting CONTROL or META case, Stallman was one of the first to confront the problem of developing a notation for writing about keystrokes.

His solution, although not popular outside the emacs community, is clear and unambiguous (Table 7-1). It uses the capital letters C and M to denote holding down the CONTROL and META (or ALT) keys, respectively, and a few simple acronyms for the most common special characters, such as RET (this book uses RETURN), LFD (LINEFEED), DEL (DELETE), ESC (ESCAPE), SPC (SPACE), and TAB. Most emacs documentation, including the online help, uses this notation.

Table 7-1. emacs key notation


Classic emacs notation

(lowercase) a


(uppercase) SHIFT-a





C-a (do not use SHIFT), equivalent to CONTROL-a




M-A (do use SHIFT), different from M-a




M-C-a (not used frequently)

This use of keys had some problems. Many keyboards had no META key, and some operating systems discarded the META bit. In addition, the emacs character set clashes with XON-XOFF flow control, which also uses CONTROL-S and CONTROL-Q.

Although the flow-control problem still exists, the META key issue was resolved by making it an optional two-key sequence starting with ESCAPE. For instance, you can type ESCAPE-a instead of META-a or type ESCAPE CONTROL-A to get CONTROL-META-a. If the keyboard you are using does not have a META key, you can use the two-key ESCAPE sequence by pressing the ESCAPE key, releasing it, and then pressing the key following the META key in this book. For example, when this book says to press META-r, you can either press the META or ALT key while you press r or press and release ESCAPE and then press r.

Tip: Using emacs from the Terminal utility

By default the Terminal utility does not treat the standard Apple keyboard OPTION key as a META key. You must either use ESCAPE or configure Terminal to use OPTION as META. If you are using a non-Apple keyboard, the key may be labeled ALT, but you must still configure it in the same way. For more information see the tip "Activating the META key" on page 31.

Tip: The notation used in this book

This book uses an uppercase letter following the CONTROL key and a lowercase letter following the META key. In either case you do not have to hold down the SHIFT key while entering a CONTROL or META character. Although the META uppercase character (that is, META-A) is a different character, it is usually set up to cause no action or the same effect as its lowercase counterpart.

Key Sequences and Commands

In emacs the relationship between key sequences (one or more keys that are pressed together or in sequence to issue an emacs command) and commands is very flexible, and there is considerable opportunity for exercising your personal preference. You can translate and remap key sequences to other commands and replace or reprogram commands.

Although most emacs documentation glosses over the details and talks about keystrokes as though they were the commands, it is important to recognize that the underlying machinery remains separate from the key sequences and to understand that you can change the behavior of the key sequences and the commands. For more information refer to "Customizing emacs" on page 238.

META-x: Running a Command Without a Key Binding

The emacs keymaps (the tables, or vectors, that emacs uses to translate key sequences to commands [page 240]) are very crowded, and often it is not possible to bind every command to a key sequence. You can execute any command by name by preceding it with META-x. When you press META-x, the emacs editor prompts you for a command in the Echo Area. After you enter the command name and press RETURN, it executes the command.

Smart completion

When a command has no common key sequence, it is sometimes described as META-x command-name. The emacs editor has a smart completion for most prompted answers, using SPACE or TAB to complete, if possible, to the end of the current word or the whole command, respectively. Forcing a completion past the last unambiguous point or typing ? displays a list of alternatives. You can find more details on smart completion in the online emacs manual.

Numeric Arguments

Some of the emacs editing commands accept a numeric argument as a repetition count. You place this argument immediately before the key sequence for the command. Absence of an argument almost always means a count of 1. Even an ordinary alphabetic character can have a numeric argument, which means "insert this many times." To give a command a numeric argument, you can do either of the following:

  • Press META with each digit (09) or the minus sign (). For example, to insert 10 z characters, type META-1 META-0 z.

  • Use CONTROL-U to begin a string of digits, including the minus sign. For example, to move the cursor forward 20 words, type CONTROL-U 20 META-f.


For convenience, CONTROL-U defaults to multiply by 4 when you do not follow it with a string of one or more digits. For example, entering CONTROL-U r means insert rrrr (4 * 1), whereas CONTROL-U CONTROL-U r means insert rrrrrrrrrrrrrrrr (4 * 4 * 1). For quick partial scrolling of a tall window, you may find it convenient to use repeated sequences of CONTROL-U CONTROL-V to scroll down four lines, CONTROL-U META-v to scroll up four lines, CONTROL-U CONTROL-U CONTROL-V to scroll down 16 lines, or CONTROL-U CONTROL-U META-v to scroll up 16 lines.

Point and the Cursor

Point is the place in a buffer where editing takes place and is where the cursor is positioned. Strictly speaking, Point is the left edge of the cursorit is thought of as lying between two characters.

Each window has its own Point, but there is only one cursor. When the cursor is in a window, moving the cursor also moves Point. Switching the cursor out of a window does not change that window's Point; it is in the same place when you switch the cursor back to that window.

All of the cursor-movement commands described previously also move Point.

Scrolling Through a Buffer


A buffer is likely to be much larger than the window through which it is viewed, so you need a way of moving the display of the buffer contents up or down to position the interesting part in the window. Scrolling forward refers to moving the text upward, with new lines entering at the bottom of the window. Use CONTROL-V or the PAGE DOWN key to scroll forward one window (minus two lines for context). Scrolling backward refers to moving the text downward, with new lines entering at the top of the window. Use META-v or the PAGE UP key to scroll backward one window (again leaving two lines for context). Pressing CONTROL-L clears the screen and repaints it, moving the current line to the center of the window. This command is useful if the screen becomes garbled.

A numeric argument to CONTROL-V or META-v means "scroll that many lines"; thus CONTROL-U 10 CONTROL-V means scroll forward ten lines. A numeric argument to CONTROL-L means "scroll the text so the cursor is on that line of the window," where 0 means the top line and 1 means the bottom, just above the Mode Line. Scrolling occurs automatically if you exceed the window limits with CONTROL-P or CONTROL-N.


You can move the cursor to the beginning of the buffer with META-< or to the end of the buffer with META->.

Erasing Text

Delete versus kill

When you erase text you can discard it or move it into a holding area and optionally bring it back later. The term delete means permanently discard, and the term kill means move to a holding area. The holding area, called the Kill Ring, can hold several pieces of killed text. You can use the text in the Kill Ring in many ways (refer to "Cut and Paste: Yanking Killed Text" on page 218).

The META-d command kills from the cursor forward to the end of the current word. CONTROL-K kills forward to the end of the current line. It does not delete the line-ending LINEFEED character unless Point and the cursor are just to the left of the LINEFEED. This setup allows you to reach the left end of a line with CONTROL-A, kill the whole line with CONTROL-K, and then immediately type a replacement line without having to reopen a hole for the new line. Another consequence is that, from the beginning of the line, it takes CONTROL-K CONTROL-K (or CONTROL-U 2 CONTROL-K) to kill the text and close the hole.


The emacs editor has several types of search commands. You can search in the following ways:

  • Incrementally for a character string

  • Incrementally for a regular expression (possible but uncommon)

  • For a complete character string

  • For a complete regular expression (Appendix A)

You can run each of the four types of searches either forward or backward in the buffer.

The complete searches behave in the same manner as a search on other editors. Searching begins only when the search string is complete. In contrast, an incremental search begins when you type the first character of the search string and keeps going as you enter additional characters. Initially this approach may sound confusing, but it is surprisingly useful.

Incremental Searches


A single command selects the direction of and starts an incremental search. CONTROL-S starts a forward incremental search, and CONTROL-R starts a reverse incremental search.

When you start an incremental search, emacs prompts you with I-search: in the Echo Area. When you enter a character, it immediately searches for that character in the buffer. If it finds that character, emacs moves Point and cursor to that position so you can see the search progress. If the search fails, emacs tells you so.

After you enter each character of the search string, you can take one of several actions depending on the result of the search to that point.

  • The search finds the string you are looking for in the buffer, leaving the cursor positioned just to its right. Stop the search and leave the cursor in its new position by pressing RETURN. (Any emacs command not related to searching will also stop the search but remembering exactly which ones apply can be difficult. For a new user, RETURN is safer.)

  • The search finds a string but it is not the one you are looking for. You can refine the search string by adding another letter, press CONTROL-R or CONTROL-S again to look for the next occurrence of this search string, or press RETURN to stop the search and leave the cursor where it is.

  • The search hits the beginning or end of the buffer and reports Failing I-Search. You can proceed in several ways at this point.

    • If you mistyped the search string, press BACKSPACE as needed to remove characters from the search string. The text and cursor in the window jump backward in step with your removal of characters.

    • If you want to wrap past the beginning or end of the buffer and continue searching, you can force a wrap by pressing CONTROL-R or CONTROL-S again.

    • If the search has not found the string you are looking for but you want to leave the cursor at its current position, press RETURN to stop the search.

    • If the search has gone wrong and you just want to get back to where you started, press CONTROL-G (the quit character). From an unsuccessful search a single CONTROL-G backs out all the characters in the search string that could not be found. If this action returns you to a place you wish to continue searching from, you can add characters to the search string again. If you do not want to continue the search from that position, a second CONTROL-G stops the search and leaves the cursor where it was initially.

Nonincremental Searches


If you prefer that your searches succeed or fail without showing all the intermediate results, you can give the nonincremental command CONTROL-S RETURN to search forward or CONTROL-R RETURN to search backward. Searching does not begin until you enter a search string in response to the emacs prompt and press RETURN again. Neither of these commands wraps past the end of the buffer.

Regular Expression Searches

You can perform both incremental and nonincremental regular expression searching in emacs. Use the commands listed in Table 7-2 to begin a regular expression search.

Table 7-2. Searching for regular expressions




Incrementally searches forward for a regular expression; prompts for a regular expression one character at a time

META-x isearch-backward-regexp

Incrementally searches backward for a regular expression; prompts for a regular expression one character at a time

META-x isearch-complete RETURN

Prompts for and then searches forward for a complete regular expression

META-x isearch-backward-regexp RETURN

Prompts for and then searches backward for a complete regular expression

A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: