Emacs is probably the most powerful text editor in existence. It can do absolutely everything and is infinitely extensible 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. This isn't to say that you'll learn how to do all of that in this chapter, but you'll get a good start with opening Emacs, editing one or more files, saving them and quitting Emacs.
Invoking Emacs is relatively simple:
emacs [file] [file...] |
Emacs will open every file entered as an argument into a separate buffer, with a maximum of two buffers visible at the same time. If you started Emacs without specifying any files on the command line you will be placed into a buffer called *scratch*. If you are in X, you will have menus available, but in this chapter we will concentrate on working strictly with the keyboard.
It is time to get some hands-on experience. For our example, let's start by opening two files, file1 and file2. If these 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 access this buffer directly; you must be invited by Emacs during interactive entries. To change the current buffer, type Ctrl+x o. You type text just as in a “normal” editor, deleting characters with the DEL or Backspace key.
To move around, you can use the arrow keys, or you could use the following 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 ones for each of the arrow keys [10].
Once you're ready to save your changes to disk, type Ctrl+x Ctrl+s, or if you want to save the contents of the buffer to another file, type Ctrl+x Ctrl+w. Emacs will ask you for the name of the file that the contents of the buffer should be written to. You can use completion to do this.
If you want, you can switch to displaying a single buffer on the screen. There are two ways of doing this:
There are two ways of restoring a buffer back to the screen:
type Ctrl+x Ctrl+b. This will open a new buffer 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 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”). To move between buffers, use the commands that were previously described.
You can open other files at any time, using Ctrl+x Ctrl+f. Emacs will prompt you for the filename and you can again use completion if you find it more convenient.
Suppose you find yourself in the following situation: Figure 4.2.
First off, you'll need to select the text that you want to copy. In X, you can highlight the text using the mouse, but we will focus on the text-based keyboard interface. In this example we want to copy the entire sentence. The first step is to place a mark at beginning of the area. Assuming the cursor is in the position where it is in the figure above, the command sequence would be Ctrl+ SPACE (Control + space bar). Emacs will display the message Mark set in the mini-buffer. Next, move to the beginning of the line with Ctrl+a. The area selected for copying or cutting is the entire area located between the mark and the cursor's current position, so in this case it will be the entire line of text. There are two command sequences available–Alt+w (to copy) or Ctrl+w (to cut). If you copy, Emacs will briefly return to the mark position so that you can view the selected area.
Finally, go to the buffer where you want the text to end up and type Ctrl+y. This will give you the result displayed in Figure 4.3.
In fact, what you have 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 Ctrl+y only “pastes” the area at the top. If you want to access any of the other areas, press Ctrl+y then Alt+y until you get to the desired text.
To search for text, go to the desired buffer and type Ctrl+s. Emacs will ask you what string it should search for. To continue a search with the same string in the current buffer, just 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.
To Undo, type Ctrl+x u which will undo the previous operation. You can undo as many operations as you want.
The shortcut for this is Ctrl+x Ctrl+c. If you haven't saved your changes, Emacs will ask whether you want to save your buffers.
[10] Emacs has been designed to work on a great variety of machines, some of which don't have arrow keys on their keyboards. This is even more true of Vi.