First steps in Emacs

by Bas Kooijman, 5 April 2002


This note is meant to give a quick introduction to Emacs (Editor MACroS), which is an extensible, customizable editor and computing environment. It offers true Lisp--smoothly integrated into the editor--for writing extensions and provides an interface to the X Window System. At its core is an interpreter for Emacs Lisp (``elisp'', for short), a dialect of the Lisp programming language with extensions to support text editing.

Emacs runs under a.o. Windows and Unix; it recognizes and preserves both ways of breaking lines (carriage return + line feed in Windows, line feed only in Unix).

The sources basic use and quickie are used for this note.


enter carriage return, including the spaces around the word; so 4 enter means press "4" then "enter" (skip space)
C- A control character. C-f means "control then f" (pressed simultaneously). C-_ means "control then underscore" (which is undo!)
M- A two-character command sequence where the first character is Altmode (which is entered by ESCAPE). M-f means "ESCAPE then f" (pressed simultaneously)
F1 Press function-key "F1"
kill means delete and paste to clipboard (cut)
yank means copy from clipboard (paste)

Basic use of Emacs

To start editing a new buffer (i.e. temporary file) with Emacs type:
>> $ emacs
at the UNIX prompt, or click on the Emacs-icon in Windows.

Once you are inside the Emacs window just begin to type your text. Don't press enter at the end of each line, but M-q at the end of a paragraph for wrapping text (undo wrapping with C-_). Anytime you enter an editing command it will appear on the bottom left of your screen under the Emacs mode bar.

Many functions can be assessed via the menu-bar, where key short cuts are given.

To edit an existing file simply enter Emacs by typing
>> $ emacs
and then type C-x C-f (find file). Emacs will then prompt you for the name of the file to find and load it into the current buffer (use the space bar for automatic completion of the file name).

You may also type
>> $ emacs foo.m
and the Octave-file foo.m will automatically be copied into Emacs' buffer when it starts up. Emacs can hold many buffers, to wich you can switch and copy text from. Emacs can show its buffers simultaneously in several windows (see Emacs' menu bar).

Emacs recognizes extentions and loads editing-support functions automatically. The menu-bar depends on the extension. Here are some examples:
test.m a Octave source code file example.c a C source code file
try.p a Pascal source code file hello.f a Fortran source code file
letter.txt a simple text file paper.tex a Latex text file
list.bib a Bibtex source file

File name extensions (.txt) are not required. Emacs behaviour is coded in the .emacs or _emacs file (Unix or Windows repectively), such as the cursor behaviour when pressing "END". Octave and Latex support in Emacs require some special lines in the .emacs file.

Warning: Emacs looses all buffers upon exiting. You must save buffers into files before exiting. Type C-x C-s to save the file with the current file name or C-x C-w to write the file with a new name.

If the file you just saved already existed, Emacs will not only save your current file but also make a backup of the old file by giving it the same name followed by a tilde (~) e.g. foo.m~

After you are done with Emacs you may type C-x C-c to Exit Emacs.

For more help go into Emacs and type C-h t for the online tutorial. This opens a new buffer; come back to your old buffer with C-x b enter.

Some Emacs commands

Getting out

C-x C-s Write the current buffer into a new version of the current file name
C-x C-w Write the current buffer into a file with a different name
C-x C-c Finish by exiting to the exec

Buffer operations

C-x C-f Get a file into a buffer for editing
C-x i Insert a file at cursor
C-x b Select a different buffer (prompts; default is the last one)
C-x C-b Display the list of available buffers
C-x k Kill a buffer (prompts for which one; default is current one)
M-< Move to the top of the current buffer
M-> Move to the end of the current buffer

Help and helper functions

C-g Abort anything at any time (it beeps, sometimes you need 2)
C-_ undo last change
F1 A string Show every command containing string (try F1 A Paragr )
F1 c key What does this key do? (try F1 c C-k as an example)
F1 d string Describe a command (try F1 d Query Rep)
C-x d Directory editing subsystem; use "?" or "SPACE" to show possible completions
M-x string A command designated "by hand". M-x undo means "undo previous change". Use space bar for automized word completion (alternatives are shown).
C-d Yank back the last thing killed
M-n Provide a numeric argument of n for the command that follows (M-4 5 gives "5555"; M-6 C-x e executes pre-defined keyboard macro 6 times)
C-x ( key-sequence C-x ) Define key bindings (key-board macro) given in key-sequence, execute macro at cursor with C-x e

Character operations

C-b Move left (Back)
C-f Move right (Forward)
C-p Move up (Previous)
C-n Move down (Next)
BACKSPACE Delete left
C-d Delete right
C-t Transpose previous 2 characters (ht -> th)
C-q Inserts a control character in the buffer, e.g. C-q C-l inserts character ^L, C-q n enter inserts acsii character number n (octal); see HTML-4.0

Word operations

M-b Move left (Back)
M-f Move right (Forward)
M-BACKSPACE Kill left (C-y yanks it back at cursor)
M-d Kill right (C-y yanks it back at cursor)
M-t Transpose 2 words around cursor (if only -> only if)
M-c Capitalize word
M-u Uppercase word
M-l Lowercase word

Line operations

C-a Move to the beginning
C-e Move to the end
C-o Open up a line for typing
C-x C-o Close up any blank lines around cursor
M-0 C-k Kill from beginning to cursor (C-y yanks it back at cursor)
C-k Kill from cursor to end (C-y yanks it back at cursor)
M-g Goto line number n, where the number is asked for

Paragraph operations

C-x BACKSPACE Kill from beginning to cursor (C-y yanks it back at cursor)
M-k Kill from cursor to end (C-y yanks it back at cursor)
M-{ Move back to previous paragraph beginning
M-} Move to next paragraph end
M-q Fill the current paragraph (undo with C-_)
M-n C-x f Set the fill column to n (e.g. M-60 C-x f; default is 70)

Screen operations

C-v Show next screen
M-v Show previous screen
C-l Redisplay screen
M-0 C-l Move the line where cursor is to line 0 (top) of the screen

Search and replace

C-s "Incremental" search searches while you enter string (C-s str enter); case insensitive
C-r "Incremental" backward search (C-r str enter)
M-x replace-string Replace one string with another (M-x repl enter one string enter another)
M-x query-replace Replace one string with another, wants "y" meaning "do it" or "n" to skip (M-x Qu enter one string enter another)

Region operations

Region is area of buffer between cursor and mark (or mark and cursor). Some commands set the mark, so check it before using. You can also select a region with the mouse and then type C-w kill or C-$ (copy).
C-@ Set the mark (for use with REGION commands)
C-x r k Kill rectangle between mark and cursor
C-x r y Yank the last killed rectangle with upper left corner at cursor
C-x C-x Interchange cursor and mark (i.e. go to the other end of the current region)
C-w Kill region; C-y yank it back at cursor
C-$ Copy region; C-y yank it at cursor

Window operations

C-x 3 Split vertically screen in two windows (same buffer shown in each)
C-x 2 Split horizontally screen in two windows (same buffer shown in each)
C-x 1 Resume single window (using buffer from top window)
C-x o Move cursor to other window (all the usual commands apply)
M-x-v Display the next screen in the other window