3.37. Editing a File: emacsEmacs (Editor MACroS) is a popular editor that is found on many UNIX systems and was one of the first contributions of the GNU Project to the world of open source software. Emacs had its start in the Lisp-based Artificial Intelligence community. In 1975, Richard Stallman and Guy Steele wrote the original version. Stallman later rewrote it from scratch, and this version is the one available on Linux and for other platforms from the Free Software Foundation (FSF). 3.37.1. Starting emacsTo start emacs with a blank file, enter the command emacs with no parameters. To edit an existing file, specify its name as a command-line parameter. You can run emacs in a terminal window, or it can also start up in it own window when you're using a graphical desktop on Linux. GNU Emacs also includes a tutorial as part of the program. Assuming that you supply no parameters, your screen will initially look something like Figure 3-67.
I'll draw screens that are only about six lines long to conserve space. The second-from-bottom line is called the mode line, and contains information in the following left-to-right order:
3.37.2. emacs CommandsUnlike vim, emacs doesn't distinguish between text entry mode and command mode. To enter text, simply start typing. The initial emacs welcome banner automatically disappears when you type the first letter. Long lines are not automatically broken, so you must press the Enter key when you wish to start a new line. Lines longer than the screen width are indicated by a \ (backward slash) character at the end of the screen, and the remainder of the line is "wrapped" onto the next line (Figure 3-68).
emacs' editing features are accessed via either a control sequence or a metasequence. I'll indicate control sequences by prepending the name of the key with the prefix "Control-". For example, the sequence Control-H t means "Press and hold the Control key and then press the H key (for control sequences, it doesn't matter whether you use uppercase or lowercase, so I suggest that you use lowercase, as it's easier). Then release both keys and press the t key on its own." Similarly, metasequences use the Esc key. For example, the sequence: Esc x means "Press the Esc key (but don't hold it) and then press the x key." The next few sections contain many examples of emacs command sequences. If you ever accidentally press Esc followed by Esc, emacs warns you that you're trying to do something advanced and suggests that you press the n key to continue. Unless you're a seasoned emacs user, it's good advice. 3.37.3. Getting Out of TroubleWhenever you're learning a new editor, it's quite easy to get lost and confused. Here are a couple of useful command sequences to return you to a sane state:
3.37.4. Getting HelpThere are several ways to obtain help information about emacs. One of the best ways to get started with emacs is to read the self-describing help tutorial. I suggest that you do this before anything else. To read the tutorial, use the command sequence Control-H t. The tutorial will appear and give you directions on how to proceed. 3.37.5. Leaving emacsTo leave emacs and save your file, use Control-X Control-C. If you haven't saved your file since it was last modified, you'll be asked whether you want to save it. 3.37.6. emacs Modesemacs supports several different modes for entering text, including Fundamental, Lisp Interaction, and C. Each mode supports special features that are customized for the particular kind of text that you're editing. emacs starts in Fundamental mode by default, which is the mode that I'll be using during my description of emacs. For more information about modes, consult the emacs tutorial. 3.37.7. Entering TextTo enter text, simply start typing. For example, Figure 3-69 shows a short four-line poem.
The next section describes the editing features of emacs that allowed me to change this poem to something a little better. 3.37.8. Common Editing FeaturesThe most common emacs editing features can be grouped into the following categories:
These categories are described and illustrated in the subsections that follow, using the sample poem that I entered at the start of this section. 3.37.9. Moving the CursorFigure 3-70 is a table of the common cursor movement commands.
For example, to insert the words "worry or" before the word "fear" on the first line, I moved the cursor to the first line of the file by typing Esc < and then moved forward several words by using the Esc f sequence. I then typed in the words, which were automatically inserted at the current cursor position. 3.37.10. Deleting, Pasting, and UndoingFigure 3-71 is a table of the common deletion commands.
Whenever an item is deleted, emacs remembers it in an individual "kill buffer." A list of kill buffers is maintained so that deleted items may be retrieved long after they have been removed from the display. To retrieve the last killed item, use Control-Y. After you have typed Control-Y, you may type Esc y to replace the retrieved item with the previously deleted item. Every time you type Esc y, the retrieved item moves one step back through the kill buffer list. You may append the next deleted item onto the end of the last kill buffer rather than create a new one by typing Esc Control-W immediately prior to the delete command. This is useful if you wish to cut different bits and pieces out of a file and then paste them all together back into one place. You may undo editing actions one at a time by typing Control-X u for each action that you wish to undo. Figure 3-72 is a summary of the kill buffer and undo commands.
3.37.11. Searchingemacs allows you to perform something called an incremental search. To search forward from your current cursor position for a particular sequence of letters, type Control-S. The prompt "I-search:" is displayed on the bottom line of the screen, indicating that emacs wants you to enter the string that you wish to search for. As you enter the character sequence, emacs searches to find the first string from your initial cursor position that matches what you've entered so far; in other words, partial substrings are found as you enter the full string. To terminate the search and leave your cursor at its current position, press Esc. If you delete characters in the full string before pressing the Esc key, emacs moves back to the first match of the remaining substring. To repeat a search, don't press Esc, but instead press Control-S to search forward or Control-R to search backward. Figure 3-73 is a summary of the searching commands.
3.37.12. Search/ReplaceTo perform a global search/replace, type Esc x followed by the string "repl s" followed by Enter. emacs then prompts you for the string to replace. Enter the string and press Enter. emacs then prompts you for the replacement string. Enter the string and press Enter. emacs then performs the global text substitution. 3.37.13. Saving/Loading filesTo save your current work to a file, type Control-X Control-S. If your work hasn't been associated with a filename yet, you are prompted for a filename. Your work is then saved into its associated file. To edit another file, type Control-X Control-F. You are prompted for the new filename. If the file already exists, its contents are loaded into emacs; otherwise, the file is created. To save your file and then quit out of emacs, type Control-X Control-C. Figure 3-74 is a summary of the save/load commands.
3.37.14. MiscellaneousTo redraw the screen, type Control-L. To place emacs into auto-wrap mode, which automatically inserts line breaks when words flow past the end of a line, type Esc x auto-fill-mode and press Enter. To leave this mode, repeat the command again. |