Editing with emacs


emacs is another screen editor that is popular among UNIX users. emacs differs from vi and ed in that it is a single-mode editor-that is, emacs does not have separate input and command modes. In a way, emacs allows you to be in both command and input modes at the same time. Normal alphanumeric characters are taken as text, and control and metacharacters (those preceded by an ESC) are taken as commands to the editor.

Several editors are called emacs. The first emacs was written by Richard Stallman at MIT as a set of editing macros for the teco editor for the ITS System. The second was also written at MIT for the MULTICS System by Bernie Greenberg. A version of emacs was developed by James Gosling at Carnegie Mellon University to run on UNIX Systems. Another version of emacs (with a different user interface) was written by Warren Montgomery of Bell Labs. Stallman’s version has become predominant with the birth of the Free Software Foundation (FSF) and GNU (GNU is Not UNIX). The GNU project’s aim is to provide public domain software tools, distributed without the usual licensing restrictions. GNU Emacs is included with several LINUX distributions, including Red Hat and Slackware. Since GNU Emacs is the most common version of emacs, the examples used in this chapter are based on it. Although different versions of emacs use different keystroke commands, the command sets among different forms of emacs are, for the most part, similar.

emacs is supported as one of the editor options used for command-line editing in the Korn shell. On systems that allow you access to both the emacs and vi features, you can use either as a shell command line editor or as a text editor.

If you are not already a vi or emacs user, you can decide which one you might like to use by trying the ten-minute tutorial for each in this chapter.

Setting Your Terminal Display Type for emacs

As with the vi editor, the first thing you must do if you are planning to use emacs is to specify the type of terminal that you are using or emulating on the PC. You do this by setting a shell environment variable. Refer to the previous section “Setting Your Terminal Display Type for vi.” The three methods for setting your display are identical for emacs.

Remember, unlike ed and vi, emacs is a single-mode editor. As Figure 5–3 shows, in emacs you can enter commands or text at any time.

image from book
Figure 5–3: Emacs commands and input

Each character you type is interpreted as an emacs command. Regular (alphanumeric and symbolic) characters are interpreted as commands to insert the character into the text. Combinations, including nonprinting characters, are interpreted as commands to operate on the file. emacs offers several distinct types of commands. For example, there are commands that use the control characters, such as CTRL-B. CTRL-B will move the cursor left one character-hold the CTRL key down, while simultaneously pressing the B key Some commands use the ESC character as part of the command name. The command ESC-B will move the cursor left one word. Press the ESC key, release it, and then press B.

Some commands are combination commands that begin with CTRL-X. For example, the command CTRL-X CTRL-S saves your work by writing the buffer to the file being edited.

Although the number of control and escape characters is large, there are still many more emacs commands than there are characters. Many of these commands have names but are not bound to (associated with) specific key presses. You invoke these commands by using the ESC-X commandname combination, for instance, ESC-X isearch- complete.

The preceding command invokes the command called isearch-complete, which is not bound to any set of keystrokes. You can make up new associations for key presses and command names to customize emacs to your liking. For example, if you don’t like the fact that the BACKSPACE key invokes help, you can change that. Putting the following lines in your .emacs file makes BACKSPACE move the cursor left one space and CTRL-X-? invoke the help facility:

 (global-set-key "\C-x?" 'help-command) (global-set-key "\C-h" 'backward-char)

Starting emacs

You can begin editing a file in emacs with the command in the form emacs filename. Using the example filename mydog, the command emacs mydog reads in the file mydog and displays a window with several lines, as shown in Figure 5–4.

image from book
Figure 5–4: A sample emacs window

A buffer is associated with each window, and a mode line at the bottom of the window has information about the material being edited. In this example, the name of the buffer is mydog, and the full pathname of the file is /home/rrr/mydog. On some versions of emacs, the mode line will also tell you where you are in the file and what special features of emacs are being used.

Creating Text with emacs

