Linux-Mandrake:
User Guide and
Reference Manual

MandrakeSoft

 
 
January 2000
http://www.linux-mandrake.com


Next : Organization of the file tree
Previous : Introduction to the command line
Up

(Back to the table of contents)

Chapter 2 : Text Editing: Emacs and VI


As stated in the introduction, text editing[8] is a fundamental feature in the use of a Unix system. The two editors we are going to take a quick look at the use of, are slightly difficult initially, but once you have got the basics, both prove to be powerful tools.

Emacs

Emacs is probably the most powerful text editor in existence. It can do absolutely everything and is infinitely extendable thanks to its built-in Lisp-based programming language. With Emacs, you can move around the web, read your mail, take part in discussion forums, make coffee, etc. But what you will be able to do at the end of this section will be limited to: open Emacs, edit one or more files, save them and quit Emacs. Which is not bad to start with.

Short presentation

Calling up Emacs is relatively simple:

emacs [file] [file...]

Emacs will open every file entered as an argument in a buffer up to a maximum of two buffers visible at the same time, and will present you the buffer *scratch* if you do not specify a file. If you are in X, you also have menus available, but here we will look at working on the keyboard. C-x represents the sequence Control+x, M-s represents the sequence Alt+s.

Getting started

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 (if you write something in them).

$ emacs file1 file2

to get the window shown in figure 60.1.

Emacs, editing two files at once
Figure 60.1 Emacs, editing two files at once

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)): that is the mini-buffer. You cannot yourself go into this buffer, but must be invited by Emacs during interactive entries. To change buffers, type C-x o. You can type text as in a "normal" editor, deleted with the DEL or Backspace key.

To move around, you can use the arrow keys, but also other key combinations: C-a to go to the beginning of the line, C-e to go to the end of the line, M-< to go to the beginning of the buffer and M-> to go to the end of the buffer. There are many other combinations, even for each of the arrow keys[9].

As soon as you want to save changes made in a file, type C-x C-s, or if you want to save the contents of the buffer to another file, type C-x C-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.

Handling buffers

If you want, you can show only one buffer on the screen. There are two ways of doing this:

There are then two ways to restore the buffer which you want on the screen:

If you have finished with a file and want to get rid of the associated buffer, type C-x k. Emacs will then ask you which buffer it should close. By default, it is the name of the buffer in which you are; if you want to get rid of a buffer other than the one proposed, enter its name directly or all 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 C-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 to another buffer.

You can open other files at any time, using C-x C-f. Emacs will then ask you for the filename and here again completion is available.

Copy, cut, paste, search

Suppose we are in the situation of figure 13.2.

Emacs, before copying the text block
Figure 13.2 Emacs, before copying the text block

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 mark the beginning of the area. Assuming the cursor is in the position where it is in the figure above, first type C-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 C-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 M-w (to copy) or C-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 C-y, to obtain what is displayed in figure 13.3.

Emacs, after having copied the text block
Figure 13.3 Emacs, after having copied the text block

In fact, what you have just done is copy 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 C-y only "pastes" the area at the top: if you want to have access to the other areas, press C-y then M-y until you get to the desired text.

To search for text, go into the desired buffer and type C-s: Emacs then asks you what string to search for. To start a new search with the same string, still in the current buffer, type C-s again. When Emacs reaches the end of the buffer and finds no more occurrences, you can type C-s again to restart the search from the beginning of the buffer. Pressing the Enter key ends the search.

To search and replace, type M-%. 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: C-x u undoes the previous operation. You can undo as many operations as you want .

Quit Emacs

The shortcut for this is C-x C-c. Emacs then asks you whether you want to save the changes made to the buffers if you have not saved them .

VI: the ancestor

VI was the first full-screen editor in existence. That is one of the main objections of Unix detractors, but also one of the main arguments of its defenders: while it is complicated to learn, it is also an extremely powerful tool once one gets into the habit of using it. With a few keystrokes, a VI user can move mountains, and apart from Emacs, few text editors can say the same.

The version supplied with Linux-Mandrake is in fact VIm, for VI iMproved, but we will call it VI throughout this chapter.

Insert mode, command mode, ex mode...

First, calling up: exactly like Emacs. So let us go back to our two files and type:

$ vi file1 file2

At this point, you find yourself in front of a window resembling figure 13.4.

Starting position in VIm
Figure 13.4 Starting position in VIm

