Academic and Research Computing
QUICK STUDY #23
March 2001

Using the UNIX emacs editor

Whether you are writing a program, writing a paper, or editing a mail message, you will need to create, store, retrieve, view, and edit files. Emacs is one of several UNIX text editors available on RCS. You can use emacs on an RCS workstation or via a remote connection.

If you are using a workstation, you'll find that many of the key sequences described in the sections below are available on the emacs menu, making it easy to perform a number of common tasks.

Starting emacs

To create a new file or to view an existing file, enter the following command at a UNIX prompt:

emacs filename
replacing filename with the name of the file. From a workstation, it is a good idea to append an & to the above command. Doing so makes emacs run ``in the background,'' allowing you to work in other windows without quitting emacs.

Entering Text

To start your work, simply start typing. To insert text before existing text, move the cursor to the appropriate place and type. Text to the right of the cursor will move over. Press Return or Enter at the end of a line to insert a new line after it. (Because emacs does not have word wrap by default, you should do this at the end of each line you type.)

To insert a new line before an existing line, press Return at the beginning of the line. To split a line in two, press Return in the middle of the line. To join two lines, move the cursor to the beginning of the lower line and press the Delete key to join it to the preceding line.

Deleting Text

You can use the Delete key or the Backspace key to delete the character that appears before the cursor. Additional delete functions are:

CTRL-d delete the character under the cursor
CTRL-k delete from the cursor position to the end of line
ESC k delete to the end of the current sentence
Note that you hold down the CTRL key while pressing the next character, but you press and release the ESC key before pressing the next character.

Moving Around in the File

You can move the cursor by using the four arrow keys on the keyboard. To scroll up and down, you can use the PageUp and PageDown keys. In addition, the Home and End keys move to the beginning of the file and the end of the file, respectively. If you are logged in remotely, however, you may find some of these keys don't work. You can always use the following key combinations:

CTRL-f move cursor forward one character   CTRL-v move forward one screen
CTRL-b move cursor backward one character   ESC v move backward one screen
CTRL-p move cursor to previous line   ESC < move to top of file
CTRL-n move cursor to next line   ESC > move to end of file

Undoing Mistakes (CTRL-x u)

To undo the command you just issued, press CTRL-x u.

Canceling a Command (CTRL-g)

If you started a command you don't want to finish, mistype a command, or otherwise get confused, you can bail out by pressing CTRL-g. Sometimes, depending on the trouble you're in, you may need to press it more than once.

Searching for Text (CTRL-s and CTRL-r)

Press CTRL-s to start a search. After the cursor moves to the bottom of the screen, type the text you want to search for. (Case is ignored.) Emacs will start searching as soon as you type the first character. To search for the next occurrence, type CTRL-s again. To stop searching, press the Enter/Return key. To search backwards in the file, use CTRL-r instead. Remote users on some systems may find that CTRL-s is used by communications software to control data flow. (If you find that pressing CTRL-s freezes your session, press CTRL-q to unfreeze it.) If you have this conflict, the easiest way around it is to use CTRL-r for searching. If you keep pressing it, the search will wrap around.

Replacing Text (ESC x replace-string)

To replace every occurrence of some text with new text, first move the cursor to the top of the file and press ESC x. When the cursor moves to the bottom of the screen, type:

replace-string (Press Return.)

Type the text to be replaced and press Return. Then type the new text and press Return.

Automatic Line Wrap (ESC x auto-fill-mode)

If you want to avoid pressing Return at the end of each line of text, you can instruct emacs to do it for you by turning on ``auto-fill-mode.'' First press ESC x, then, when the cursor moves to the bottom of the screen, type

auto-fill-mode (Press Return.)

This is a toggle command: if you do this again, it turns off auto-fill-mode.

Online Help in emacs

These are only a few of the emacs commands. The table below shows some ways to get online help.

CTRL-h t starts the online tutorial. (Press CTRL-x CTRL-c to quit tutorial and emacs.)
CTRL-h b displays a list of basic commands with their respective key sequences.
CTRL-h a prompts you for a word about which you'd like help. (Try entering undo.)
CTRL-x 1 closes help window (1 is the number one). Be sure the cursor is in your text window when you do this.
Note that on RCS, instead of typing CTRL-h, you can also enter help mode by typing ESC x help, followed by Return. This option may be helpful for remote use on systems where CTRL-h is set to perform the backspace function.

Exiting emacs and Saving a File (CTRL-x CTRL-c)

To leave emacs, press CTRL-x CTRL-c. Emacs will ask whether or not you want to save the file. Enter yes or no. On a workstation, you can save changes without leaving emacs by pressing CTRL-x CTRL-s. Remote users might have a conflict with CTRL-s, as noted above.


About this document ...

Published by Academic and Research Computing, RPI, Troy, NY 12180

Send comments to consult@rpi.edu.