↑↑ Home ↑ UNIX

The editor emacs

Self-defence -- Moving -- Delete and paste -- Help features -- Labour-saving -- Customising -- Hackery


It is slightly risky to try to use emacs without knowing anything about it. It is so powerful that an accidental keypress can completely change the edited file, the portion of text displayed, your outlook in life, or the world order. To avoid that (at least to avoid it being done unintentionally), it is useful to know at least a starter's kit of key codes. Here they are.

The most important key shortcuts in emacs are Ctrl-g and Ctrl-_. Which is more important depends on whether according to your personal taste it is more disconcerting to see a bad thing happen or to find it has happened already. Ctrl-g aborts the command currently being executed. emacs prints out brief status messages in the bottom line of its window. (It also displays the control key sequence you typed so far if you wait a second or so. Unlike other programs, emacs has multi-key shortcuts.) If you see anything there that worries you, you still have a chance to abort with Ctrl-g. Ctrl-_ is the undo function, to be used when large parts of your text are suddenly gone.

To decide the hairy question of whether the text was modified beyond recognition or you just jumped to a different position (not so easy to tell in large files), the last but one line in the emacs window is helpful. It displays at its beginning something like "--:--" if the file is unchanged, and "--:**" if it was modified.

Another useful key shortcut is Ctrl-x Ctrl-c, which quits emacs. Conversely, if you read the following in the bottom (status) line:

Save file ...? (y, n, !, ., q, C-r or C-h)
it means that you have pressed Ctrl-x Ctrl-c accidentally and that emacs is going to quit if you don't abort with Ctrl-g. (This is always allowed even though it is not listed in the status message.) Incidentally, you save your file with Ctrl-x Ctrl-s.

Cursor movement

"Whaddayamean, cursor movement, I use the cursor keys, right?" Sure, you can use them, mate, but you might save some work by using other keys, too. As for cursor keys, some terminal keyboards do not have them, and therefore emacs originally used other key codes for moving the cursor up and down, left and right. They are easy to remember and a real alternative on laptops where cursor keys are squeezed into a corner of the keyboard and are sometimes small. Ctrl-p ("previous line") moves the cursor up, Ctrl-n ("next line") moves it down, Ctrl-f ("forward") moves it to the right and Ctrl-b ("backward") to the left.

Besides, there are other movement commands: Ctrl-e moves to the end of the line, Ctrl-a to the beginning. Alt-f moves one word forward, and Alt-b one word backward. (Really this should be "Meta"-f and "Meta"-b, but nowadays more people use Linux than other UNIX systems, and there the two are the same. If you find the Alt key codes do not work on a workstation or X terminal, look for additional modifier keys which may be marked with some strange symbol.) Alt-e moves to the end of the sentence and Alt-a to the beginning. This is an example for a rather general principle of emacs keys: Where the combinations with Ctrl act on smaller, "physical" units like single characters and lines, the corresponding Alt key acts on larger, "semantical" units like words and sentences. Important: To be able to use the functions which deal with sentences, you have to leave two spaces after a full stop. emacs interprets as full stops only points followed by two spaces; points followed by just one are taken to be abbreviation points.

Those more used to cursor keys can also use Ctrl or Alt with the right and left arrow keys to move over words. The keys Home, End, PageUp and PageDown work as expected, though of course emacs has its own key combination for moving up and down by a page. Ctrl-v moves one page down, Alt-v one page up. Alt-< or Ctrl-Home move to the top of the buffer (= edited file), Alt-> or Ctrl-End to the bottom.

If you have split an emacs window (with Ctrl-x 2, to display two files or two parts of the same file), you can leaf through the other one with Ctrl-Alt-v (page down) and Shift-Ctrl-Alt-v (page up). This saves you moving the cursor to the other frame with Ctrl-x o (or a mouseclick) if you just want to use it for reading. (By the way, you get rid of the other frame with Ctrl-x 1).

emacs commands for searching behave somewhat differently from the search commands of other programs. The keys for them are Ctrl-s for searching forward and Ctrl-r for backward. These commands perform a so-called incremental search. That means that emacs searches as soon you have typed the first character. When you have typed the second, the two character combination will be searched for and so on. This has the advantage that you usually don't have to type the whole expression you want to find. If you mistype, just press backspace. To abort the search and return to the original position, type Ctrl-g (twice in some versions). You jump to the next match by repeating the search key (Ctrl-s or Ctrl-r) and reverse the direction of your search by typing the respective other one. By starting a search by pressing Ctrl-s (or Ctrl-r) twice one can repeat the previous search.

