Use the Emacs Text Editor for UNIX


Table of contents

Overview of Emacs

In the world of UNIX text editors, Emacs is a special case. It is more than just an editor; it is a complete Lisp environment. It can do, with sufficient prodding, just about anything. Emacs has many sets of commands, each of which executes a particular function. Many of these functions are bound or attached to key sequences. Key sequences are one or more keys, with or without a prefix key. Key sequences can be:

All alpha keys (A-Z) and numeric keys (0-9) are Emacs functions. They are bound to the command self-insert, which inserts the key that is typed into the text buffer.

There are a number of different variations of Emacs. The one described in this document is GNU Emacs.

There are several different modes that Emacs runs within. Unlike many other text editors, Emacs does not have separate edit and input modes; its different modes are environments designed for specialized purposes. The mode for editing text is the Fundamental mode. The C mode, for editing C programs, provides automatic indentation and brace matching. Other modes include Perl mode for editing Perl programs and Tex mode for editing Tex/LaTeX documents.

In addition to specialized programming modes, Emacs can compile programs without leaving the editor. The key sequence Esc-x compile will open a second window and run the make program. The output from the compilation will be sent to this window, allowing you to view compilation errors alongside the actual source code.

Top

Entering Emacs commands

As previously mentioned, most Emacs commands are bound to keys and some keys are bound to the self-insert command producing printable characters. All the normal letters, numbers and special characters on the standard keyboard will be entered as text unless part of a command sequence. Most others, though, are bound to control keys, or to sequences-of-control keys. To enter a command, you simply type the key sequence to which the command is bound.

Top

Emacs command notation

Most Emacs function names are multiple words, separated by hyphens (e.g., describe-variable or save-buffers-kill-emacs). Their bindings are represented by C- for control key bindings, and M- for meta key bindings. The Meta key on most keyboards is the Esc key.

Control key bindings are entered by holding down the Ctrl key, and then pressing the given key. For instance, C-v is bound to "scroll-down" one screen. To scroll downwards to the next screen, hold down the Ctrl key and then press the v key. For meta key sequences, on the other hand, press and release the Esc key, then enter the remaining keys in the sequence. For instance, Esc-v will scroll up one screen.

Some commands are accessed through multiple meta and control sequences. To save all buffers and exit Emacs, for example, use the two-key sequence C-x C-c.

All Emacs commands can be bound to multiple or different keys, using other Emacs commands, or a startup file (often called .emacs on UNIX systems). In addition, a list of all current bindings can be seen by invoking describe-bindings, which is usually bound to C-h b (the two-key sequence of a Ctrl-h, followed by typing b). The command bindings described in this document are the default keybindings. All commands can be invoked directly by using the M-x command (this is the only way to invoke a command that has no binding). This gives a prompt for a command line at the bottom of the screen. Enter the argument for the command to be executed. M-x is invoked by the command Esc-x. After typing the Esc-x command, Emacs will tell you the key sequence for that command, i.e., after entering M-x undo, Emacs will briefly display "you can run the command 'undo' by typing C-_.."

For example, if a document is more than 100 lines long and you want to readily move to line 34, the following Emacs command will place the cursor at line 34.

M-x goto-line
Goto line:34

The illustration below indicates how to use Emacs to modify a file - the .environment file. There are several labels used here to describe this illustration.

Modifying the .environment file

The Menu Bar is active only when displaying the Emacs editing window on an X-windows server display. If you are not certain that you are operating in this environment, click on any of the Menu Bar items with your mouse. If a drop-down menu of choices is displayed, you are operating on an X-windows display. You can use the Menu Bar as an alternate method for entering various keystroke combinations. Below is a description of commands available on the Menu Bar.

Buffers    To check on and/or move around in all the different buffers Emacs can access: the current window, the message buffer and a scratch buffer (used by default). Other buffers may be created during an editing session, for example, when you use the M-x compile function.
Files To open a new file, save the current editing buffer, revert to buffer, insert a file into the current buffer, open a new window or split the current window
Tools To print, compare or merge files; you can also invoke a newsreader, a mail program or a calendar tool
Edit To perform basic editing commands: cut/copy/paste, spell checking
Search To search forward/backward with strings or regular expressions, to repeat a previous search(es) and to perform a search-and-replace function
Help For Emacs news, Emacs FAQs, describe mode, describe key bindings, Emacs tutorial, etc.

The editing area in Emacs is located in the Text Editing Region. The Status Bar displays the current file name, editing mode (fundamental, Perl, etc.) and the line location of cursor inside of the file. Any command functions that the user may invoke, is also displayed in the Action Bar area. The previous illustration on this page displays an example of a text search. When you invoked the search command (C-s) and enter the search string (in this case EDITOR), Emacs will position the cursor at the end of the word.

