Edit text in Emacs
Entering and changing text in a buffer as well as navigating through that text are the most important things you do in Emacs, whether you're editing an existing file, creating a new file, or just perusing a file.
In this section, learn the basic key sequences and commands for doing these things: how to enter text in a buffer, how to navigate through the text, and how to do basic edits on that text, such as deleting characters and words.
As you've already seen in the Make a new file from scratch section, entering text in an Emacs buffer is easy -- you just start typing. You can enter any alphanumeric character in a buffer by typing that character.
Emacs is sometimes called a modeless editor, often in comparison with a modal editor such as vi. This means the behavior of the editor and the keystrokes and commands you can type remain constant in your session, as opposed to in an editor like vi, where keystrokes have certain meanings depending on whether you're in command mode or insert mode. Emacs has no such modes; however, it does have a different kind of mode that can change its behavior or extend its capability -- but that's a topic for the next tutorial in this series.
As with plain typing, you need to keep a few things in mind.
In Emacs, an important concept called point denotes where character insertion occurs. It's an imaginary spot in the buffer that's just between the character where the cursor is located and the character immediately preceding it.
Whenever you type text in a buffer, it's inserted at point. By default, all the text on the same line but after point is pushed over to the right to make room for what you insert. Press Enter to move to the next line and press Enter again to insert a blank line.
Try inserting a paragraph of text at the beginning of your practice.b buffer:
- Start Emacs with that buffer:
$ emacs practice.b
- Type a paragraph by pressing Enter at the end of each line, and press Enter again at the end to make a blank space after it:
Tyger! Tyger! burning bright In the forests of the night, What immortal hand or eye Could frame thy fearful symmetry?
Note: This is the first stanza from a poem called The Tyger! by William Blake. The author references several stanzas of this poem throughout the tutorial.
Now your practice.b buffer should look like Figure 5.
Figure 5. Inserting a paragraph
You see how the cursor follows your typing and how point remains the same: Everything you type is inserted before the letter O from the first sentence you typed.
To move point, you can use the arrow keys and, as you'd expect, all the other cursor motion keys work, such as the PgUp, PgDn, Home, and End. But Emacs has its own bindings for moving the cursor and, because you don't have to move your hands away from the home row of the keyboard to use them, you'll find that they're much more useful when you're typing.
As you saw in the Learn to type Meta key combinations section, you can use
C-p to move the cursor up to the previous line; similarly,
C-n moves the cursor down to the next line.
C-f moves forward to the next character and
C-b moves backward to the previous character.
To move up and down by a screenful without using the PgDn and PgUp keys, use the
M-v keystrokes, which do the same thing. To go to the beginning of the current line, use
C-a; and use
C-e to go to the end of the current line.
Usually, a Meta key is bound to a command that's similar to its corresponding Ctrl key and, for movement, this general rule applies.
When used with Meta instead of Ctrl, the F and B keys move forward and backward by the word instead of by the character, whereas the A and E keys move to the beginning and ending of the current sentence. (In the default configuration, the
M-p keystrokes are not defined.)
Table 3 lists the various movement and navigation keys, giving their function names and descriptions. Try using them to move to the beginning of the buffer, end of the buffer, and places in between.
Table 3. Useful Emacs keystrokes for movement and navigation
||Move point up to the previous line.|
||Move point down to the next line.|
||Move point forward to the next character.|
||Move point backward to the previous character.|
||Move point forward to the next word.|
||Move point backward to the previous word.|
||Scroll the text upward by a screen.|
||Scroll the text downward by a screen.|
||Move point to the beginning of the buffer. (On some versions, this key is defined by default to move to the beginning of the current line.)|
||Move point to the end of the buffer. (On some versions, this key is defined by default to move to the end of the current line.)|
||Move point to the beginning of the line.|
||Move point to the end of the line.|
||Move point to the beginning of the sentence.|
||Move point to the end of the sentence.|
||Move point to the beginning of the paragraph.|
||Move point to the end of the paragraph.|
As you've seen, the text you type in a buffer is inserted at point. But you can overwrite existing text, too. Press the Ins key once; this toggles overwrite mode, which is off by default. Look at the mode line -- you should see Ovwrt to tell you that this mode is now active.
Use the commands for movement as described in Table 3, and move the cursor so that it's on the w in we, and then type an h character.
Try going to the top of the buffer: Type
M-a M-a C-f to move the cursor above the y, and then type an i character. Do the same for the next y by typing
M-f C-f C-f C-f i so that your buffer looks like Figure 6.
Figure 6. Overwriting characters in a buffer
Press Ins again to turn overwrite mode off.
You're not limited to the alphanumeric keys for entering text. You can enter literal control characters, and you can enter characters based on their character code. Do this by performing a quoted insert, which is bound to
C-q; follow it with a literal keypress, such as a Ctrl combination, to enter that key at point. You can also give a character code value, in octal, followed by the Enter key.
Move to the end of the buffer, type
Page break here, and press Enter. Now type a page break, which is a literal Ctrl-l character, followed by an escape character, which has an octal value of 033:
C-q C-l C-q 033 Enter.
It's time to try the Emacs facilities for getting rid of existing text and for undoing (and redoing) what you've already done.
Use either the Backspace or Del key to delete the character to the left of point. Try using both to delete the two control characters you just entered.
To delete the character at point, use
M-d deletes from point forward to the end of the word. You can go backward, too -- both
M-Backspace delete everything from the left of point to the beginning of the word.
Try using these commands to delete the sentence Page break here that you just typed, and then delete the blank line the words were typed on.
Move the cursor to the last line in the file (which should be the sentence "What the hand dare seize the fire?"), and delete it with several taps of
Oops, what if you didn't mean to delete that last sentence? You can get it back by running the
undo function. This is bound to
C-_, which you type by holding Ctrl and using the Shift key to type an underscore. Try it now to get back the last line, tapping it once for each word to come back.
Tap it a few more times so that Page break here comes back.
Now you decide that you regret this last undo -- you really do want Page break here to go away. You can redo your undo by typing
C-g, which cancels any more undoing, and then type
C-_ enough times so that the words are gone again.
C-u is the universal argument command, which you follow with a number and then a command. It runs the given command that number of times.
Try it: Move to the beginning of the second verse, type
In what distant d, and then type
C-u 2 e -- two e characters should be written. Finish the verse by typing:
ps or skies Built the fire of thine eyes?
C-x C-s to save your buffer; it should now look like Figure 7.
Figure 7. Character insertion with the universal argument buffer
With no numeric argument,
C-u assumes a value of 4. If you give
C-u itself as the argument, it multiplies that default 4 by itself to make 16; you can build on this as much as you like before you give a command -- for example,
C-u C-u C-u A types the letter A 64 times at point.
Try moving backward with
C-b once, and see how the cursor wraps back to the line before it. Now try it with the universal argument by typing
C-u C-b; notice that the cursor moves back four characters instead of one. Try it again with
C-u C-u C-b, and see how the cursor moves 16 characters to the left. Type
C-u C-u C-u C-b, and then give it an argument of 1000:
C-u 1000 C-b. The cursor moves to the beginning of the buffer, but notice that Emacs beeps -- that means it reached the top of the buffer (and could move back no further) before it went through all the requested iterations.
Let's recap what you've just learned. Before you advance to the next section, take a glance at Table 4, which lists important editing commands and the default keystrokes to use them.
Table 4. Common Emacs editing commands
||Toggle overwrite mode (default is off).|
||Delete the character before point.|
||Delete the character at point.|
||Delete the characters from point forward to the end of the word.|
||Delete the characters from point backward to the beginning of the word.|
||Undo your last typing or action.|
||Insert, at point, the literal character keypress or the character whose octal value is XXX.|
||Execute command a total of number (default 4) times in succession.|