You are now in command mode in front of the first open file. And here, the difficulties begin :) In command mode, you cannot insert text into a file... To do this, you have to go into insert mode, and therefore enter one of the commands which allows you to do so:

In insert mode, you will see the string --INSERT-- appear at the bottom of the screen (so you know what mode you are in). It is in this and only this mode that you can enter text. To return to command mode, press the Esc key.

In insert mode, you can use the Backspace and DEL keys to delete text as you go along. To move around the text, both in command mode and in insert mode, you use the arrow keys. In command mode, there are also other key combinations which we will look at later.

ex mode is accessed by pressing the ':' key in command mode: the same ':' will appear at the bottom of the screen, and the cursor will be positioned on it. Everything you type subsequently, followed by pressing Enter, will be considered by VI to be an ex command. If you delete the command up to "delete" the ':', you will return to command mode and the cursor will go back to its original position.

To save changes to a file you type :w in command mode. If you want to save the contents of the buffer to another file, type :w <file_name>.

Handling buffers

As with Emacs, you can have several buffers displayed on the screen. To do this, use the :split command .

To move from one file to another, in a buffer, you type :next to move to the next file and :prev to move to the previous file. You can also use :e <file_name>, which allows you either to change to the desired file if this is already open, or to open another file. Here again completion is available.

To change buffers, type C-w j to go to the buffer below or C-w k to go to the buffer above. You can also use the up and down arrow keys instead of 'j' or 'k'. The :close command hides a buffer, the :q command closes it.

Watch out, VI is finicky: if you try to hide or close a buffer without saving the changes, the command will not be carried out and you will get this message:

No write since last change (use! to override)

In this case, do as you are told :) type :q! or :close!.

Editing text and move commands

Apart from the Backspace and DEL keys in edit mode, VI has many commands for deleting, copying, pasting, and replacing text -- in command mode. Here, we will look at a few. All the commands shown here are in fact separated into two parts: the action to be performed and its effect. The action may be:

The effect defines which group of characters the command acts upon. These same effect commands entered as they are in command mode correspond to movements:

Each of these effect characters or move commands can be preceded by a repetition number. For 'G', this references the line number in the file. On this basis, you can make all sorts of combinations. Some examples:

It is true that these commands are not very intuitive, but as always the best method is practice. But you can see that the expression "move mountains with a few keys" is not such an exaggeration :)

Cut, copy, paste

VI has a command that we have already seen for copying text: the 'y' command. To cut text, simply use the 'd' command. You have 27 memories for storing text: an anonymous memory and 26 memories named after the 26 lowercase letters of the alphabet.

To use the anonymous memory you enter the command as it is. So the command y12w copies to the anonymous memory the 12 words after the cursor[11]. Use d12w if you want to cut this area.

To use one of the 26 named memories, enter the sequence "<x> before the command, where <x> gives the name of the memory. Thus, to copy the same 12 words into the memory 'k', you would write "ky12w, and "kd12w to cut them.

To paste the contents of the anonymous memory, you use the commands 'p' or 'P' (for Paste), to insert text respectively after or before the cursor. To paste the contents of a named memory, use "<x>p or "<x>P in the same way (for example "dp will paste the contents of memory d after the cursor).

Let us look at the example of figure 13.5.

VIm, before copying the text block
Figure 13.5 VIm, before copying the text block

To carry out this action, we will:

We get the expected result, as shown in figure 13.6.

VIm, after having copied the text block
Figure 13.6 VIm, after having copied the text block

Searching for text is very simple: in command mode, you simply type '/' followed by the string to search for, and then press the Enter key. For example, /party will search for the string party from the current cursor position. Pressing 'n' takes you to the next occurrence, and if you reach the end of the file, the search will start again from the beginning. To search backwards, use '?' instead of '/'.

Quit VI

To quit, the command is :q (in fact, this command closes the active buffer, as we have already seen, but if it is the only buffer present, you quit VI). There is a shortcut: most of the time you edit only one file. So to quit, you will use:

By extension, you will have guessed that if you have several buffers, :wq will write the active buffer then close it.

A last word...

Of course, we have said much more here than was necessary (after all, the first aim was to edit a text file), but it is also to show you some of the possibilities of each of these editors. There is a great deal more to be said on them, as witnessed by the number of books dedicated to one or the other.

Take the time to absorb all this information, opt for one of them, or learn only what you think necessary. But at least you know that when you want to go further, you can :)


Next : Organization of the file tree
Previous : Introduction to the command line
Up

Copyright © 2000 MandrakeSoft