The coelacanth of text editors

Emacs Abides

People either love or hate the Emacs text editor.

By Bruce Byfield

Fans of Emacs praise its speed and appreciate the ability to do much of their daily computing without leaving its environment. Flamers dismiss it as difficult to learn and as arcane as non-Euclidean geometry. These criticisms have a measure of truth, but, once you grasp a few basic concepts, you can start to understand why the Emacs family of editors has been the main rival of Vi and its clones for decades.

Emacs and similarly structured editors have existed since the early 1970s. A major figure in their development is Richard M. Stallman, the founder of the Free Software Foundation, who began work in 1984 on GNU Emacs, the first application completed by the GNU Project. He maintained the project off and on until a couple of years ago.

GNU Emacs [1] is probably the most widely used version of Emacs - so much so that, when you hear Emacs mentioned without qualification, GNU Emacs is usually what is being discussed. Today, GNU Emacs features more than a thousand commands accessible by keystrokes and dozens of specialized editing modes. Fortunately, though, the average user can get by without learning most of this specialized knowledge until it's required. In fact, some more advanced commands are disabled by default for the sake of beginners.

Learning to Speak Emacs

Emacs seems hard to use because of its unique structure and terminology. For example, Emacs can be configured with a number of modes, or working environments, each with its own features [2]. Some modes are designed for writing in specific programming languages, with customized keyboard shortcuts and pre-set indentations for the language. Others are designed for general text editing.

When you open a file, Emacs places it in a separate buffer, or editing space. The current view of a buffer is called a window. Depending on the size of the file, a window might show all of a file or only part. At any given time, you can have multiple windows open, showing either different parts of the same buffer or parts of different buffers.

The modes, buffers, and windows you use are all enclosed in a frame. On the desktop, a frame is equivalent to an editing window. At the command line, a frame makes Emacs appear as though it is embedded in the command line, running as a separate application (Figure 1).

Figure 1: Whether running on the desktop (bottom) or at a command line (top), Emacs looks much the same.

Emacs also has some special editing vocabulary. Your present position in a file is marked by a square cursor, which - as you can see when running Emacs from your desktop - is separate from the mouse cursor. The cursor's position is called the point. Also, you can set a mark in addition to your present position, so that all the text between the point and the mark is affected by a command. Similarly, cutting text is referred to as "killing" and pasting as "yanking."

This specialized vocabulary originates from the earliest days of Emacs, when people were not working from desktops, and the language was not yet standardized. Now, millions of users are familiar with these old terms, and changing them would only add confusion.

The Keystroke System and Abbreviations

Emacs was designed for a mouseless command line and 8-bit ASCII characters. It does have menus, even at the command line (although in some versions, they might not be accessible). However, most Emacs users prefer to use the editor with keystroke combinations, using the windows mostly as a crib sheet.

Although this structure makes Emacs highly portable, it also makes the keystrokes for many commands longer than the comparatively simple keyboard shortcuts for applications like Also, it means that Emacs long ago established its own notation system to make writing these commands less cumbersome.

Typically, a keystroke combination begins with a compound key, indicated by a capital letter. Usually, it is the Ctrl key, which is abbreviated to C in Emacs documentation. When the keys in a combination are joined by a hyphen, that means that you should press both keys simultaneously. However, when a key in the combination is not joined to another key by a hyphen, you should press it after letting go of the keys that are joined by a hyphen.

For instance, if you want to view the Emacs info help file, the command is C-h i. That means you should press Ctrl+h, release both keys, then press the i key. Similarly, but somewhat more elaborately, to read Emacs's statement that it comes with no warranty, the command is C-h C-w, which indicates that you should press Ctrl+h, release both keys, then press Ctrl+w together. Sensibly, these longer combinations are usually reserved for more advanced or obscure commands, or commands that you only use once or twice per session.

After the Ctrl key, the most common compound key is the Meta key (M). However, a confusing feature of the Meta key is that its identity can change, depending on your keyboard. Although it is usually the Alt key, if that doesn't work, try the Esc key instead.

