EMACS (which stands for ``Editor Macros'') is an advanced, highly extensible general purpose text editor. It is largely used by programmers, students and researchers, as it offers a large variety of features which facilitate writing code. EMACS itself is extensible via Elisp, which is a variation of the functional language Lisp.
This document aims to provide a short-and-fast introduction to editing text with EMACS, largely from a programmer's point of view. It is particularly targeted toward users who are not familiar with an advanced text editor and looking for more power than can be provided by editors such as Pico or Nano.
EMACS is largely controlled by key bindings--sequences of key presses which invoke certain actions. For example, Control-w is a key binding, and it means ``hold down the Control key while pressing the w key.''
EMACS key bindings can also be ``chained.'' For example, Control-x Control-c means ``hit Control-x (as described in the previous paragraph) and then hit Control-c.'' In a key binding like this, the user can simply hold Control down and hit x and c one after the other.
In this document, we use the following abbreviations when referring to key bindings:
| Abbreviation | Meaning |
| C-k | Control-k |
|---|---|
| M-k | Meta-k (see below) |
| S-k | Shift-k |
| A-k | Alt-k |
The Meta key is typically the ``Windows key'' on 104-key PC keyboards, between Control and Alt. On keyboards without a Windows key, Meta is sometimes Alt, since Alt is not as frequently used in EMACS key bindings as Meta. On Sun UNIX keyboards, the Meta key is next to the space bar, marked by a black diamond. If your keyboard lacks a Meta key, every key binding M-k can be ``emulated'' by hitting and releasing the Escape key, and then hitting k.
There is a special key binding, M-x, which prompts the user for an EMACS command to execute. In the rest of this document, M-x command means ``hit M-x, type command and hit Enter.''
Finally, when we say ``modifier key'' we mean any one of Control, Meta, Shift or Alt.
Start EMACS by typing emacs -nw filename.c at the command line. This will start EMACS in terminal mode, editing a file named filename.c. At this point, you can begin typing text. To exit EMACS with the possibility of saving your changes, type C-x C-c.
Note: At any point during the editing session, you can hit C-g to cancel the current ``action.'' For example, if you want to hit C-x C-c but you first hit C-c by mistake, you can hit C-g to cancel and start over.
To enable color highlighting of source code, type M-x
global-font-lock-mode. This process can be easily automated,
see section
.
It is possible that in your current setup, the arrow keys and Home, End, PgUp and PgDn keys can be used to navigate the file, but this might not always be the case, and it is not always desirable. The following commands are guaranteed to work to move around a file in EMACS. Note that by ``move'' we mean ``move the cursor.''
| Command | Action |
| C-f | Move forward one character |
|---|---|
| C-b | Move backward one character |
| C-n | Move down one line |
| C-p | Move up one line |
| C-v | Move down one page |
| M-v | Move up one page |
| C-l | Scroll the page so the cursor is in the middle |
| C-a | Move to the beginning of the line |
| C-e | Move to the end of the line |
| M-f | Move forward one word |
| M-b | Move backward one word |
| M-a | Move to the end of the paragraph |
| M-e | Move to the beginning of the paragraph |
| M-x goto-line | Move to a specific line number |
All of these key bindings (with the exception of C-a and C-e) can be hit repeatedly to cause the given action to be executed several times.
When you want to hit a sequence of bindings which have the same modifier key, you can hold down the modifier for the duration of the sequence. If, for example, you want to move two lines down and two characters forward, you can type n n f f while holding down the Control key continuously.
To save a file, type C-x C-s. To save the current file under a different name, type C-x C-w. EMACS will prompt you for the file name you would like to write to.
To interactively search for a sequence of characters somewhere below the current position of the cursor, hit C-s. EMACS searches for the text as you type it, so the more characters you type, the more accurate the search match will be. Hit C-s again to jump to the next match.
To search backwards, i.e. to search the text above the cursor in the buffer, use C-r instead of C-s.
The terms ``cut'' and ``paste'' are known as ``kill'' and ``yank,'' respectively, in the EMACS world. Cutting, copying and pasting are commands which operate on text regions, not unlike ``selections'' which you would select with the mouse in graphical editors.
To select a region of text, move the cursor on top of the first
character of the text you would like to select and hit
C-SPACE (that is, hit the space bar while holding down
Control). At this point we say that ``the region is active.''
Now move the cursor (See section
) one character past the
last character of the text you would like to select,and hit one of:
| Command | Action |
| C-w | Kill (cut) the region |
|---|---|
| M-w | Copy the region |
Once you have cut or copied the region, you can yank (paste) it at the cursor position by hitting C-y.
Unlike most other editors, EMACS saves the last N kills and copies into something called the Kill Ring--a clipboard history of sorts. Thus, if you copy some text X and then copy some text Y, you will then be able to yank (paste) X if you want to. This turns out to be quite useful in many situations.
To yank something from the Kill Ring, whenever you hit C-y to yank, hit M-y immediately afterward to yank the next entry in the Kill Ring. Hit M-y repeatedly until you yank the desired entry.
EMACS comes with several command packages which can spell-check a portion of text interactively. The most popular spell-check package is probably ispell, which uses the ispell UNIX program in the background.
A partial summary of EMACS ispell functionality:
| Command | Action |
| M-x ispell-buffer | Spell-check the whole current buffer |
|---|---|
| M-x ispell-region | Spell-check the currently active region |
| M-x ispell-word | Spell-check the word the cursor is on |
| M-x ispell-comments-and-strings | Does what it says, assuming the current |
| buffer contains code |
The previous section gives an introduction to EMACS' abilities to
facilitate editing a single file. EMACS can do much, much more,
however. It can keep multiple files open simultaneously so that you
can switch between them, it can show you several files at the same
time in a split view, it can allow you to look at different parts of
the same file in a split view, and in Graphical mode, (see section
) you can open several frames (windows), each with their
own split views.
When you edit a file in a text editor, you are not physically editing the actual file on disk. You are making changes to an intermediate representation of the file, which is somewhere in memory. When you are done making changes and ``save the file,'' this intermediate representation of the file is flushed back to disk, overwriting what was previously there. EMACS calls this intermediate representation a buffer.
One instance of EMACS can have several open buffers. There is at least one open buffer, named *scratch*, which cannot be deleted. This is the buffer you see when you open EMACS without giving it a file argument. Note that not all buffers correspond to files on disk, and *scratch* is an example of such a buffer.
To open a new file from a running EMACS, type C-x C-f. This will prompt you for a file location, create a new buffer with that file's contents, and make that buffer the current buffer. Here are some common buffer-related commands:
| Command | Action |
| C-x k | Delete/kill current buffer |
|---|---|
| C-x b | Switch to another buffer. |
| (Default is last buffer you visited.) | |
| C-x C-b | Open a new window (see section ) |
| showing the current buffers. |
In addition to buffers (ability to edit multiple files simultaneously), EMACS has windows: the ability to give you several views into several files simultaneously. An emacs window is not a window in the graphical user interface sense--it cannot be moved around the screen, etc. It is a window in the sense of ``a limited view into something larger.'' Any EMACS instance has at least one window, which cannot be closed.
To split the current window horizontally, hit C-x 2. Now you have two windows, each giving a different view of the same buffer. You can ``jump'' between them by hitting C-x o. You can scroll each one to a different point in the buffer. If two windows ``share'' the same buffer, changing text in one window is immediately visible in the other.
Here are some common window-related commands:
| Command | Action |
| C-x 2 | Split current window horizontally |
|---|---|
| C-x 3 | Split current window vertically |
| C-x o | ``Jump'' to another window |
| C-x 0 | Close current window |
| C-x 1 | Close all windows except the current one |
If the graphical version if EMACS (See section
) is
available, you can make use of frames. Essentially, a frame is a
physical window--here, in the graphical user interface sense. What
you are looking at when you start a graphical EMACS session is a
frame. There is at least one frame for any session, which cannot be
deleted. The only way to delete it is the same way you can delete the
last window or the last buffer: by quitting the editor.
To start a new frame, hit C-x 5 2. You can now do in the new frame everything that you could in the previous. The two frames share all the open buffers, so a change to a buffer in one frame is immediately visible in the other. To close the current frame, hit C-x 5 0.
Typically, the same EMACS binary is capable of running in graphical mode as well as terminal mode. So far we have been talking in the context of terminal mode. If you are capable of running graphical programs (i.e. you are in front of an X11-capable computer or something equivalent), you can type emacs to start EMACS in graphical mode.
Graphical mode has clickable icons, regions can be selected with the mouse, and much of the core editor functionality is available through graphical menus. All of the keyboard commands discussed in this document are valid for both graphical and terminal EMACS.
EMACS can be customized and extended by placing the appropriate commands in a file named .emacs (note the dot) in the user's home directory. We will not go into the details of extending EMACS here, but we give an example of a simple .emacs file, which can be used as a starting point.
Minimal .emacs file:
(global-font-lock-mode t) ; always highlight source code (blink-cursor-mode -1) ; make cursor not blink (setq-default indent-tabs-mode nil) ; use 8 spaces instead of 1 tab (setq require-final-newline t) ; make sure all files end in \n (setq case-fold-search t) ; search is case-insensitive (if window-system (setq mouse-wheel-mode t)) ; enable mouse wheel in graphical mode (setq c-basic-offset 4) ; make indent size 4 spaces in C (global-set-key [(meta g)] 'goto-line) ; bind M-g to the goto-line command
EMACS is a popular editor, so the documentation is plentiful. Googling for ``emacs cheat sheet,'' ``emacs manual'' and ``emacs tutorial'' turns up useful results. We describe a few sources of information:
A list of essential EMACS commands:
| Fundamental | |
| C-x C-c | Exit EMACS |
|---|---|
| C-x C-s | Save file |
| C-x C-w | Save file as |
| C-g | Cancel whatever you're doing |
| Movement | |
| C-f | Move forward one character |
| C-b | Move backward one character |
| C-n | Move down one line |
| C-p | Move up one line |
| C-v | Move down one page |
| M-v | Move up one page |
| C-l | Scroll the page so the cursor is in the middle |
| C-a | Move to the beginning of the line |
| C-e | Move to the end of the line |
| M-f | Move forward one word |
| M-b | Move backward one word |
| M-a | Move to the end of the paragraph |
| M-e | Move to the beginning of the paragraph |
| M-x goto-line | Move to a specific line number |
| Search | |
| C-s | Search forward |
| C-r | Search backward |
| Kill & Yank | |
| C-SPACE | Activate a region |
| C-w | Kill (Cut) |
| C-y | Yank (Paste) |
| M-w | Copy |
| M-y | After C-y, yank next entry in the Kill Ring |
| Buffers | |
| C-x C-f | Open new file in new buffer |
| C-x k | Delete/kill current buffer |
| C-x b | Switch to another buffer. |
| C-x C-b | Open a new window showing the current buffers. |
| Windows/Frames | |
| C-x 2 | Split current window horizontally |
| C-x 3 | Split current window vertically |
| C-x o | ``Jump'' to another window |
| C-x 0 | Close current window |
| C-x 1 | Close all windows except the current one |
| C-x 5 2 | Open new frame |
| C-x 5 0 | Close frame |
| Spell-check | |
| M-x ispell-buffer | Spell-check the whole current buffer |
| M-x ispell-region | Spell-check the currently active region |
| M-x ispell-word | Spell-check the word the cursor is on |
| M-x ispell-comments-and-strings | Does what it says, assuming the current |
| buffer contains code | |
| Help | |
| M-x help-with-tutorial | Interactive tutorial |
| M-x help | Interactive help |
| M-x info-emacs-manual | Complete EMACS manual |