Project31.Edit with emacs


Project 31. Edit with emacs

"What do I do once I've outgrown the nano text editor?"

This project introduces the emacs text editor, a full-featured text editor that'll do all you want and more, with an extensive interactive help system and tutorials to help you get started. Projects 32 through 35 cover the vim text editor, and Project 30 covers the simpler nano text editor.

Basics

Fire up emacs, and we'll take a look at what it can do. You should find a file called index.html in the directory Sites in your home directory. We'll work on a copy of it, which we'll call index-new.html, but you can use any other text file instead.

$ cd ~/Sites $ cp index.html index-new.html $ emacs index-new.html


You'll see a terminal window similar to that shown in Figure 4.2. The top line shows a menu bar. The bottom line is the minibuffer, in which you type command arguments such as the name of a file to load. The line above that, with white text on a black background, is the mode line; it shows status and important and informational messages such as the name of the file you are editing.

Figure 4.2. The emacs text editor provides more tools and greater sophistication than the simpler nano editor.


Commands in emacs are introduced with either the Control key or the Escape key (termed the meta key). Control sequences are shown in the emacs documentation as, for example, C-a, which means press Control-a. Meta-key sequences are shown as, for example, M-a, which means press Escape and then the letter a. (Don't hold the Escape key down, as you would the Control key.)

Move Around

Use the cursor (arrow) keys in the normal manner to select an editing position in a file. To position the cursor by clicking, you must first enable Terminal's Option-Click to Position Cursor feature. Launch the Terminal Inspector by choosing Terminal > Window Settings; select Emulation from the pop-up menu, and check Option-Click to Position Cursor. (If you did this already to enable mouse clicks in nano, you don't need to do it again for emacs.) Option-clicking tells Terminal to send a series of cursor-movement keystrokes to emacs, thereby moving the cursor to the point where you clicked.

Other useful sequences are

  • Control-a to move to the start of the line

  • Control-e to move to the end of the line

  • Escape a to move to the start of the sentence

  • Escape e to move to the end of the sentence

  • Escape < to move to the start of the document

  • Escape > to move to the end of the document

  • Control-l (letter ell) to center the document on the current cursor position

Tip

The Page Up and Page Down keys scroll Apple's Terminal. Use Shift-Page Up and Shift-Page Down to get emacs to scroll the document.

If you use X11's xterm as your terminal, the Page Up and Page Down work without the Shift key.


Note that control sequences move the cursor by characters and lines, whereas the corresponding escape sequences move the cursor by syntactic elements such as words and sentences.

Tip

The cursor keys have control-sequence equivalents. Try Control- and p, n, f, b. Also, Control-v is equivalent to Page Down, and Escape v is equivalent to Page Up.


Get Help

Press Control-h to enter the help system and then

  • ? to display the help screen

  • t to enter the tutorial

  • k and then a Control or Escape sequence on which to get help

If you enter the help system while editing another file, return to the file by typing Control-x b. See "Multiple Files (Buffers) and Windows" later in this project.

Type Control-x Control-c to exit emacs.

Tip

To undo changes, press Control-_ (underscore) or type Control-x u.


Edit File Contents

Inserting and removing text is straightforward in emacs. To add text, place the cursor at the location where you want your addition to begin. The Delete key (also referred to as the Backspace key) deletes the character behind the cursor. Forward-Delete (the key above the left arrow on full-size Mac keyboards, emulated by pressing Fn-Delete on laptops) removes the character under the cursor.

Other useful delete sequences are

  • Control-k to delete (kill) from the cursor to the end of the current line

  • Escape k to delete (kill) from the cursor to the end of the current paragraph

Cut and Paste

Cut text: In emacs lingo, you don't cut text, you kill it. Press Control-@ (or Control-space, if that key combination is not already used as a hot key elsewhere), to mark the start of a section to be killed. Move the cursor to just after the end of the section to be killed, and press Control-w. This kills (cuts) the text and saves it in a buffer.

Paste text: The emacs term for "paste" is yank. Position the cursor where you want some previously cut text to be inserted, and press Control-y. Any operation that kills text also makes that text available to be yanked back with Control-y. This applies to the deletion sequences described in "Edit File Contents" earlier in this project.

As with nano, if you use Apple's Terminal application for Unix sessions (instead of the X11 xterm), you can also copy text by pressing Command-c and paste it by pressing Command-v. It is not possible to cut text by using Command-x.

Tip

When you do several kills, each kill is saved in a separate buffer. Yanking pastes the most recent kill. When you follow Control-y with Escape y, previous kills are recalled, overwriting the text that was just pasted. Type Escape y several times to choose which kill to paste.


Save Your Edits

To save changes to the current file, type Control-x Control-s. You'll see confirmation of the save on the bottom line. The first time the changes are saved, the original file is renamed to end with a tilde (~). If you later decide you want the original file back, it'll be there ready and waiting for you.

To discard changes, type Control-x Control-c. You'll see a prompt on the bottom line, to which you should answer "no" by typing n; then answer yes to the next prompt to confirm that you do indeed intend to discard all changes made to the current document.

Official emacs Information

The official home site for emacs is www.gnu.org/software/emacs. It provides lots of useful information, including a comprehensive manual.


Search and Replace

emacs has search, and search-and-replace, capabilities. In addition, you may search in a case-sensitive manner, employ regular-expression matching, and search in the reverse direction.