To make matters worse, Esc is an ASCII character in its own right. Consequently, if you are using Esc as a Meta key, you don't press keys in the same way that you would for a combination that involves Ctrl or Alt. When a command requires the Meta key and another key simultaneously, you must first press the Esc key, release it, and then press the other key. Otherwise, the key combination won't have the effect you expect.

Although you might also find references to other keys in Emacs documentation, mercifully, these are usually obvious at a glance, such as Del for delete.

As you read Emacs documentation, you will also find that each key combination is associated with a command name of half a dozen words or fewer. As you might expect in an editor designed by old Unix hands who abhorred blank spaces in a file name, the words in each command name are joined by hyphens. However, this stylistic oddity should not affect you in any way.

Orienting Yourself in Emacs

With these basic concepts in mind, you are ready to start exploring Emacs. To start the editor, use the command emacs, or emacs filename if you have a specific text file to edit. If you start Emacs without an existing file, then you can use the "find-file" command (C-x C-f) to locate a file whose name you know or to name a new file.

If you are running a desktop, these commands open the graphical version of Emacs unless you add the parameters -nw after the command. The graphical version lets you use a mouse, but the main difference from the command-line version is that it looks - slightly - more polished.

Regardless of how you are running Emacs, the environment is similar (Figure 2). Most of the environment is a window showing the current file you are working on. Unlike in Vi, this window does not require you to shift into any mode before you start editing. Instead, you simply start typing. The cursor, a rectangular block, shows the current position.

Figure 2: The Emacs window comprises the menu, the edit window, the mode line or status bar, and the mini-buffer that displays notifications.

Below the window is the mode line, which gives information about what you are doing. It inverts the colors at the command line, so that if you are entering white letters on a black background, it will display black letters on a white background.

However, you can never directly edit the mode line. Each buffer that is visible in the window will have its own mode line. From left to right, the mode line gives the following information:

  1. The state of the current buffer. A series of hyphens indicates that you have made no changes, two asterisks indicates that you have made changes, and two percent signs means that the file is read-only.
  1. The name of the file. If you are not editing a file, then *scratch* displays.
  1. How much of the file is currently visible: All, Top, Bot (bottom), or the percentage above the window.
  1. The line you are on. A new line begins when you press the Enter key, so remember that this statistic does not refer to the number of lines that you appear to see in the window.

Below the mode line is the minibuffer. The minibuffer displays messages about what Emacs is doing (Figure 3). When you enter a keystroke combination that requires more input, such as find-file, the writing in the minibuffer changes color and you are prompted with ~/. Also, the cursor moves into the minibuffer until you provide input and press the Enter key.

Figure 3: The Emacs minibuffer is a command-line equivalent of a notification tray, giving information about what you are doing (top). When a command requires input, the Emacs cursor moves to the minibuffer (bottom).

At the command line, you cannot enter the menus at the top of the window directly (Figure 4). Instead, you press F10 to open the *Completions* buffer, which gives you the names of the top-level menus from which you can select. Your choice moves you to another *Completions* buffer that gives you a choice of menu items. Possible choices, with an ellipsis (three periods) at the end, will require more input from you in the minibuffer.

Figure 4: You cannot enter the menus directly in command-line Emacs, but you can access the items in them.

During your first explorations of Emacs, you are almost certain to find that you have blundered into a buffer that you don't know how to exit. Or, you might be displaying buffers that you don't need and want to close. To help orient yourself, you can use the list-buffers command (C-x C-b) to see which buffers you are running. Then, you can use C-x b to switch to a buffer you name or use C-x k to kill the buffer you name. To exit from a command in the minibuffer, use C-g.

To save a file, use C-x C-s and wait for the message in the minibuffer that it has been saved to file. To exit Emacs, use C-x C-c.

Editing with Emacs

You can apply editing functions in Emacs to any unit of text. Often commands that perform the same function on a different unit have very similar commands.

