Operating System Information --> Unix Information --> General Usage Topics --> Using the Emacs Text Editor -->

Using the Emacs Text Editor

Contents

Introduction

This document is intended to provide you with the basic information you need to use the emacs text editor. This document assumes that you are using your account from some kind of terminal window - either by connecting from home, by using a Macintosh or a PC in a computer lab or office at the University, or by using a terminal window generated by the "xterm" program on a Unix workstation. Information on how to connect to the OIT host computers with a terminal program is available on-line at http://www.helpdesk.umd.edu/topics/applications/terminal.

Note: Access to OIT Computer Labs at the University is limited. You must have a valid University ID to use the labs.

As you read this document ...

This document will not go into great detail regarding all of emacs - it will, however, give a sufficient introduction to emacs for the casual user, and will help point you in the right direction for learning more about some of the more advanced features of this environment. The emacs tutorial function also makes more information is available to the user.

There are other text editors that can be used on UNIX systems including vi and pico. Information relating to these editors is available on all of the UNIX systems maintained by OIT. Documents describing basic vi and pico commands: Using the Vi Text Editor and Using the Pico Text Editor are available.

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:

  • a single key.
  • a control-key sequence (e.g., Control-f).
  • a prefix key (e.g., Control-x or Esc). There are about eleven prefix keys in emacs, but typically only a few of these are used. The meta-key sequence, Esc, is also a prefix key.

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-xcompile 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.

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.

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 Escape 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, simply 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 type 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, ie., 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 a 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 a 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.

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, type 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]

Follow these commands, the following option will appear:

ESC $ ispell-word

The above option checks the spelling of the word ( $ ) that preceeded 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-{

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 won't 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

Files, Buffers, and Windows

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

  1. Type 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. Type 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. Type 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.

Other Features

As mentioned before, this is just an introduction to the world of emacs. To further the claim that emacs can do just about everything, here are some of its other features and abilities:

  • Modes for various languages: C, Perl, TeX, etc.
  • Lisp function evaluation
  • Tag Tables
  • Rectangle cutting, copying, and pasting
  • Lisp Debugger
  • Macros
  • Abbreviations
  • Built-in mail handler (Rmail)
  • Shell commands/buffers
  • Customizing key bindings
  • Emulation of VMS EDT and Unix vi editors

More information?

For more detailed documentation, visit http://www.gnu.org/software/emacs/manual.

How are we doing? Comments on this page?
Office of Information Technology
Office of Information Technology Help Desk Web Site University of Maryland Web Site Office of Information Technology Web Site