There is one more advanced emacs feature for positioning the cursor (which is, by the way, called the "point" in emacs lingo). You have a number of registers for storing postions in a file. You save the current position by typing Ctrl-x r Space and a character which determines the register. You can jump to that position with Ctrl-x r j and the same character used for saving.

emacs keys are pretty much standard on UNIX systems. There are many other programs where you can use Ctrl-a, Ctrl-e, Alt-f, Alt-b and others in input lines. The degree of compliance is proportional to the closeness of the program's relationship with UNIX ;). The shell bash is largely emacs compatible (providing the xterm passes the keys correctly, which is often a problem; rxvt seems to work fine). So is the calculator bc and the plot program gnuplot. Even Qt (a graphical user interface library) supports Ctrl-a and Ctrl-e and can be patched to support some word-oriented functions.

Lastly, there is the command "goto-line". In some versions of emacs, it is bound to the key Alt-g. This is not quite standard and has only come up recently. An alternative is to go to the top with Ctrl-Home and move down the desired number of lines with Alt- Down, where the number has to be typed while holding down the Alt key. Or one can call the function by Alt-x goto-line. Or define the key binding, see below. Or...

Delete, cut and paste

Like many other things, cutting and pasting is done a bit differently in emacs compared to other editors. Whenever more than a single character is deleted, it is saved in a storage for removed text called the kill ring. If several chunks of text are deleted in a row without a different command in between, they are put together and saved as one. So if you delete several words with Alt-Backspace or Alt-d or a combination of both, you will be able to insert the whole phrase.

Inserting killed text is called "yanking" in emacs parlace, and hence the key combination is Ctrl-y. (Often Insert does the same.) This yanks the most recently killed or the most recently yanked text (if the last command was a yank). For obtaining earlier killed portions of text, you can either give a numerical argument to Ctrl-y (by typing Alt-<number> before it) or use Alt-y. Alt-y has to be pressed immediately after Ctrl-y and replaces the yanked text with the text preceding it in the kill ring. By pressing it repeatedly, one can go some way back.

There are other commands than Alt-d and Alt-Backspace which kill text. There is Ctrl-k which removes everything between the cursor and the end of the line (if there is anything) or the following newline (otherwise). By pressing it repeatedly one can move several lines of text to the kill ring. Alt-k differs from Ctrl-k in that it deletes everything up to the end of a sentence (in line with the principle that Alt key combinations work on semantical entities).

Then there is Ctrl-w ("wipe"). It kills all the text between the cursor and the mark. The mark is a unique position marker used mainly for this purpose. It is set with Ctrl-Space. (Some versions of emacs then highlight the selected text (between mark and cursor). If this annoys you, you can quit the highlighting with Ctrl-g. The mark remains and can be used anyway.) It is also set automatically to the position of a yank. The key combination Ctrl-x Ctrl-x exchanges the position of cursor and mark, which allows to move the opposite end of the selected text. The region between cursor and mark can also be copied, with Alt-w. The other deleting commands have no corresponding copying command; if you want to copy with them you have to yank the text after deleting it.

Using deleting commands to copy text may seem strange to users of other editors. But it has the distinct advantage that text you removed never is really gone. If you remember much later that you want to keep the original paragraph after all, you can still retrieve it from the kill ring. Besides, having a wide range of commands to choose from to kill text, one can use the one which requires least cursor movement, such as Alt-d or Ctrl-k to kill text right of the cursor or Alt-Backspace to kill text to the left.

Getting help

emacs has an extensive help system that makes some more graphically-oriented programs look uninformative. The key command for getting help is Ctrl-h. This can be followed by many different keys specifying the type of information you want. Typing a question mark after Ctrl-h displays all of them, without aborting the help command.

Possibly the most important help command is Ctrl-h k. It displays information on the key combination pressed after it. For instance, if you type Ctrl-h k Ctrl-g, the emacs window will be split, and the lower half displays something like: "C-g runs the command keyboard-quit, which is an interactive compiled Lisp function in `simple'...", followed by a short description of what this function does. The less luxurious version of this command is Ctrl-h c, which just prints the binding in the status line. This is sufficient for people with some knowledge of basic emacs functions and saves removing the help frame with Ctrl-x 1.

The next most important help key combination is easily Ctrl-h i. It enters the info documentation browser, which can display the documentation of emacs and other Gnu programs. It is more comfortable to use than the info program, which is why I define

