Using Emacs

Advantages and Disadvantages of Emacs

Emacs is a text editor with some advantages over other text editors. Advantages are as follows. The main disadvantage of Emacs is that Generally, people who have learned emacs tend to be dissatisfied with other text editors.

Starting Emacs

To edit file foo.c, type
    emacs foo.c&
in a command window, if you are at a console, or
    emacs foo.c
if you are using telnet.


Emacs is case sensitive. X and x are considered different letters.

I will write ^X for the keystroke that is done by holding down the control key while typing X (or x). I will write esc for the escape character.

Some commands are just a single keystroke, but some require more than one keystroke. For example, command ^X ^C (the command to quit the editor) consists of a control-X followed by a control-C.

Basic Editing

Normally, Emacs just inserts any text that you type into the buffer. You can use the arrow keys to move the cursor. The backspace and delete keys each backspace. Use ^D to delete the character under the cursor.
     backspace  Do a backspace.

     ^D         Delete one character (forward).

     ^A         Move to the start of the current line.

     ^E         Move to the end of the current line.

The Minibuffer

There is a one line buffer at the bottom of the Emacs window that is called the minibuffer. It is used to communicate with you when commands need extra information.

The Status Line(s)

Each buffer except the minibuffer has a status line beneath it that tells whether the file has been modified (look for **) and other information, such as the editing mode. If you are editing a C++ file, you will probably see the mode as C++.


You can use the scroll bar. Drag it with the middle mouse button. Or use the following commands.
     ^V         Scroll down one page.

     esc v      Scroll up one page.     

     ^X <       Go to the start of the buffer.

     ^X >       Go to the end of the buffer.

Opening and Saving Files

When you edit a file, you are working on a buffer that is stored in memory. To save the file use
     ^X ^S      Save the current file.

     ^U ^X s    Save all modified files.
You can open a file without closing the one that you are working on.
     ^X ^F      Find (open) a file.  You will be asked for the
                name of the file in the minibuffer.

     ^X 4 ^F    Find (open) a file in another window.  Emacs splits
                the current window, so that you can see two files.

     ^X o       Switch to the other window.
You can switch to another buffer that you already have open.
     ^X b       Switch to another buffer.  You will be prompted
                for the name of the buffer, with the default
                being the most recently hidden buffer.

     ^X ^B      List all buffers.  Go to the window that shows the
                buffers.  Typing f while on a line in that window
                will take you to that buffer.  Typing o will take you
                to that buffer in the other window.

Cutting and Pasting

You can use the mouse to mark a region of text, but it is usually just as easy to use keystrokes.
     ^(space)   Mark the current buffer position.

     ^W         Cut (wipe) the text between the current cursor position and
                the most recently set mark, and put it on the

     esc w      Copy the text between the current cursor position and
                the most recently set mark to the clipboard.

     ^K         Cut the part of the current line from the current
                cursor position to the end, and put it on the clipboard.
                (If the line has no text on it, the line is cut.)

     ^Y         Paste (yank) what is on the clipboard to the current cursor
Emacs actually has a powerful cut/paste facility. If you do several cut or copy commands in a row, each one adds to what is already on the clipboard. Emacs keeps several clipboards in what is called the kill ring. When a new clipboard is started, the old one is pushed onto the kill ring. This allows you to get former clipboard contents.
     esc y      When done after a ^Y, this replaces what what was just
                pasted with the former clipboard contents.  The old
                clipboard contents is moved to the bottom of the kill
                ring.  Doing several esc Y commands in a row allows
                you to go through the entire kill ring.

Searching and Replacing Text

     ^S         Search for text.  You type the text into the minibuffer.
                As you type, the search will be performed, based on
                what you have typed so far.  Type ^S during the search
                to skip ahead to the next match.

                The search stops when you type return.

                If you reach the end of the buffer, type ^S again to
                continue the search from the front of the buffer.

     ^S ^S      Repeat the previous search.

     ^R         Search backwards.

     esc %      Perform a substitution.  You will be prompted for the
                string to replace and what to replace it with.  For
                each occurrence, you will be asked whether to replace
                that occurrence.  Type

                  y   to replace
                  n   to skip
                  q   to stop the command

Backup and Checkpoint Files

If you edit a file called, Emacs will periodically write a checkpoint file called This is in case of a system crash or other disaster. When you save the file, Emacs will write a backup file called The backup file is only written the first time that you save the file in a give session.

Getting help

The ^H command asks for help. Follow it with another keystroke to tell what kind of help you want.
     ^H k     Describe the command to which a given key sequence
              is bound.  Type the key sequence after ^H c.

     ^H f     Describe a given function.  You type the function name.

     ^H t     Give a tutorial on Emacs.

     ^H ^H ^H Describe available help commands.

Keyboard Macros

You can package a sequence of keystrokes for repeated execution.
     ^X (     Begin recording a keyboard macro.

     ^X )     End recording a keyboard macro.

     ^X e     Execute the last keyboard macro.  This consists of the
              sequence of keystrokes between ^X ( and ^X ).

Miscellaneous Commands

     ^X ^C   Exit emacs.

     ^G      Abort a command.

Long commands

You can give any command by name. Type esc x, followed by the name of the command, and a return. There are more commands than there are key sequences, so some must be run this way. Some of the commands are as follows.
     c-mode             Put the editor into C mode.  The tab key indents
                        the current line.  Braces self-indent, and a right
                        brace shows you the matching left brace, which is
                        very useful.

     c++-mode           Put the editor into C++ mode.  This is similar
                        to C mode.

     fundamental-mode   Put the editor into its default mode.

     goto-line          Goto a given line number.

     shell              Open a shell window.  This is useful when
                        running through a telnet connection.
You only need to type as much of the command as is necessary to distinguish it from other commands. Typing fund, for example, is enough to get command fundamental-mode. There are many more commands. You can learn some from the ^H t tutorial.


If you have a file called .emacs in your home directory, then Emacs will read that file to start. It is a good idea to get hold of somebody's .emacs file. I have installed a .emacs file into directory /export/stu/ugrad/skeleton, and you can copy it. You can look at that file. It is written in Emacs-Lisp. Commands offered by that file include the following.
     ^Z          Zap the current line.  This cuts the current line
                 and puts it on the clipboard.  Several zaps in a row
                 accumulate lines on the clipboard.

     ^B          Scroll up one line.

     ^F          Scroll down one line.

     esc G       Same as goto-line.

     esc R       Replace string.  Like esc %, but does not ask at each
                 replacement.  It just does them all.
Additionally, the current line number will be displayed in the status line. Read the .emacs file to see a little more on customizing.