1. Emacs

Emacs is probably the most powerful text editor in existence. It can do absolutely everything and is infinitely extensible through 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 is not to say that you will learn how to do all of that in this chapter, but you will get a good start with opening Emacs, editing one or more files, saving them and quitting Emacs.

If, after reading this, you wish to learn more about Emacs, you can have a look at this Tutorial Introduction to GNU Emacs.

1.1. Short Presentation

Invoking Emacs is done as follows:

emacs [file1] [file2...]

Emacs will open every file entered as an argument into a separate buffer. If more than two files are specified at the command line, the window will be automatically split in two and there will be one part of it with the last file specified while the other part shows a list of available buffers. If you start Emacs without specifying any files on the command line you will be placed into a buffer called *scratch*. If you are in X, menus will be available and usable with the mouse, if you're on text mode, you can still access the menus with the F10 key, but in this chapter we will concentrate on working strictly with the keyboard and without any menus.

1.2. Getting Started

It's now time to get some hands-on experience. For our example, let us 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

By typing that command, the following window will be displayed:

Figure 4.1. Editing Two Files at Once

Editing Two Files at Once

As you can see, two buffers have been created. A third one is also present at the bottom of the screen (where you see (New file)). That 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-< or Ctrl-Home to go to the beginning of the buffer and Alt-> or Ctrl-End to go to the end of the buffer. There are many other combinations, even ones for each of the arrow keys [10].

Once you are 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 by pressing the Tab key just like with bash.

1.3. Handling buffers

If you want, you can switch to displaying a single buffer on the screen. There are two ways of doing this:

  • If you are in the buffer that you want to hide: type Ctrl-X 0.

  • If you are in the buffer which you want to keep on the screen: type Ctrl-X 1.

There are two ways of restoring a buffer back to the screen:

  • type Ctrl-X B and enter the name of the buffer you want, or

  • 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 you want to get rid of the associated buffer, type Ctrl-X K. Emacs will then ask you which buffer it should close. By default, this will be 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 mentioned.

You can open other files at any time, using Ctrl-X Ctrl-F. Emacs will prompt you for the file name and you can again use completion if you find it more convenient.

1.4. Copy, Cut, Paste, Search

Suppose you find yourself in the following situation: Figure 4.2, “Emacs, before copying the text block”.

Figure 4.2. Emacs, before copying the text block

Emacs, before copying the text block

First off, you will need to select the text you want to copy. 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 Figure 4.2, “Emacs, before copying the text block”, 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 following result:

Figure 4.3. Copying Text with emacs

Copying Text with emacs

In fact, what you've done is copy text to Emacs's kill ring. This kill ring contains all of 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 Ctrl-Y sequence 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 or Ctrl-Shift-- which will undo the previous operation. You can undo as many operations as you want.

1.5. Quit emacs

The shortcut to quit Emacs is Ctrl-X Ctrl-C. If you have not saved your changes, Emacs will ask you whether you want to save your buffers or not.



[10] Emacs has been designed to work on a great variety of machines, some of which do not have arrow keys on their keyboards. This is even more true of Vi.