Using Emacs


Advantages and Disadvantages of Emacs

See here if you are interested in why Emacs is useful.


Starting Emacs

To edit file foo.cpp in a graphical environment, such as when you are using NX, use command

    emacs foo.cpp&
If you are using a text-only interface, use command
    emacs foo.cpp


Getting ready

Some of the editor commands described below are define in file .emacs. Copy that file into your home directory. Emacs will read it whenever it starts.


Backspace and DEL

If you use the SSH text-only client, you will probably want to select Edit/Settings. Under Profile Setting/Connections, select keyboard. Check the boxes for backspace sends delete and line wrap.

The backspace character is an ASCII control-H. Emacs uses that for requesting help. By making backspace send the delete character instead, you will not have a problem with that.

Emacs treates the delete character like most editors treate backspace. To delete the character under the cursor, use control-D.


Notation

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.


The Minibuffer

There is a one line pane 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 pane 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++.


Opening and Saving Files

When you open a file, it goes into a buffer where you can edit it.

Keystrokes Effect
^X ^F Find (open) a file. You will be asked for the name of the file in the minibuffer. This does not remove the current buffer. It just shows a new buffer in the pane that was formerly used for the previous buffer.

You can open a directory (a folder). Type f on a line the buffer showing the directory to open that file (or directory) in the current pane. Type o to open it in the other pane.

^X 4 ^F Find (open) a file in another pane. Emacs splits the window, so that you can see two buffers at once.
^X ^S Save the current buffer into its file.
^U ^X s Save all modified buffers.

Switching among buffers and panes

Keystrokes Effect
^X o Switch to the other pane.
^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 pane 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 pane.

Basic Editing

Normally, Emacs just inserts any text that you type into the buffer at the cursor.

Keystrokes Effect
DEL Do a backspace (delete the character before the cursor)
^D Delete one character (the one under the cursor)
^O Open a new line above the current line.
^X u Undo. Several of these in a row will undo several commands.

Scrolling

Use the arrow keys to move around. You can also use the following.

Keystrokes Effect
^A Move to the start of the current line.
^E Move to the end of the current line.
(page-down) Scroll down one page.
(page-up) Scroll up one page
^X < Go to the start of the buffer.
^X > Go to the end of the buffer.
ESC G Go to a line, by its number. You will be asked for the line number in the minibuffer.

Cutting and Pasting

Keystrokes Effect
^(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 clipboard.
ESC w Copy the text between the current cursor position and the most recently set mark to the clipboard.
^Z Cut the current line. If you cut several lines in a row, they are recorded as a single piece in the clipboard. (This is defined in .emacs.)
^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, cut the line.)
^Y Paste (yank) what is on the clipboard to the current cursor position.
ESC y If you type this after ^Y, the pasted material is replaced with the prior clipboard contents. Each time you type ESC y, the next clibboard down in the stack is used.

ESC y actually moves old clipboard contents to the bottom of the kill ring. If you do a subsequent ^Y, you will get the top clipboard in the kill ring. Doing a cut or copy operation (such as ^W) restores the kill ring so that the most recently cut or copied thing is on top, then adds the newly cut or copied text.


Searching and Replacing Text

Keystrokes Effect
^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
Keystrokes Effect
y to replace
n to skip
q to stop replacing
ESC X rs Replace a string by another. You will be prompted in the minibuffer. After typing the string to replace, press the ENTER key and type the key to replace, then ENTER again.

Backup and Checkpoint Files

If you edit a file called foo.cpp, Emacs will periodically write a checkpoint file called #foo.cc#. This is in case of a system crash or other disaster. When you save the file, Emacs will write a backup file called foo.cc~. 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.

Keystrokes Effect
^H k Describe the command to which a given key sequence is bound. Type the key sequence after ^H k.
^H f Describe a given Emacs function. You type the function name.
^H t Give a tutorial on Emacs.
^H ^H Describe available help commands.

Keyboard Macros

You can package a sequence of keystrokes for repeated execution.

Keystrokes Effect
^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 ).
^V Same as ^X e (but defined in the .emacs file provided)

Miscellaneous Commands

Keystrokes Effect
^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.

If a command needs to get additional information from you, it will ask in the minibuffer. Type your response and then press ENTER.

Keystrokes Effect
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.
column-number-mode Show column numbers in the status line.
fundamental-mode Put the editor into its default mode.
goto-line Goto a given line number.
shell Open a shell buffer that acts like a terminal, except that you have full editing capabilities.
rename-buffer Rename the current buffer.
tabs Set the distance between tab stops.

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.


Coding system

Keystrokes Effect
dos Convert to DOS/Windows coding system (where a line is ended by \r\n).
undos Remove ^M's from the entire buffer.
unix Convert to UNIX coding system (where a line is ended by \n).