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.
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.
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.
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:
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
CONTROL-V META-v CONTROL-L
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->.
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:
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.
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.
CONTROL-S RETURN CONTROL-R RETURN
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.