Top

Getting help in Emacs

Emacs has an extensive on-line help feature. This feature includes such things as command lookup, binding lists, and a tutorial. By far the best way to learn about Emacs is to use it as often as possible. The tutorial gives an introduction to general Emacs use, and is a good place for beginners to start. The other help functions, particularly describe-function, can help better explain many of the commands described here. A hypertext documentation browser is also provided - to run this, use the info function (it is fairly self-documenting). The help prefix is C-h. The C-h C-h command will give a list of possible help options. 

NOTE: You can also select the Help option from the Emacs menu bar.

Function Binding Function Binding
command-apropos C-h a describe-bindings C-h b
describe-key-briefly    C-h c key    describe-function C-h f
info C-h i describe-key C-h k
describe-mode C-h m view-emacs-news    C-h n
help-with-tutorial C-h t describe-variable C-h v
where-is C-h w    

For example, to illustrate the above functions, enter the following sequence of commands to spell check a document using emacs's help feature: 

C-h b [enter the describe-bindings command]
C-x o [enter the bindings screen]
C-s spell    [search for the spell utility]

If you follow these commands, the following option will appear:

ESC $ ispell-word

The above option checks the spelling of the word ( $ ) that preceded the cursor when the spell utility was invoked.

Other options with the spell utility include: 

Esc-x spell-buffer [checks your buffer]
Esc-x spell-region    [checks the defined region]
Esc-x spell-string [checks the specified string]

For the above example, the spell utility option Esc-x spell-buffer will do the job. 

NOTE: Some interesting features available are:
C-x 1 -- one window,
C-x 2 -- two horizontal windows, and
C-x 3 -- two vertical windows.

Moving the Cursor

Moving the cursor in Emacs is a little different than with most text editors - rather than placing the keys in a sort of diamond or row, Emacs uses (in most cases) letters which correspond to English words to make their functions easier to remember. Some commands deal not with characters, but with English constructs like words or sentences. These are often the same letter as an analogous character-based function, but with a meta key instead of a control key. The cursor in an Emacs session is also called the point. 

Function Binding Function Binding
previous-line C-p backward-sentence M-a
next-line C-n end-of-line C-e
forward-char C-f forward-sentence M-e
forward-word M-f scroll-down C-v
backward-char C-b scroll-up M-v
backward-word M-b beginning-of-buffer M-<
beginning-of-line C-a end-of-buffer M->
recenter C-l transpose-chars C-t
capitalize M-c forward-paragraph M-}
backward-paragraph M-{    

Top

Cutting, copying, and pasting

When you delete a line (or part of one), it is stored in a kill ring, which basically is a list of all recent deletions. Text stored in the kill ring can be yanked back into the document at a later time by using Ctrl-y. To yank something other than the most recent kill, use Meta-y (yank-pop), after first yanking with C-y. Each time M-y is used, the last deletion is removed from the screen and replaced with the previous deletion. In this way you can cut and paste several different blocks of text at once. To copy a block, just kill it, yank it, and then move to where it should be copied, and yank it again. Only commands with kill in their name get stored in the kill ring - that is, simply deleting 20 characters with C-d will not work. Killing text is often done between the mark and the point. 

Function Binding Function Binding
delete-char C-d yank C-y
kill-word M-d yank-pop M-y
kill-line C-k delete-backward-char     DEL
kill-sentence M-k backward-kill-word M-DEL
open-line C-o delete-blank-lines C-x C-o
undo C-x u query-replace M-%
isearch-forward    C-s isearch-backward C-r
kill-rectangle C-x r k    yank rectangle C-x r y


Top

Files, buffers, and windows

The following is a step-by-step procedure to copy a paragraph:

1. Enter C-Space at the beginning of the paragraph to set the mark.
2. Move the cursor to the end of the paragraph using C-n until you reach the end (or use M-}).
3. Enter M-w which will place a copy of the paragraph into the kill ring.
4. Move the cursor the location where you want to place a copy of the paragraph.
5. Enter C-y which will place the contents from the buffer in the desired location.

Function Binding Function Binding
find-file C-x C-f    list-directory C-x C-d
save-buffer C-x C-s insert-file (none)
save-some-buffers C-x s split-window-vertically C-x 3
switch-to-buffer C-x b split-window-horizontally    C-x 2
list-buffers C-x C-b delete-other-windows C-x 1
kill-buffer C-x k kill-window C-x 0
kill-some-buffers (none) other-window C-x o
find-alternate-file C-x C-v enlarge-window C-x ^
write-file C-x C-w write-region (none)
save-buffers-kill-emacs    C-x C-c move-to-previous-table C-space

NOTE: Alternatives to the C-space command, the "set-mark" function, include C-@ and M-x set-mark.

Top