Now that you know how to enter and exit Emacs as well as the basics of working with files, it's time to learn how to move around in and edit files. Emacs offers lots of ways to move around in files. At first, you might find it confusing that there are so many ways to do the same thing. Be patient—as you learn, the confusion will lessen, and you'll begin to appreciate the variety of Emacs commands. The more ways you learn, the fewer keystrokes you'll need to get to the part of the file you want to edit.
If you want to practice commands while you're reading—which will help you learn faster—start by typing a page or two from anything you happen to have handy; the newspaper is fine. That will give you some text to work with as you learn the editing skills described in this chapter. Don't worry if you make mistakes; just keep on typing. You can correct any mistakes after you learn the basic editing skills outlined here. Learning any editor is primarily a matter of forming certain finger habits rather than memorizing what the book says. You will learn the right finger habits only if you start typing.
When you are typing and you get to the right side of the display, you have two options. You can press Enter to go to the next line, or you can keep typing. If you type a long line and don't press Enter, Emacs waits until you reach the end of the display. Then it puts a curved arrow at the end of the line and one at the beginning of the next line as a visual indication that the next line is a continuation of the previous line (see Figure 2-1). If Emacs is run in a nongraphical environment, a backslash (\) is used instead.
Figure 2-1. Graphical versions of Emacs use curved arrows to indicate that a line is continued; terminal versions use backslashes
Refill mode is a minor mode that
keeps
paragraphs neat as you edit them. It is not on by default. Look at
the mode line. If the word Refill
appears, you are
in refill mode already. If not, you can turn it on for this buffer
only by typing M-x refill-mode
Enter. If you decide that you don't like
refill mode, type M-x refill-mode
Enter again. This command is like a light switch: it
toggles refill mode on and off.
You may decide that you want to enter refill mode automatically whenever you edit. We'll describe how to do so at the end of this chapter.
In some contexts, refill mode can be annoying, and it is still a work
in progress according to the Emacs manual. You
m
ay
prefer auto-fill mode. You enter it in the same way; type M-x auto-fill-mode Enter. The word
Fill
appears on the mode line.
When you type paragraphs, auto-fill mode formats them. When you edit them, however, auto-fill mode does not automatically reformat them. You do that yourself using the fill-paragraph command, M-q.
If you turn on refill mode and then decide to use auto-fill mode, you still have to turn refill mode off explicitly by typing M-x refill-mode Enter. Otherwise, both modes appear on the mode line, and refill mode continues its merry automatic reformatting of paragraphs, ignoring the fact that auto-fill mode has been enabled.
Watch out for one important pitfall when reformatting paragraphs. In text mode, a paragraph is any text that is indented or has a blank line before and after it. If you have a file with no blank lines, Emacs thinks it is all one long paragraph. Typing M-q takes all the text, ignoring line breaks, and makes it one long paragraph. This command is a particular problem if you have a data file, a program, or if you just prefer to write files with no blank lines. Luckily, pressing C-_ or C-x u (both for undo) magically puts things back the way they were. If you regularly create files with no blank lines, here are some suggestions:
Instead of writing
in text mode, use paragraph indent
text mode. In this mode, a line that starts with any blank space is a
new paragraph. Type M-x
paragraph-indent-text-mode to start this mode;
you'll see Parindent
on the mode
line. See Chapter 6 for more details.
Use a specific mode rather than text mode for writing. For example, use HTML mode or LaTeX mode, described in Chapter 8, for editing files of these types. These special modes redefine what a paragraph means so that the fill-paragraph command works correctly. Otherwise, these modes are very similar to text mode.
Instead of filling a paragraph, fill a marked section of text called a region (we'll discuss regions later in this chapter). Define the region you want to fill and press M-x fill-region Enter. This command takes a region and formats each individual paragraph within it.
Table 2-1 lists commands for filling text automatically and reformatting paragraphs with auto-fill mode.
Table 2-1. Text filling and reformatting commands
Keystrokes |
Command name |
Action |
---|---|---|
(none) [1] |
refill-mode |
Toggle refill mode, in which Emacs automatically reformats text. |
(none)Options → Word Wrap in Text Modes |
auto-fill-mode |
Toggle auto-fill mode, in which Emacs formats paragraphs as you type them. |
M-q |
fill-paragraph |
Reformat paragraph. |
(none)Edit → Fill |
fill-region |
Reformat individual paragraphs within a region. |
[1] Remember that (none) in the first column means that you type M-x followed by the command name in the second column, then press Enter to run the command. There are no default keystrokes. To use the refill-mode command, type M-x refill-mode Enter. |
The easiest way to move the cursor is to click the left button on your mouse or to press the arrow keys. However, it's a hassle to reach for a mouse all the time. Learn to use keyboard commands to move around so that you will ultimately achieve blinding speed and maximum productivity in Emacs.
To use Emacs commands to move the cursor forward one space, type C-f (f for "forward"). As you might guess, C-b moves the cursor backward. To move up, type C-p (for previous-line), and to move down, type C-n (for next-line). It's easier to memorize commands if you remember what the letters stand for.
Figure 2-2 illustrates how to move up, down, left, and right using Emacs commands.
If you're at the end of a line, C-f moves to the first character on the next
line. Likewise, if you're at the beginning of a
line, C-b moves to the last
character of the previous line. If there's no place
to go, Emacs beeps and displays the message Beginning of
buffer
or End of buffer
.
Now we'll learn some more advanced ways to move the cursor. One common way is moving forward and backward by word: M-f moves forward a word; M-b moves backward a word. You can also move to the beginning or end of the line. C-a moves you to the beginning of the line (just like a is the beginning of the alphabet). C-e moves you to the end of the line. To move backward one sentence, type M-a; to move forward one sentence, type M-e. To move forward a whole paragraph at a time, type M-}; to move backward a paragraph, type M-{. If you're in the middle of a sentence or paragraph, moving back a sentence or paragraph actually takes you to the beginning of the current sentence or paragraph.
Figure 2-3 uses a few paragraphs of Victor Hugo's Les Misérables to show how you can move the cursor more than one character at a time.
You may have picked up on a pattern here. Notice the difference between commands starting with Ctrl and those starting with Meta. Ctrl commands generally move in smaller units than their associated Meta commands. For example, C-b moves the cursor backward one character, whereas M-b moves the cursor back one word. Likewise, C-a moves to the beginning of the line, whereas M-a moves to the beginning of a sentence.
There's one caveat about moving by sentence or paragraph. Emacs defines a sentence pretty strictly. You need two spaces after the final punctuation mark, unless you're at the end of the line. If there's only one space, Emacs won't recognize it. Similarly, moving backward and forward by paragraph involves understanding the Emacs definition of a paragraph. To Emacs (and to most of us), paragraphs are either indented with a tab or at least one space or have blank lines between them (block style). You can change these definitions, but first you have to understand how to use regular expressions, which are discussed briefly in Chapter 3 and in more depth in Chapter 11. Chapter 10 discusses how to change variables.
If your file has page breaks in it, you can move to the next page or previous page by typing C-x ] (forward-page) or C-x [ (backward-page). Similar to paragraph and sentence movement, moving by page involves the Emacs definition of what a page is. A variable called page-delimiter defines what constitutes a page break. If there are no Emacs-recognized page breaks in the file, Emacs regards the buffer as one very long page. In this case, the forward-page command takes you to the end of the buffer, and the backward-page command takes you to the beginning of the buffer.
In text mode, a page break is a formfeed character that tells the printer to move to the next page (to feed the next form or page through the printer, hence the term formfeed) before continuing to print. If you are in text mode and you want to insert page breaks in your file, type C-q C-l (the lowercase letter L). C-q is the quoted-insert command. It tells Emacs to put a C-l control character in your file, rather than interpreting C-l as the recenter command. A C-l character looks like two characters (^L), but it's really only one. (Try to erase one using Del and see what we mean.)
Like other graphical applications, you can use the scrollbar to move around in Emacs. Like most things in Emacs, in addition to using the mouse or scrollbar to move around, you should learn Emacs's own keyboard commands to maximize your productivity.
If you want to page through a file one screen at a time, use the PgDown key or type C-v. Emacs displays the next full screen from your file. It leaves a couple of lines from the previous screen at the top to give you a sense of context. Likewise, pressing M-v (or the PgUp key) shows you the previous screen. Together, M-v and C-v provide a convenient way to scroll through a file quickly.
Scrolling happens automatically if you type any motion command that takes you beyond the limits of the text currently displayed. For example, if you are on the last line of the screen and press C-n, Emacs scrolls forward. Similarly, if you are at the top of the screen and press C-p, Emacs scrolls backward.
You often want to move all the way to the beginning or the end of a file. Type M-> or press End to go to the end of a buffer. To go to the beginning, type M-< or press Home. It may help you to remember that > points to the end of the buffer, and < points to the beginning of the buffer.
There are two more ways to move around that may come in handy. M-x goto-line Enter n Enter moves the cursor to line n of the file. Of course, Emacs starts counting lines from the beginning of the file. Likewise, M-x goto-char Enter n Enter goes to the nth character of the file, counting from the beginning. In both cases, n is a number.
For programmers, these commands are useful because many compilers
give error messages like Syntax error on line 356
.
By using these commands, you can move easily to the location of your
error. There are some more sophisticated ways to link Emacs with
error reports from compilers and other programs. In addition, several
other cursor motion commands are applicable only when you are editing
programs (see Chapter 9 for details).
Now let's learn some efficiency tricks. Emacs lets you repeat any command as many times as you want to. First, you can repeat a command any number of times by pressing M- n before the command, where n is the number of times you want to repeat it. This command is called the digit-argument command.
You can give M- n a large argument if you want it to repeat the command many times. For example, let's say you are editing a large file of 1000 lines. If you typed M-500 C-n, the cursor would move down 500 lines, to the halfway point in the file. If you give M- n a larger argument than it can execute, it repeats the command as many times as possible and then stops.
There's another multiplier command you can use, too: C-u (the universal-argument command). You can give C-u an argument just like you do M- n. Typing either M-5 or C-u 5 repeats the command that follows five times. But unlike M- n, C-u doesn't need an argument to repeat commands. With no argument, C-u executes the next command four times. If you type C-u C-u, it executes the command 16 times. In this way, you can stack up C-u's to make commands execute many times: 16, 64, 256, and so on.[2]
C-l, the recenter command, puts the current line in the center of the window vertically. This feature is useful if you're typing at the bottom or the top of the display. Typing C-l quickly moves the material that you care about to the middle of the display, where it is easier to see the full context.
C-l also redraws the display, if for any reason it appears obscured or contains random characters. This doesn't happen as often as it used to when we used terminals, but it can be a handy thing to know about, especially if you find yourself using Emacs remotely in a terminal interface.
Table 2-2 lists cursor movement commands. If the command is mnemonic, the word to remember is given in italics.
Table 2-2. Cursor movement commands
Keystrokes |
Command name |
Action |
---|---|---|
C-f |
forward-char |
Move forward one character (right). |
C-b |
backward-char |
Move backward one character (left). |
C-p |
previous-line |
Move to previous line (up). |
C-n |
next-line |
Move to next line (down). |
M-f |
forward-word |
Move one word forward. |
M-b |
backward-word |
Move one word backward. |
C-a |
beginning-of-line |
Move to beginning of line. |
C-e |
end-of-line |
Move to end of line. |
M-e |
forward-sentence |
Move forward one sentence. |
M-a |
backward-sentence |
Move backward one sentence. |
M-} |
forward-paragraph |
Move forward one paragraph. |
M-{ |
backward-paragraph |
Move backward one paragraph. |
C-v |
scroll-up |
Move forward one screen. |
M-v |
scroll-down |
Move backward one screen. |
C-x ] |
forward-page |
Move forward one page. |
C-x [ |
backward-page |
Move backward one page. |
M-< |
beginning-of-buffer |
Move to beginning of file. |
M-> |
end-of-buffer |
Move to end of file. |
(none) |
goto-line |
Go to line n of file. |
(none) |
goto-char |
Go to character n of file. |
C-l |
recenter |
Redraw screen with current line in the center. |
M- n |
digit-argument |
Repeat the next command n times. |
C-u n |
universal-argument |
Repeat the next command n times (four times if you omit n). |
You can access many Emacs commands by pressing standard keys on your keyboard, such as PageDown (to scroll down one screen) or Home (to go to the beginning of a buffer). Figure 2-4 shows a sample keyboard layout and what the keys do. Your keys may be in a slightly different place, but if you have a key with the same or a similar name, it should work. We say "should" because there are situations in which the keys won't work—for example, if you use Emacs on a remote machine. We recommend that you also learn the standard Emacs commands; they work on any keyboard, and they are often easier to reach once you learn them.
[2] Most often, you'll use C-u as we've described here. However, it doesn't always work as a multiplier; sometimes C-u modifies the command's function. Later in this chapter, you'll see one such case. However, if you're doing something where a multiplier makes sense, C-u is almost certain to work.