There is no separate input mode in emacs. Because emacs is always in input mode, any normal characters typed will be inserted into the buffer.

Exiting emacs

When you are done entering text, the command CTRL-X CTRL-C will exit from the editor. If you have made changes to the file, you are prompted to decide whether you want the changes saved. If you respond with a y, then emacs saves the file and exits. If you respond with an n, then emacs asks you to confirm by typing yes or no in full.

Moving Within a Window

A screen editor shows you the file you are editing one window at a time. You move the cursor within the window, making changes and additions, and moving the text that is displayed in the window. One set of commands enables you to move by characters or lines:

CTRL-F

Moves forward (right) one character

CTRL-B

Moves back (left) one character

CTRL-N

Moves to the next line (down)

CTRL-P

Moves to the previous line (up)

CTRL-A

Moves to the beginning of the current line

CTRL-E

Moves to the end of the current line

To move in larger units within the window, use the following set of commands:

ESC-F

Moves forward to the end of a word

ESC-B

Moves back to the beginning of the previous word

ESC->

Moves the cursor to after the last character in the buffer

ESC-<

Moves the cursor to before the first character in the buffer

Moving the Window in the Buffer

emacs shows you a file one window at a time. You can move the window within the text file to go back one screen or forward one screen by using the following commands:

CTRL-V

Moves ahead one screen

ESC-V

Moves back one screen

CTRL-L

Redraws the screen with the current line in the center

DeletingText

emacs provides several commands for deleting text:

DELETE

Deletes the previous character

CTRL-D

Deletes the character under the cursor

ESC-DELETE

Deletes the previous word

ESC-D

Deletes the word the cursor is on

CTRL-K

Kills (deletes) the text to the end of the line

CTRL-W

Deletes from the mark to the cursor

CTRL-@

Sets the mark

CTRL-X CTRL-X

Exchanges the position of the cursor and mark

CTRL-Y

Yanks deleted text

emacs Help

emacs has several help facilities. If you issue the command CTRL-H, you’ll be put into the help facility. You can ask for help by typing CTRL-H CTRL-H. This will give you a list of all the help commands in the mini-buffer. If you type CTRL-H t, emacs will run a short tutorial on basic editing. The command CTRL-H i will provide information through the documentation reader, a hypertext-like viewer that enables you to browse emacs info. Typing h will give you a tutorial, and typing CTRL-H will give you help on the info mode.

Another type of help is called apropos, and is invoked with the command CTRL-H a. When you issue this command, emacs will prompt you for a keyword and display a list of commands whose names contain that keyword. For example, if you reply with the word “search,” you will see a list of all the emacs commands that have something to do with searching:

 isearch-*-char                (not bound to any keys)     Function: Handle * and ? specially in regexps. isearch-abort                 (not bound to any keys)     Function: Abort incremental search mode if searching is successful, signalling quit. isearch-backward              CTRL-r     Function: Do incremental search backward. isearch-backward-regexp ESC   CTRL-r     Function: Do incremental search backward for regular expression. isearch-complete              (not bound to any keys)     Function: Complete the search string from the strings on the search ring.

This listing is only the beginning of all the items relevant to “search.” You type ESC CTRL-V to scroll the help window to see the other entries.

The Ten-Minute emacs Tutorial

emacs, like vi, is a complex program. A list of the commands and what they mean, such as the one presented in the preceding pages, is important to have available and to know. But simply reading a command summary will not teach you how to use the editor.