function info() { emacs --eval '(info)' --eval '(Info-menu "'$1'")' & }
in my bashrc. This redefines the info command to open an emacs window and use emacs's info function to display the info page.

Other help commands which are sometimes useful are Ctrl-h f, Ctrl-h w and Ctrl-h m. Ctrl-h f describes a function you specify by its name, rather than its key binding, and Ctrl-h w gives you all key (and menu) bindings for a given function. You don't usually have to type the full function name; press Tab to make emacs complete it. Ctrl-h m displays information on the current mode. emacs enters a specific mode when editing a file of a specific type (eg C++, HTML, TeX,...). Depending on the mode, some key bindings may be changed and special commands for that mode be available. These are printed in the help page about the mode.

Making the machine do the chores

Users of ordinary text editors spend a lot of time doing the sort of work computers were supposed to save us from. Ever waited seconds filling a line with a character? Or put comment signs before whole paragraphs by hand?

emacs can save you a lot of work doing that kind of thing provided you know how. The simplest kind of automation is doing the same thing several times. Most emacs commands accept a numerical argument, and most of them interpret it as a repeat count. There are two ways of giving that argument: Holding down the Alt key while typing a number or typing Ctrl-u and then the number. Both have to be used before the command in question is executed. For instance, Alt-7 Alt-5 * results in a line of 75 stars. This feature can also be helpful with the cursor keys and Backspace. It is surprisingly easy to estimate the distance in characters between the cursor and a different position, say of a typo. (Presumably it helps that I use a fixed-width font.)

If you have to something more complicated a large number of times, keyboard macros come in useful. You define a keyboard macro by typing Ctrl-(, the keys of the macro (which will be executed as usual) and Ctrl-). You call it with Ctrl-x e. So if you want to comment out lines in TeX, you could define the macro as Ctrl-a % Down and then type Alt-7 Ctrl-x e to comment out seven lines. To remove every second line, define the macro as Ctrl-k Ctrl-k Down and execute it with a repeat count.

The first example task for repeated keyboard macros could be accomplished in an even easier way. Since manipulations of columns are needed fairly frequently, emacs provides special commands for that purpose. They all work on the rectangle between the cursor and the mark. Ctrl-x r k kills the rectangle, Ctrl-x r y yanks (pastes) it, and Ctrl-x r t replaces each line of it by the text you input. The inserted text may well be wider or smaller than the existing text, which may also be empty if mark and cursor are in the same column. The easiest way to comment out code in TeX or script languages is to set the mark at the beginning of the first line you want to comment out, move to the beginning of the last and type Ctrl-x r t and the comment character (followed by Return). It will be inserted at the beginnning of all the lines.

Last but not least, there are commands concerning indentation. Automatic indentation is pretty much standard even in "non-emacs" editors, but even here emacs adds a few useful commands of its own. Pressing Return in emacs inserts a newline character. For indenting the next line in the same process, one has to type Ctrl-j instead. (In newer versions, Return may already indent.) Joining an indented line with the previous one is done with Alt-^, which replaces the indentation by a single space. The command Alt-Space explicitly replaces an arbitrary amount of white spaces (spaces or tabs) at the cursor position with one space.

The .emacs file and emacs lisp

Before starting to customise emacs, one has to understand how emacs works in principle. All of its functionality is programmed in the programming language emacs Lisp. Many standard functions are pre-defined, and the user can define new ones. Keys can be bound to any function. Again there are many pre-defined bindings, but they can be changed arbitrarily. Many functions are not bound to any key and can only be accessed via the function "execute-extended-command", which is usually bound to the key Alt-x. It is part of the Lisp syntax that functions contain a description of what they do. This is what is printed when the user requests information on it with Ctrl-h f (help on a function) or Ctrl-h k.

The configuration file .emacs in a user's home directory contains Lisp statements, ie calls of existing functions and definitions of new ones. A lisp function call has the following form:

(function argument1 argument2 ...)
One of the most frequently used functions is setq which assigns a value to a variable. Its first argument is the variable, the second the value. For instance, "(setq make-backup-files nil)" sets the variable "make-backup-files" to nil. nil is Lisp's value for false. Any other value means true as far as boolean variables are concerned. The above example tells emacs not to make backups when saving a file.

