Section 3.37. Editing a File: emacs


[Page 93 (continued)]

3.37. Editing a File: emacs

Emacs (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 emacs

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

Figure 3-67. Example of starting emacs.

[View full width]

GNU Emacs 21.3.2 (i386-mandrake-linux-gnu, X toolkit) of 2004-02-12 on ke.mandrakesoft.com, modified by Mandrake Copyright (C) 2001 Free Software Foundation, Inc. --- Emacs: *scratch* (Fundamental) --L1- All ----------



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:

  • If the first three dashes contain a **, this means that the current file has been modified.

  • The name that follows "Emacs:" is the name of the current file. If no file is currently loaded, the name *scratch* is used instead.


  • [Page 94]
  • The current editing mode is then shown between parentheses. In this case, it's Fundamental, which is the standard editing mode.

  • L with a number indicates the line number of the line where the cursor is positioned.

  • The next entry indicates your relative position in the file as a percentage. If the file is very small and fits completely on the screen, then All is displayed. If you're at the top or the bottom of a file, then Top or Bot is displayed, respectively.

3.37.2. emacs Commands

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

Figure 3-68. How emacs wraps long lines.

[View full width]

This is a long line that illustrates the way unbroken lines a \ re displayed. This is a much shorter line. -** Emacs: *scratch* (Fundamental) --L2-- All ---------



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 Trouble

Whenever 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:

  • The command sequence Control-G terminates any emacs command, even if it's only partially entered, and returns emacs to the state where it's waiting for a new command.


  • [Page 95]
  • The command sequence Control-X 1 closes all emacs windows except your main file window. This is useful, as several emacs options create a new window to display information, and it's important to know how to close them once you've read their contents.

3.37.4. Getting Help

There 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 emacs

To 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 Modes

emacs 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 Text

To enter text, simply start typing. For example, Figure 3-69 shows a short four-line poem.

Figure 3-69. Entering text in emacs.

[View full width]

There is no need for fear in the night, You know that your Mommy is there, To watch over her babies and hold them tight, When you are in her arms you can feel her sigh all night. -** Emacs: *scratch* (Fundamental) --L4-- All -------



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 Features

The most common emacs editing features can be grouped into the following categories:

  • moving the cursor

  • deleting, pasting, and undoing text

  • searching text

  • search/replace text

  • saving/loading files

  • miscellaneous

These categories are described and illustrated in the subsections that follow, using the sample poem that I entered at the start of this section.


[Page 96]

3.37.9. Moving the Cursor

Figure 3-70 is a table of the common cursor movement commands.

Figure 3-70. Moving the cursor in emacs.

Movement

Key sequence

Up one line

<cursor up> or Control-P (previous)

Down one line

<cursor down> or Control-N (next)

Right one character

<cursor right> or Control-F (forward, wraps around)

Left one character

<cursor left> or Control-B (backward, wraps around)

To start of line

Control-A (a is first letter)

To end of line

Control-E (end)

Back one word

Esc b (back)

Forward one word

Esc f (forward)

Down one screen

Control-V

Up one screen

Esc v

Start of file

Esc <

End of file

Esc >


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 Undoing

Figure 3-71 is a table of the common deletion commands.

Figure 3-71. Delete, paste, and undo in emacs.

Item to delete

Key sequence

Character before cursor

<delete> key

Character after cursor

Control-D

Word before cursor

Esc <delete>

Word after cursor

Esc d

To end of current line

Control-K

Sentence

Esc k



[Page 97]

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.

Figure 3-72. The kill buffer in emacs.

Action

Key sequence

Insert last kill buffer.

Control-Y

Retrieve previous kill.

Esc y

Append next kill.

Esc Control-W

Undo.

Control-X u


3.37.11. Searching

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

Figure 3-73. Searching in emacs.
(This item is displayed on page 98 in the print version)

Action

Key sequence

Search forward for str.

Control-S str

Search backward for str.

Control-R str

Repeat last search forward.

Control-S

Repeat last search backward.

Control-R

Leave search mode.

Esc


3.37.12. Search/Replace

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


[Page 98]

3.37.13. Saving/Loading files

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

Figure 3-74. Saving and loading files in emacs.

Action

Key sequence

Save current work.

Control-X Control-S

Edit another file.

Control-X Control-F

Save work and then quit.

Control-X Control-C


3.37.14. Miscellaneous

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




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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