The easiest way to learn emacs is to have a friend sit down with you and teach you its operation. The next best way is to try out the commands and see how they work and what effect they have on a file. To make it easy for you to do this, we provide a ten-minute tutorial for you to use. This tutorial quickly teaches you enough of the features and commands of emacs for you to begin using it for text editing and command editing in the shell. Before you begin, you should be logged into the UNIX System with your terminal (TERM) variable set. Once you’re ready, follow these steps:

  1. Type emacs mydog.

    emacs will start and show you an almost-blank screen. Your cursor will be at the first position of the first line.

  2. Since emacs is always ready to accept input, you can begin typing the following text:

    The quick brown fox jumped over the lazy dog. Through half-shut eyes, the dog watched the fox jump, and then wrote down his name. The dog drifted back to sleep and dreamed of biting the fox. What a foolish, sleepy dog.

  3. emacs does not have separate input and command modes. Regular alphanumeric characters are interpreted as input; control characters (CTRL-X) or metacharacters (ESC-X) are interpreted as commands.

  4. Go to the last line in the file by typing ESC->

  5. You can write the buffer to a file named dog by typing CTRL-X CTRL-S dog.

  6. Insert the contents of the file dog back into the current buffer by typing CTRL-X i dog.

  7. GO TO THE BEGINNING OF THE FILE BY TYPING ESC-<.

The following keys move the cursor by one position:

Operation

Move

Delete

Direction

Left

Right

Left

Right

Characters

CTRL-B

CTRL-F

DELETE

CTRL-D

Word

ESC-B

ESC-F

ESC-DELETE

ESC-D

Intraline

CTRL-A

CTRL-E

 

CTRL-K

Interline

CTRL-P

CTRL-N

 

CTRL-W

  1. Using these keys, position the cursor at “fox” and delete three characters by typing CTRL-D CTRL-D CTRL-D.

  2. Insert the word “cat” by typing cat.

  3. You can move to the next line with CTRL-N (Next) or to the previous line with CTRL-P (Previous). Press CTRL-N until the cursor is at “lazy dog.”

  4. Pressing ESC-F (Forward) will advance one word; ESC-B will back up one word. Back up to “lazy” by pressing ESC-B; go forward to “dog” by pressing ESC-F. Delete the word “dog” by typing ESC-D (for delete word). Undo this deletion by typing CTRL-X u (for undo).

  5. Scroll through the file by pressing CTRL-V to advance one screen, then press ESC-B to back up one half screen. Scroll to the end of the file, and then back up once by typing CTRL-V ESC-V.

    Your cursor should be at “the dog watched.” If it isn’t, move it there.

  6. Change the word “the” to “my” by typing ESC-Dmy (delete word, enter “my”).

  7. Move back to the first line of the file by typing ESC-<.

  8. Delete two lines into a buffer with CTRL-K CTRL-K CTRL-K CTRL-K.

  9. Move them to the end of the file with ESC->.

  10. Put the deleted material there by typing CTRL-Y.

  11. Move back one half screenful with ESC-V.

  12. Move to the end of the line with CTRL-E.

  13. Move to the beginning of the line with CTRL-A.

  14. Delete to the end of the line with CTRL-K.

  15. Write the file and quit with CTRL-X CTRL-C.

Advanced Editing with emacs

So far you have seen how to use emacs to add text to a file, how to move around a window, and how to do some simple editing. In this section, you will learn about some advanced features of emacs that make it easy to edit documents.

Searching for Text

Several methods are used to search for text while in emacs. We will describe incremental searches and regular expression searches here.

Incremental Searches   An incremental search searches for the string as you type it. That is, as you type the first letter of the search string, emacs finds the first word that starts with that letter, then the first word that starts with the first two letters you typed, and so on. To execute an incremental search for a string within the file, use these commands:

CTRL-S

Searches forward for a string

CTRL-R

Reverses search for a string

When you use the search commands, emacs prompts you with the words “I-Search:” at the bottom (message) line of the window and waits for you to type in a search string. When you have finished the search string, press ENTER. The characters in a search string have no special meaning in simple searches. To repeat a previous search, use the CTRL-S or CTRL-R command with an empty search string.

Regular Expression Searches   emacs also supports regular expression searches. Regular expression syntax of the kind used by ed, vi, grep, diff, and so forth is supported. In addition, some new regular expression semantics is supported with new expressions (such as \<, meaning “before cursor position in buffer”) defined for use in emacs.

