Introducing the Emacs editing environment
The first thing you should know is that the two most popular editors in UNIX are Emacs and vi, and both of them are over 30 years old. That hardly makes them obsolete tools, however. As with UNIX itself, the basic design is decades old, but today these editors are universal and used in cutting-edge development.
Emacs is one of the first open source and free software projects in history. Its inventor, Richard Stallman, is the founder of the GNU Project and its parent organization, the Free Software Foundation (see Resources). Even before he published the GNU Public License (GNU GPL) -- the license terms under which the majority of today's open source software is released -- Stallman published his Emacs source code under a similar free copyleft software license called the EMACS Public License.
The name Emacs has come to be a word of its own, but it initially was an acronym that stood for Editing MACros; Stallman's original implementation was a set of macros in the TECO language. Emacs is now written in Emacs Lisp, an elegant high-level programming language.
The GNU Project's short description of Emacs is the extensible self-documenting text editor. Emacs is extensible, meaning that its functionality can be added to and built upon. This is possible because it's written in Lisp, and you can write new Emacs Lisp routines to add new functions. They can run even while an Emacs session is still running. It's self-documenting because instant help is available for every keypress, even in the middle of typing a command. In that case, clicking the Help button pulls up a list of possibilities.
Emacs is said to be an editing environment because it's much more than just an editor in the usual sense of plain-text editing. Many administrators and developers on all platforms use it to compile and debug programs, manage e-mail, manipulate files on the system, run shell commands, and many other things. People even use it to communicate on Usenet news and to browse the Web. Extensions and built-ins handle everything from Internet Relay Chat (IRC) and messaging to network communications. An old UNIX joke goes something like, "The Emacs environment wouldn't be so bad, if only it had a good editor."
Because there's a definite Emacs way to do things, it has its own vocabulary, which you'll learn in this section as you step through an anatomy of a typical Emacs window. This section also covers how to start and stop Emacs and how to type various commands.
To start Emacs from a the shell, type:
When you start Emacs in a console or terminal, you should see it open to fill the entire terminal screen, as shown in Figure 1.
Figure 1. Emacs in a terminal window
If you're in X, Emacs generally opens in a window of its own, as shown in Figure 2. You can also specify that it open in a terminal window (like Figure 1) by giving the
-nw option. At first glance, these two views of Emacs might seem like different applications, but the differences between the two are superficial. The only real differences are the default colors, the graphical logo that the X client displays, and the set of graphical icons near the top of the X client that contain shortcuts for some of the most common commands. The functionality of the text and X versions is exactly the same.
Figure 2. Emacs, the X client
Some parts of the screen need explaining right away. Let's start from the top and work down.
At the top of the Emacs screen is a highlighted bar with some words on it. This is called the menu bar, and it lets you select common Emacs commands from menus. You can access the menus with the keyboard; with the X client, you can also pull down the menus with the mouse.
Experts usually configure their Emacs so that the menu bar is turned off, which gives a little more room on the screen for editing. But when you're learning Emacs, the menu bar is a nice way to familiarize yourself with its many features.
Additionally, if you opened Emacs in X, you should see a special set of icons at the top (see Figure 2); they're shortcuts to the most popular menu options.
To access the menu bar with the keyboard, press F10. Try it now and see how in the terminal a new window opens to show the list of menus, as in Figure 3.
Figure 3. Selecting from the Emacs menu bar
You can use the up and down arrow keys to move between the menu choices and then press Enter. If there's a submenu for your selection, it's displayed on the screen. You can select an option from the new submenu the same way, until you pick a command to run.
If you want to abort the procedure at any time, press Ctrl-G. This special keystroke makes a beep and quits whatever command is currently being executed. If no command is executing, pressing this keystroke only emits a beep. Try it now.
You should see the Emacs window revert to how it was before you selected the menu.
The large main area in the center of the screen is called the Emacs window, and it's where you do your editing. When you open a file for editing, this is where its contents are displayed. When the contents of a file or document appear in an Emacs window, it's called a buffer. You can have multiple buffers open in Emacs at any one time, even if they're not displayed in the main window. During an editing session, you typically have many buffers open, even if only one buffer is displayed in the window.
In the X client, a scroll bar appears to the left of the window. (The terminal version has one as well, but it displays only when a buffer is open.) The scroll bar shows where the text in the Emacs window is in relation to the rest of the buffer, and it also shows the size of this text in relation to the rest of the buffer.
The highlighted bar that run across the bottom of every Emacs window is called the mode line, and you can think of it as the status bar. It gives you important information about your Emacs session and about the current buffer displayed in the window above it, including whether your latest changes were saved to disk, what line number the cursor is at, how far into the buffer the bottom of the screen is (in percentage of the whole), and what Emacs features and settings are currently active.
The small space beneath the mode line and the bottom of the screen or X client window is a single line called the minibuffer. This is where Emacs displays messages that are relevant to any operations. When Emacs asks you to input anything, such as a file name, it happens here.
Like the UNIX shell, the minibuffer prompt uses tab completion. Press the Tab key to get a list of possibilities.
A particular Emacs key combination to call a given command is called a key binding. These can all be customized, but Emacs comes with default bindings.
At first glance, the large amount of default Emacs key bindings might seem confusing and complex, but remember that they're designed for speed and ease of memory. There's usually a mnemonic reason for every binding, such as using the S key for save. Think about these facts as you try them.
Broadly speaking, there are two major classifications of Emacs commands: those prefaced by the Ctrl key and those prefaced by what's called the Meta key.
Many Emacs commands are specified by a Ctrl key combination. In Emacs notation, Ctrl keys are written as "
C-" followed by the character corresponding to the second key to press. For example, the popular Ctrl-X combination is written
C-x in Emacs notation.
To enter a Ctrl key combination, press and hold the Ctrl key, press the second key, and then let go of both keys. Most commands have a Ctrl key combination followed by either a word or a second Ctrl key combination.
For instance, try running the
C-x C-s command, which saves the current buffer to disk. Because you haven't made any changes, you don't need to save anything, but it's a good key combination to try. Try it now:
- Press and hold the Ctrl key.
- Press the X key once and let go of both keys.
- Press and hold the Ctrl key again.
- Press the S key and let go of both keys.
This keystroke runs the
save-buffer command. In the minibuffer, Emacs reports, "(No
changes need to be saved)."
Emacs users often keep the Ctrl key pressed in the second step when running these combinations -- this omits the third step altogether and makes it even faster to type. Try it.
The second major type of Emacs keystroke is the Meta key; in Emacs notation, the Meta key is denoted by
M-. If you've never heard of a Meta key, that's because most systems today don't have one. There are three ways to input
Meta key combinations:
- The Meta key is often bound to the Alt key, which is used like the Ctrl key. If your setup is like that, use it -- it's the easiest and most common method.
- You can usually use the Esc key to give a Meta sequence, but you do it differently than when you enter a Ctrl sequence. Press Esc, let go, and then press the second key.
- You can use Ctrl-[ as a substitute for the Esc key. This is handy if you're running an Emacs session over a network line where both Esc and Alt don't work.
Try typing the
M-b command, which moves the cursor back one word, in three different ways:
- Press and hold the Alt key, and then press B once.
- Press Esc, let go, and then press B once.
- Press and hold Ctrl, press [, let go of both keys, and then press B once.
Every Emacs command you run is a function, defined in Emacs Lisp, and has a function name. Even the command to move the cursor to the previous line with the up arrow key is a function (
previous-line). You can run any function by using the
M-x command followed by the name of the function.
- Press and hold the Alt key.
- Press the X key.
- Let go of both keys and notice how "M-x" appears in the minibuffer.
previous-lineand press Enter.
When you do this, the cursor moves up another line. Of course, you normally wouldn't run this function that way, because using the up arrow key is so much easier, but it's a good example. There are many, many functions and, because Emacs is extensible, you can write your own to extend its capabilities.
Like the up arrow key shortcut for
previous-line, many functions are given keyboard shortcuts. When you type such a key or key combination, that function runs. You can also run the
C-p. Try it now:
- Press and hold Ctrl.
- Press the P key.
- Try the arrow key, too, and bring the cursor to the top of the window.
To recap, Table 1 lists the main types of key prefixes you can use in Emacs key bindings. Like anything else in Emacs, these can be customized and redefined.
Table 1. Common default Emacs key prefixes
||Commands particular to the current editing mode|
||Commands for files and buffers|
||Literal function name|
To exit Emacs, type:
If there are any unsaved buffers, this command gives you a chance to save them first.
This is the usual way to exit Emacs. Try it now, and then start Emacs up again by typing
the shell prompt.