2.1. Moving the Cursor


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.

Figure 2-2. Basic cursor motion


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.

2.1.1 Other Ways to Move the Cursor

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.

Figure 2-3. Moving 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.)

2.1.2 Moving a Screen (or More) at a Time

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).

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.[2]

[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.

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).


2.1.5 Emacs Commands and Your Keyboard

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.

Figure 2-4. Emacs commands and your keyboard




Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net