Regular expression searches are not available with the simple search commands. They are available in the re-search (for regular expression search) commands such as re- search-forward, re-search-reverse, re-query-replace-string, and re-replace-string.

These commands are not bound to (associated with) a simple combination of keystrokes; they are invoked using the ESC-X command prefix, as in the ESC-X re- search- forward command. If you enter the string RE search:, it prompts for a search string that can contain a regular expression.

Modifying Text with emacs

You can do a global search and replacement in emacs using the replace-string command. replace-string is similar in operation to the ed command g/string/s//newstring/g, in that it replaces every instance of a string with a different string, except that emacs prompts you for the old and new strings. The command ESC-X replace-string prompts you with “Replace string:” Enter the word you want changed (“cat” for instance) and press ENTER, and you’ll be prompted with “Replace string cat with:”, at which point you can enter the replacement string (for instance “dog”).

If you wish to search for and interactively replace instances of a specific string of characters, you use the query-replace-string command, which is bound to the ESC-% keys. If you issue the command ESC-%, emacs will prompt you (at the bottom of the window) for the old string to be replaced and the new string to be used as the replacement. At each occurrence of the old string, emacs will position the cursor after the string and wait for you to tell it what to do. The options are

spacebar

Changes this one and goes on to the next

y

Changes this one and goes on to the next

n

Doesn’t change this one, goes on to the next

!

Changes this and all others without comment

.

Changes this one and quits

ESC

Exits query-replace

Copying and MovingText

emacs allows you to mark a particular region of your text and manipulate that region. You place a mark at the current position with the set-mark command CTRL-@. Setting a mark erases an old mark, if there is one. The position of this mark and the position of the cursor define a region. You move from end to end in the region by using the command CTRL-X CTRL-X, which exchanges the position of the dot (cursor) and the mark. You can move text around by marking a region, deleting it into a special buffer called the killbuffer, moving the cursor, and putting the contents of the killbuffer at a new place. The command CTRL-W is the command delete-to-killbuffer, which deletes the entire region.

To put the deleted or copied region at another place in your text, move your cursor to the new position and use the yank commands. For example, the command CTRL-Y is the command yank-from-killbuffer. It inserts the contents of the killbuffer at the cursor. After the yank, the cursor is positioned to the right of the insertion.

Editing with Multiple Windows

The preceding examples have used one window on the screen, with one file being edited. One of the advantages of emacs over vi is its ability to use several windows and edit several files. This is useful even if you are editing a single file. For example, you can use the split-current-window command CTRL-X 2 to put two windows on your screen, each associated with the same buffer. You can arrange to have text at the beginning of the file visible in one window, while viewing some other part of the file in the other window. You can work in one window, move the cursor around, define a region, and then switch to the other window and work there. You work in only one window at a time (only one has a cursor in it), but you can see different parts of the file at the same time.

You can switch between windows with the command CTRL-X o (lowercase letter O, for “other window”).

You can yank a region into the killbuffer, move to the other window, and put it at that point in the buffer.

You can split these windows into smaller ones and have several windows looking into the same buffer. (With the normal-sized screen, these windows start to get small when you split them, so it is usually not effective to use more than two to four windows at a time, unless you have a big screen.)

When you are done working in multiple windows, the command CTRL-X 1 will delete all the windows except the one your cursor is in. The command CTRL-X 0 (number 0) will delete only the window your cursor is in and give its space to a neighboring window.

When you use the command CTRL-X 2 to split the screen into two windows, both windows are associated with the same buffer. You can edit two files in two different windows by using the find-file command sequence CTRL-X CTRL-F, which will prompt you for the name of a file and put a buffer containing that file in the window. This gives you two files in two different windows. You work in one window at a time and switch between them.

emacs Window Command

Action

CTRL-X 2

Divides the current window into two

CTRL-X o

Moves to the other window

CTRL-X O

Deletes the current window

CTRL-X 1

Deletes all other windows except the current one

emacs Environments

The vi editor allows you to define macros, which are sequences of commands that execute when you use the macro name. A single-letter command can be translated into a command sequence several commands long.

emacs has a much richer facility Instead of simply allowing the execution of command sequences, emacs has built into it a full programming language: the Mlisp dialect of the lisp programming language. emacs users can write programs that are invoked as emacs commands. In some ways, this programming facility means that as a user, you can do most UNIX tasks with emacs. An experienced user would hardly ever have to leave emacs.

Using emacs to Issue Shell Commands

emacs has a shell mode that enables you to run a normal interactive UNIX shell from a window. While in emacs, give the command ESC-X shell and you’ll get a window that acts just like the normal UNIX shell interface, except that you can use emacs to edit the commands.

Using emacs to Edit Directories

You can also use emacs to edit directories. When you use Directory Edit (dired), you affect the files that are there. Using emacs, you can copy delete, or rename files within the editor. To start, you invoke emacs with the directory as an argument, for example $ emacs /u1/ home/rrr. emacs starts up and shows you a screen that looks like the output of the ls l command:

   /u1/home/rrr: total 34   drwx------    7 rrr  user  512 Jul  3 16:20 .   drwxr-xr-x  188 root user 3584 Jul  2 18:53 ..    rw------     1 rrr  user 1089 May 18 20:00 .cshrc   drwx------    2 rrr  user  512 May 18 20:00 .elm   -rw------     1 rrr  user   52 May 18 20:00 .history   -rw------     1 rrr  user  209 May 18 20:00 .login   -rw------     1 rrr  user  126 May 18 20:00 .mailrc   -rw------     1 rrr  user  423 Jul  3 11:31 .newsrc   -rw------     1 rrr  user 5494 Jun 30 17:11 .pinerc   -rw------     1 rrr  user 1835 May 18 20:00 .profile   drwx-----     2 rrr  user  512 Jun 22 13:37 Mail   drwx-----     2 rrr  user  512 Jun 13 21:37 News D -rw------     1 rrr  user  356 Jul  3 11:27 dog --%%-Dired: ~                    (Dired by name)--Top------

You move from file to file using the CTRL-N and CTRL-P commands to navigate. You can see the contents of a file by putting the cursor on the file and pressing v (view). You return to the directory listing by typing CTRL-C, or simply q.

To delete a file, you move the cursor to that file and mark it by pressing d. An uppercase D will appear to the left of the file entry In the preceding example, the file dog has been marked. You can move around the directory and mark as many files as you wish. When you are ready to have the files deleted, type x, and emacs will show you all the files marked for deletion, and ask you if you want them deleted. Some versions of emacs allow you to make other changes as well, but they are not part of the standard emacs program.

dired Command

Action

r

Renames file

e

Edits file

c

Copies file

d

Marks file for deletion

X

Deletes marked files

u

Undeletes

V

Views file contents

CTRL-N

Next file

CTRL-P

Previous file

How to Get emacs

Because emacs is not part of all UNIX distributions, it may not be available initially on your system. The first thing to check is whether it exists on your system. Ask a local expert or call your system administrator to find out. If you don’t have a system administrator (or if you are your own administrator) use the find command described in Chapter 3 to see if you already have it.

If it’s not available on your system, emacs is easy to obtain via the Internet using the File Transfer Protocol (FTP). You should ftp to the site ftp.gnu.org or go to the web site http://ftp.gnu.org/pub/gnu/emacs/. The emacs files are in the directory pub/gnu/emacs. The latest version is currently named emacs-21.3.tar.gz. This is a gzipped tape archive (tar) file that contains a version of the program. Download this file using the get command, unzip it, and run the tar command. This will create several hundred files on your system. The ones called INSTALL and README tell you how to build and install emacs on your system. Note that emacs can also be compiled for Windows systems, so you can have the same editor on your UNIX and Windows machines.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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