|< Day Day Up >|
Tutorial: Getting Started with emacs
The emacs editor has many, many features, and there are many ways to use it. Its complete manual includes more than 35 chapters. Nevertheless, you can do a considerable amount of meaningful work with a relatively small subset of the commands. This section describes a simple editing session, explaining how to start and exit from emacs and how to move the cursor and delete text. Some issues are postponed or simplified in the interest of clarity.
To edit a file named sample using emacs as a text-based editor, enter the following command. The nw option, which must be the first option on the emacs command line, tells emacs not to use its X (GUI) interface.
$ emacs -nw -q sample
This command starts emacs, reads the file named sample into a buffer, and displays its contents on the screen or window. If no file has this name, emacs displays a blank screen with New File at the bottom (Figure 7-1). If the file exists, emacs displays another message (Figure 7-2, page 200). The q option tells emacs not to read the ~/.emacs startup file from your home directory. Not reading the startup file guarantees that you get standard, uncustomized behavior and is sometimes useful for beginners or for other users who want to bypass a .emacs file.
Figure 7-1. The emacs welcome screen
Figure 7-2. Sample buffer
The screen starts with a single window. At the bottom of this window is a reverse-video titlebar called the Mode Line. At a minimum, the Mode Line shows which buffer the window is viewing, whether the buffer has been changed, what major and minor modes are in effect, and how far down the buffer the window is positioned. When you have more than one window, one Mode Line appears in each window. At the bottom of the screen, emacs leaves a single line open. This Echo Area, or Minibuffer, line is used for short messages and special one-line commands.
A cursor is in the window or Minibuffer. All input and nearly all editing takes place at the cursor. As you type ordinary characters, emacs inserts them at the cursor position. If characters are under the cursor or to its right, they get pushed to the right as you type, so no characters are lost.
The command to exit from emacs is the two-key sequence CONTROL-X CONTROL-C. You can give this command at almost any time (in some modes you may have to press CONTROL-G first). It stops emacs gracefully, asking you to confirm changes if you made any during the editing session.
If you want to cancel a half-typed command or stop a running command before it is done, you can quit by pressing CONTROL-G. The emacs editor displays Quit in the Echo Area and waits for your next command.
Typing an ordinary (printing) character pushes the cursor and any characters to the right of the cursor one position to the right and inserts the new character in the position just opened. Backspacing pulls the cursor and any characters to the right of the cursor one position to the left, erasing the character that was there before.
Depending on the keyboard you are using and the emacs startup file, different keys may delete characters in different ways. CONTROL-D typically deletes the character under the cursor, as do DELETE and DEL. BACKSPACE typically deletes the character to the left of the cursor. Try each of these keys and see what it does.
tip: More about deleting characters
If the instructions described in this section do not work, read the emacs info section on deletion. Give this command from a shell prompt:
$ info emacs
From info give the command m deletion to display a document that describes in detail how to delete small amounts of text. Use the SPACE bar to scroll through the document. Type q to exit from info.
Start emacs and type a few lines of text. If you make a mistake, use the deletion characters discussed previously. The RETURN key inserts an invisible end-of-line character in the buffer and returns the cursor to the left margin, one line down. It is possible to back up past the start of a line and up to the end of the previous line. Figure 7-2 shows a sample buffer.
Moving the Cursor
You can position the cursor over any character in the emacs window and move the window so it displays any portion of the buffer. You can move the cursor forward or backward through the text (Figure 6-8, page 155) by various textual units for example, characters, words, sentences, lines, and paragraphs. Any of the cursor-movement commands can be preceded by a repetition count (CONTROL-U followed by a numeric argument), which causes the cursor to move that number of textual units through the text. Refer to page 205 for a discussion of numeric arguments.
tip: Use the ARROW keys
Sometimes the easiest way to move the cursor is by using the LEFT, RIGHT, UP, and DOWN ARROW keys.
Moving the Cursor by Characters
Pressing the RIGHT ARROW key or CONTROL-F moves the cursor forward one character. If the cursor is at the end of a line, these commands wrap it to the beginning of the next line. The command CONTROL-U 7 CONTROL-F moves the cursor seven characters forward (to the right).
Pressing the LEFT ARROW key or CONTROL-B moves the cursor backward one character. The command CONTROL-U 7 CONTROL-B moves the cursor seven characters backward (to the left). CONTROL-B works in a manner similar to CONTROL-F (Figure 7-3).
Figure 7-3. Moving the cursor by characters
Moving the Cursor by Words
Pressing META-f moves the cursor forward one word. To press META-f hold down the META or ALT key while you press f . If you do not have either of these keys, press ESCAPE, release it, and then press f. This command leaves the cursor on the first character that is not part of the word the cursor started on. The command CONTROL-U 4 META-f moves the cursor forward one space past the end of the fourth word. See page 204 for more about keys.
Pressing META-b moves the cursor backward one word so the cursor is on the first letter of the word it started on. If the cursor was on the first letter of a word, META-b moves the cursor to the first letter of the preceding word. It works in a manner similar to META-f (Figure 7-4).
Figure 7-4. Moving the cursor by words
Moving the Cursor by Lines
|< Day Day Up >|