• 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-paragraphcommand 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-1lists 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 ) [10] |
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. |
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( ffor "forward"). As you might guess, C-bmoves 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-2illustrates 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-fmoves to the first character on the next line. Likewise, if you're at the beginning of a line, C-bmoves 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-fmoves forward a word; M-bmoves backward a word. You can also move to the beginning or end of the line. C-amoves you to the beginning of the line (just like a is the beginning of the alphabet). C-emoves 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-3uses 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 Ctrland those starting with Meta. Ctrlcommands generally move in smaller units than their associated Metacommands. For example, C-bmoves the cursor backward one character, whereas M-bmoves the cursor back one word. Likewise, C-amoves to the beginning of the line, whereas M-amoves 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 Chapter 3. Search and Replace The commands we discussed in the first two chapters are enough to get you started, but they're certainly not enough to do any serious editing. If you're using Emacs for anything longer than a few paragraphs, you'll want the support this chapter describes. In this chapter, we cover the various ways that Emacs lets you search for and replace text. Emacs provides the traditional search and replace facilities you would expect in any editor; it also provides several important variants, including incremental searches, regular expression searches, and query-replace. We also cover spell-checking here, because it is a type of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a type of automatic replacement that can be a real timesaver.
and in more depth in Chapter 11. Chapter 10discusses 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-delimiterdefines 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-pagecommand takes you to the end of the buffer, and the backward-pagecommand 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-qis the quoted-insertcommand. It tells Emacs to put a C-lcontrol character in your file, rather than interpreting C-las the recentercommand. A C-lcharacter looks like two characters (^L), but it's really only one. (Try to erase one using Deland 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 PgDownkey 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 PgUpkey) shows you the previous screen. Together, M-vand C-vprovide a convenient way to scroll through a file quickly.
Читать дальше