To search for a term such as web, press Control-s. In the minibuffer at the foot of the window you'll see the prompt I-search:. Type the search term, and if you type slowly, you'll notice the search happening as you type with all matches highlighted. As you type more letters, the search becomes more selective. (Known as an incremental search, this method will be familiar to users of Mac OS X 10.4's Spotlight search tool.)

If many matches are found, press Control-s repeatedly until the desired match is highlighted. If you overshoot, press the Delete (Backspace) key to return to the previous match. When you have found what you are looking for (which is more than U2 ever did), press Return to end the search.

To repeat the last search, type Control-s Control-s and then press Return. To search backward, use Control-r instead of Control-s.

To perform a nonincremental search, press Control-s and then Return. The prompt in the minibuffer changes from I-search: to Search:. Continue as before, typing the search term and then pressing Return.

If the search term you enter is in all lowercase, the search is made in a case-insensitive manner, so hello matches hello and HellO. Entering an uppercase character anywhere in the search term makes the search case sensitive, so Hello matches Hello but not hello or HellO.

Replace

To perform a search and replace, type Escape x replace-string and then press Return (type literally replace-string). At the minibuffer prompt Replace string:, type the string to be replaced and press Return. The prompt will change to Replace string with:. Type the new string and press Return. All occurrences of the search string will be replaced by the new string.

To perform an interactive search and replace, in which you get to choose which matches are replaced and which are not, type query-replace instead of replace-string. At each match, emacs will prompt for confirmation. Reply with one of the following:

  • y for yes, to replace the string

  • n for no, to not replace the string

  • q for quit, to quit without replacing any more strings

  • ! for replace, to replace all occurrences without further prompting

Use Regular Expressions

emacs is capable of searching for, and replacing, regular expressions. To search for a regular expression, prefix a normal search (as described above) with Control-u and then continue by pressing Control-s as usual. To perform a search and replace when matching a regular expression, proceed as for a standard search and replace, but type replace-regexp instead of replace-string.

Learn More

See projects 77 and 78 to learn more about regular expressions.


More

There is much more to searching and replacing in emacs than is presented here. Refer to the online documentation (see the sidebar "Official emacs Information") for full details.

Multiple Files (Buffers) and Windows

emacs lets you open many files in a single session. Each open file is held in a separate buffer, and emacs displays one buffer at a time. To load a second file, type Control-x Control-f. The minibuffer on the bottom line will show the prompt Find file: followed by the name of the current working directory. Type a name for the new file in the minibuffer, and press Return. The old file will vanish, and the new file will be presented in the terminal window. The old file is still loaded in emacs and can be recalled for editing later.

To switch between open files (or buffers) to continue editing a previous file, type Control-x b. At the prompt, type the name of the file to edit and press Return. You may notice that emacs shows a default filename in the prompt; if this is the one you want, you need only press Return.

If you wish to create a new file, type Control-x Control-f. The minibuffer on the bottom line will show the prompt Find file: followed by the name of the current working directory. Type a name for the new file in the minibuffer and press Return.

Type Control-x Control-b to display a list of buffers. This operation opens a second window. To close it, see the next section, "Open Multiple Windows."

To save or discard all modified buffers interactively, type Control-x s. For each buffer, emacs will ask for confirmation. Reply with one of the following:

  • y for yes, to save the buffer

  • n for no, to skip the buffer

  • q for quit, to skip all remaining buffers

  • ! for save all, to save all remaining buffers without further prompting

Tip

Use the Cursor Up and Cursor Down keys to recall filenames you've typed previously.


Tip

Split a window vertically by typing Control-x 3.


Open Multiple Windows

To open a second window, type Control-x 2. The new window will initially contain a copy of the current buffer, but you can read another file into it by typing Control-x Control-f, as described above. To flip between windows, type Control-x o.

If you have two windows open and wish to close the other window, type Control-x 1. You may open a third window by typing Control-x 2.

Use Tabbed Completion

When entering a filename in the minibuffer, use tabbed completion just as you would on the command line. Start typing a filename; then press Tab. If what you have typed so far is unambiguous, emacs will complete the filename; you need only press Return to load that file. If what you have typed is ambiguous, press Tab a second time to reveal a list of possible completions in a new window. Continue typing until what you have typed is unambiguous; then press Tab again to complete the filename and press Return to load the file.

Alternatively, when the completions are showing, type Escape v or Page Up (Shift-Page Up in Apple's Terminal) to move the cursor into the completions buffer. From here, use the cursor keys to make a selection from the list of possible completions. Press Return to load the selected filename. Select a directory and press Return to enter the directory, listing its contents in the completions buffer. To move to the parent directory, select the .. (dot-dot) entry and press Return.

Repetition

Most commands can be repeated any number of times with Control-u. To move the cursor down 30 lines, for example, type Control-u 30 and then the command or keystroke to repeatin this case, the Cursor Down key. To insert a line of 80 stars, type Control-u 80 *.

To kill the next 20 lines, type Control-u 20 Control-k. This operation is actually slightly different from pressing Control-k 20 times, as the first Control-k of a pair kills the contents of the line, and the second kills the line itself, so only 10 lines will be killed. Try it. All the killed lines are added to the yank buffer, so pressing Control-y will reinsert all the lines at the current cursor position.

Tip

You may use filename-completion tricks when switching buffers with Control-x b. Press Tab to complete a buffer name, and press Tab twice to open the completions buffer. You may move the cursor to the completions buffer to select a buffer.


Much More

The emacs text editor has many, many more features. This project presents enough for you to start using emacs for your day-to-day editing needs. Refer to the help system, or the tutorial, for more information. Alternatively, visit the emacs home page listed in the sidebar "Official emacs Information."

Try one of the following:

  • Escape x life

  • Escape x dunnet

  • Escape x teTRis

  • Escape x pong

When you get bored, type Escape x quit.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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