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).
2.1.3 Repeating Commands
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.[11]
2.1.4 Centering the Display
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). |
11
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.