Also very useful is the function global-set-key. It does what it says it does. Its first argument is the key, the second the name of the function to be bound to it. The key can be specified in many different ways; the easiest is as a double-quoted string containing all keys to be pressed as characters. You can enter a combination with the Ctrl or Alt modifier key by pressing Ctrl-q before it. Ctrl key combinations are displayed as the character "^" plus a capital letter, but this combination represents one character (for instance it can be deleted with one backspace). This specification cannot be used for special keys like Return, Tab, Home, End and others. They can be given with their name, prefixed by "S-", "C-" or "M-" for the modifier keys (M stands for Meta=Alt), in rectagular brackets [].

The name of the function to be bound to a key has to be quoted by putting an inverted comma before (but not after) it. Some examples for global key bindings:

(global-set-key "^X " 'point-to-register)
(global-set-key [insert] 'yank)
(global-set-key [C-insert] 'overwrite-mode)
The first binds the function point-to-register (which saves the current cursor position) to the key Ctrl-x Space. (Don't copy this example to your .emacs file from this browser window; I replaced the Ctrl character with two characters because your browser would not have liked it.) The other two bind yank (insert last piece of text in the kill ring) to Insert and the function which toggles overwrite mode to Ctrl-Insert. You can copy these verbatim if your default binding is that Insert calls overwrite-mode (as it was in my version of emacs).

A Lisp function is defined with the keyword defun. It is used in a similar way from a function call. The general form is:

(defun function (parameters) description (interactive) function definition)
First comes the keyword defun followed by the name of the function. After that there is the parameter list which may well be empty (the parentheses have to be there anyway). Then comes the description of the function which is a double-quoted string. If the function shall be bound to a key or invoked with Alt-x (execute-extended-command), the keyword interactive has to be there, in parentheses. Last but not least comes the function definition which consists of Lisp expressions.

When you have written a couple of new functions and key bindings, you don't have to restart emacs to see whether they are correct. The function eval-buffer makes emacs execute the Lisp expressions in the current buffer. It is not usually bound to any key, but can be executed with Alt-x.

It can be somewhat difficult to obtain information about emacs Lisp. A previous version of my distribution included an info document about it; the last upgrade put paid to that. Now I have a very useful HTML page which I'm not sure about how I obtained it. Presumably from the Free Software Foundation (gnu.org) which develops emacs. Anyway there is bound to be something there. As always when learning a programming language, it is very useful to read programs written by others.

...By the way, Lisp does not stand for "lots of isolated silly parentheses" but for "list processing" ;).

Advanced hackery

Sure you want to read this? It might be a bit much for people who have never hacked machine code...;)

We have seen that a lot of things can be changed in emacs. However, one of the most annoying features cannot easily be defeated: emacs' inclination to tell the user what kind of indentation to use is a given programming language. While this is no doubt well meant and intended to save the user the trouble of indenting lines at all, this requires a lot of work to counter when you have other ideas than the programmers of emacs what your source code should look like. My preferred indentation tends to depend not just on syntax but also on semantics and other non-obvious criteria which even a smart editor can hardly be expected to take into account (and doesn't).

Just rebinding the Tab key to self-insert-command does not solve the problem. (Even ignoring the fact that self-insert-command doesn't seem to work for Tab.) In C++ and Perl mode, characters which have special meaning in that language automatically adjust the indentation. The functions they are bound to tend to be called something like "c-electric-brace" etc. (see Ctrl-h m for a list of mode-specific bindings).

Now how to get rid of them? Not so easy. Key bindings of specific modes are executed only when the mode is entered, that is after the .emacs file. The corresponding code is contained in compiled Lisp files that come with emacs. Presumably one could install a "mode hook", a function which is run after loading a mode, and redefine all the key bindings.

Sounds like a lot of work (if you take into account finding out how hooks work). Why not have a look at the mode-specific bindings? The compiled Lisp files in question are called cc-langs.elc and perl-mode.elc. Compiled Lisp consists of binary data followed by a token list. This list contains all the strings specifying keys and function names for making the relevant key bindings. To save space, of course all tokens are present only once. Since these tokens are obviously referred to by the binary data, it wouldn't do to remove one. All those after it would probably be referenced wrongly because their index would change. So what to do? Simple: replace the unwanted bindings with one which is anyway being made. For instance, in C++ mode, Ctrl-c Ctrl-n is bound to c-forward-conditional. So we replace all "electric" keys with "^C^N" and all "electric" functions with c-forward-conditional. So the default bindings for the special characters remain unchanged by the mode file.


TOS / Impressum