For example, you can use C-f to move forward one character at a time, or C-b to move back one character at a time. To navigate by words, the commands are M-f and M-b; by vertical lines, C-a and C-e; and by sentences, M-a and M-e. With paragraphs, the commands are M-{ and M-}, although naturally you have to remember to insert line breaks as you work to make these commands useful. To navigate to the previous screen, use M-v; to go to the next screen use M-e.

Similarly, you can delete characters with C-d and lines with C-l. Also, you can mark (select) text with the use of M-@ for words and M-h for paragraphs. To select text manually, the command is C-Space bar, followed by repetitions of C-f to advance character by character.

Once text is deleted, it goes into a kill ring or an Undo history that has 30 steps by default. Entering M-y changes the active step by moving around the kill ring one step at a time. The active step can be pasted (yanked) with C-y.

To search for and replace text, type M-% and enter the target string followed by its replacement string. As the command locates each instance of the target, you have the option of selecting y to replace the current instance, n to move to the next occurrence without replacing, a . (period) to replace and exit, or ! to replace all remaining matches.

These commands are only the most basic. As you can see when you start Emacs without specifying a file, the editor has a rich collection of help available (Figure 5). A useful place to start is with the tutorial (C-h t). Later, you might find Emacs's info help useful (C-h i), or you might want to search by keywords (C-h p) or keystroke combination (C-h k) [3].

Figure 5: Emacs has a detailed help system, including the tutorial shown here.

Choosing a Mode

So far, I have described the Fundamental mode, which is the most basic mode within Emacs and is a powerful editor in its own right. The main reason people learn Emacs is for the variety of other modes customized for very specific purposes.

Emacs is especially rich in modes for programmers. C, Perl, Python, shell scripts, and Awk are only some of the languages supported by different modes. These modes provide customizations for interacting with the language's debugger, adding specific functions to the menubars, expanded help, and many other features.

Other modes are aimed at classic documentation choices, such as HTML, TeX, and SGML, or more general editing, such as the Text Refill mode, which provides the automatic word wrapping that the Fundamental mode does not have. The ispell mode provides spell checking (Figure 6). Still other modes are designed for running a Bash shell, reading a man page, using FTP, reading email, viewing PDF files, and even playing games. If you can think of a purpose, Emacs very likely has a mode to fill the niche.

Figure 6: Emacs's behavior changes radically with the modes that are loaded. Here, Text Refill mode provides automatic line wrap, whereas ispell provides spell-checking.

Some modes are packed with distributions, along with resources to enrich them. Others you will need to download and install separately [4]. However, once the modes are installed, you can change the current mode with M-x.

Choosing Between Emacs and Vi

In the years that I have used GNU/Linux, I have tried to detect a pattern in Emacs and Vi use. Seemingly, the two text editors should appeal to different groups of developers. Perhaps Vi should appeal more to digital thinkers, or Emacs to analog thinkers? But no such pattern ever emerges.

However, I have observed that the popularity of editors seems to be regional. For example, in my native Vancouver, people favor Vi, whereas in Indianapolis, Emacs is dominant (or was 10 years ago when I traveled there regularly). These differences, I suspect, could probably be traced to local schools of computing science.

Whatever the source of the differences, when you familiarize yourself with either Emacs or Vi, you start to understand why each is defended so fiercely by its fans. Learning to use either editor well requires a considerable investment of time, and, having made that investment once, users are naturally reluctant to make it again. Emacs especially is a world of its own - almost its own operating system, people joke - with its own jargon, notation, and keystrokes.

I think of Emacs as a computer coelacanth - a living fossil with few connections to anything else being done in modern computing. Yet, for all of its uniqueness and complexity, Emacs quickly repays the trouble of learning it. Spend even an afternoon learning some of its basics, and you'll appreciate the power and convenience it delivers if you show a little patience.

[1] GNU Emacs:
[2] Emacs modes:
[3] Installing Emacs packages:
[4] Installing Emacs add-ons: