As stated in the introduction, text editing[1] is a fundamental feature in the use of a Unix system. The two editors we are going to take a quick look at are a little difficult to use initially, but once you understand the basics, both prove to be powerful tools.
Emacs is probably the most powerful text editor in existence. It can do absolutely everything and is infinitely extendible thanks to its built-in lisp-based programming language. With Emacs, you can move around the web, read your mail, take part in usenet newsgroups, make coffee, and so on. However, what you will be able to do at the end of this section will be limited to opening Emacs, editing one or more files, saving them and quitting Emacs. Which is not bad to start with.
Invoking Emacs is relatively simple:
emacs [file] [file...] |
Emacs will open every file entered as an argument in a buffer, with up to a maximum of two buffers visible at the same time, and will present you with the buffer entitled *scratch* if you do not specify a file. If you are in X, you also have menus available, but in this chapter we will be working with the keyboard.
It is time to go hands-on. By way of example, let us open two files, file1 and file2. If these two files do not exist, they will be created as soon as you write something in them:
$ emacs file1 file2 |
You will get the window shown in figure 4-1.
As you can see, two buffers have been created: one per file. A third is also present at the bottom of the screen (where you see (New file)); this is the mini-buffer. You cannot force yourself into this buffer; you must be invited by Emacs during interactive entries. To change the current buffer, type Ctrl+x o. You can type text as in a "normal" editor, deleting characters with the DEL or Backspace key.
To move around, you can use the arrow keys, as well as these other key combinations: Ctrl+a to go to the beginning of the line, Ctrl+e to go to the end of the line, Alt+< to go to the beginning of the buffer and Alt+> to go to the end of the buffer. There are many other combinations, even for each of the arrow keys[2].
As soon as you want to save changes made in a file, type Ctrl+x Ctrl+s, or if you want to save the contents of the buffer to another file, type Ctrl+x Ctrl+w and Emacs will ask you for the name of the file to which the buffer contents are to be written. You can use completion to do this.
If you want, you can show only one buffer on the screen. There are two ways of doing this:
you are in the buffer which you want to hide: type Ctrl+x 0;
you are in the buffer which you want to keep on the screen: type Ctrl+x 1.
There are then two ways to restore the buffer that you want on the screen:
type Ctrl+x b and enter the name of the buffer you want,
type Ctrl+x Ctrl+b, a new buffer will then be opened, called *Buffer List*; you can move around this buffer using the sequence Ctrl+x o, then select the buffer you want and press the Enter key, or else type the name of the buffer in the mini-buffer. The buffer *Buffer List* returns to the background once you have made your choice.
If you have finished with a file and want to get rid of the associated buffer, type Ctrl+x k. Emacs will then ask you which buffer it should close. By default, it is the name of the buffer you are currently in. If you want to get rid of a buffer other than the one suggested, enter its name directly or press TAB: Emacs will then open yet another buffer called *Completions* giving the list of possible choices. Confirm the choice with the Enter key.
You can also restore two visible buffers to the screen at any time. To do this type Ctrl+x 2. By default, the new buffer created will be a copy of the current buffer (which enables you, for example, to edit a large file in several places "at the same time"), and you simply proceed as described previously to move between buffers.
You can open other files at any time, using Ctrl+x Ctrl+f. Emacs will then ask you for the filename and here again completion is available.
Suppose we are in the situation of figure 4-2.
First, you need to select the text that you want to copy. In X, you can do it using the mouse, and the area selected will even be highlighted. But here we are in text mode :-) In this case, we want to copy the whole sentence. First, you need to place a mark to select the beginning of the area. Assuming the cursor is in the position where it is in the figure above, first type Ctrl+SPACE (Control + space bar); Emacs will then display the message Mark set in the mini-buffer. Then move to the beginning of the line with Ctrl+a. The area selected for copying or cutting is the whole area located between the mark and the cursor's current position, hence in the present case the whole line. Next type Alt+w (to copy) or Ctrl+w (to cut). If you copy, Emacs will return briefly to the mark position, so that you can view the selected area.
Then go to the buffer to which you want to copy the text, and type Ctrl+y, to obtain what is displayed in figure 4-3.
In fact, what you have just done is copied text to the "kill ring" of Emacs; this kill ring contains all the areas copied or cut since Emacs was started. Any area just copied or cut is placed at the top of the kill ring. The sequence Ctrl+y only "pastes" the area at the top. If you want to have access to the other areas, press Ctrl+y then Alt+y until you get to the desired text.
To search for text, go into the desired buffer and type Ctrl+s. Emacs will then ask you what string to search for. To start a new search with the same string, still in the current buffer, type Ctrl+s again. When Emacs reaches the end of the buffer and finds no more occurrences, you can type Ctrl+s again to restart the search from the beginning of the buffer. Pressing the Enter key ends the search.
To search and replace, type Alt+%. Emacs asks you what string to search for, what to replace it with, and asks for confirmation for each occurrence it finds.
A final very useful thing: Ctrl+x u undoes the previous operation. You can undo as many operations as you want.
The shortcut for this is Ctrl+x Ctrl+c. Emacs then asks you whether you want to save the changes made to the buffers if you have not saved them.
[1] | "To edit text" means to modify the contents of a file holding mainly letters, digits, and punctuation signs; such files can be electronic messages, programs source code, documents, or configuration files. |
[2] | Emacs has been designed to work on a great variety of machines, some of which don't even have arrow keys. This is even more true of Vi. |