EMACS
The following topics are discussed in this section:
GENERAL INFORMATION
The emacs editor (version 19.17.1) is a sophisticated editor written in LISP by Richard Stallman. This
editor is also known as GNU emacs or gnuemacs. This editor includes such features as: on-line help, easy text
entry, search and replace operations, automatic justification, special C and LISP modes, and the ability to run and
capture AIX shells in a window. The emacs editor works on the idea of editing buffers. This means all text
(including a text file) is loaded into a buffer by emacs. All editing then occurs to that buffer. As a safety
precaution, emacs automatically saves your buffer to a backup file after a specific amount of time.
GETTING STARTED
The syntax for the emacs command is:
emacs [+number] [-q] [-u user] [filename]
where number is the line number on which to start the display, q means do not load the init
file, u loads user's init file, and filename is the name of the file to be edited or
created.
The emacs editor is split into two areas. The top area is where you edit the buffers (your text), and the bottom area
is the echo area. The echo area displays the commands you issue, as well as any prompts or messages from emacs.
You can abort a command by pressing the [CTRL][G] key combination.
The text area and the echo area are separated by the mode line. This line shows the current status information of
emacs. The emacs editor will place two asterisks (**) in front of the buffer name if changes have been made, but
not yet saved. Two percent signs (%%) will appear if the file is read-only. Next to the buffer name will be the
major mode of the buffer. These modes control the way emacs behaves with your text and consist of the modes
text, lisp, and c. Next to the major mode will be a list of the minor options that can be altered. Next to this will be
a display showing a percentage of the file displayed.
USING EMACS
Most emacs commands are based on the use of the [CTRL] key or the meta key ( [ESC] ). The [CTRL]
key should be held down while the next key is pressed. The meta key should be pressed and then released before
entering the next key sequence. The [CTRL] key should only be held for the next character only. For example, the
command [CTRL][X] [U] means hold down the [CTRL] key and press [X] and then release the [CTRL] key and
press [U] . Some commands do not have key sequences assigned to them, and must be entered by hand. If you
wish to cancel a command, issue the [CTRL][G] key sequence. This will return you back to the text window so
that you can edit your buffer.
Quitting emacs
There are several ways to leave the emacs editor and return to your shell. The first is by issuing a
[CTRL][X] [CTRL][C] command. This will quit emacs after asking if you wish to save any of the altered
buffers. If you plan on doing more editing, you may wish to suspend the emacs process and return to the shell.
This can be done by issuing the [CTRL][Z] command. You can then return to emacs by issuing a
fg %emacs command at the OS prompt. If you use [CTRL][Z] to return to the OS prompt, you must
remember to use the [CTRL][X] [CTRL][C] command to quit emacs, or you will be unable to logout
because you have a suspended process.
Moving the Cursor
When moving the cursor, emacs restricts its position to within the text boundaries. This means that if one line
is longer then the next, the cursor will go to the end of the shorter line instead of staying in the same column.
Cursor commands can be repeated by pressing [CTRL][U] , followed by the number of times to execute the
command, and then the command. For instance, to move up 5 lines you would type [ESC] [5] [CTRL][P] . The
exception to this rule are the page commands. They move the cursor the specified number of lines, not the number
of pages.
The table below shows some of the basic cursor movement commands within emacs.
Key Action Key Action
------------------------------------------------------------------------------
[CTRL][B] Back one character [CTRL][F] Forward one character
[ESC] [B] Back one word [ESC] [F] Forward one word
[CTRL][P] Up one line of text [CTRL][N] Down one line of text
[ESC] [V] Up one page of text [CTRL][V] Down one page of text
[CTRL][A] Beginning of line [CTRL][E] End of line
[ESC] [A] Beginning of sentence [ESC] [E] End of sentence
[ESC] [>] End of file [ESC] [<] Beginning of file
Adding and Deleting Text
Unlike vi, emacs is always in insert mode. Therefore, you can insert text simply by placing the cursor at the
desired location and start typing. You can insert a new line or split a line into two lines by pressing the [ENTER]
key at the desired location. The rubout key varies on your terminal. On most terminals, it is a
[CTRL][Backspace], but some may use the [Backspace] key alone. Text that is removed by one of the "kill"
commands can be placed back into the document via the "yank" commands. This is a useful way of performing cut
and paste operations. Below is a table showing some of the more useful editing commands.
Key Action Key Action
------------------------------------------------------------------------------
rubout Delete character before cursor [CTRL][D] Delete the character after the cursor
[ESC]rubout Kill the word before the cursor [ESC] [D] Kill the word after the cursor
[CTRL][K] Kill from cursor to end of line [ESC] [K] Kill to end of current sentence
[CTRL][Y] Yank previous text [ESC] [Y] Yank next previous text
Undoing a Change
The emacs editor provides an undo function so you can erase a change you made to your text. To undo a
change to the buffer, use the [CTRL][X] [U] command. Each time you use the undo command, the previous
change will be undone. As an alternative, you can type [CTRL][-] to execute the undo command.
Loading and Saving Files
You can load a file into an edit buffer by issuing the [CTRL][X] [CTRL][F] command. The emacs editor
will then wait for you to enter the name of the file to load into the buffer. When the file is loaded, your cursor will
be placed in the text area and you will be able to edit your text. If you specify a file that does not exists, emacs will
assume you are creating a new file. If the file is already in a buffer, emacs will display that buffer in the text
area.
To see a list of all the buffers loaded, you can use the [CTRL][X] [CTRL][B] command. This will open a
window on the bottom half of the screen showing information about the various buffers in emacs. You can then use
the [CTRL][X] [CTRL][F] command to make the specified buffer active. You can remove the buffer window by
pressing [CTRL][X] [1] .
When you wish to save your text, you can issue the [CTRL][X] [CTRL][S] command. If the file you are saving
already exists, the old file will be renamed with a tilde (~) at the end of the name. Until you save your changes, they
are not made to the original file. Remember that you are editing in a buffer that contains a copy of the file, not the
actual text in the file. If you have several buffers open, only the active buffer is affected by [CTRL][X] [CTRL]
[S] . If you wish to save a buffer which is not the active buffer, issue the [CTRL][X] [S] command. This will
cause emacs to go through the list of buffers in memory and ask for each one if you wish to save it.
Using Multiple Windows
It is possible within emacs to have several buffers displayed on the screen at the same time. Each buffer will be
displayed in its own window. When you invoked the help command, a second window was opened to display the
contents of the help buffer. There are two ways that you can divide the current window into two windows. The first
is the [CTRL][X] [2] command which splits the window horizontally. To split the widow vertically, use the
[CTRL][X] [5] . The picture to the right shows the same file split into three windows. You can switch between
windows by pressing [CTRL][X] [O] . Splitting a window is useful for cutting and pasting within a document.
When you are done with a window you can close it by making it the active window and pressing [CTRL][X] [0] .
If you press [CTRL][X] [1] , emacs will close all windows but the active one. You can also increase the size of a
window horizontally or vertically by using the [CTRL][X] [}] and [CTRL] [X] [^] respectively.
Using Multiple Buffers
By creating different buffers, you can edit several different files at the same time. You can create a new buffer
by issuing the command [CTRL][X] [B] followed by the name of the buffer. This command will also switch to the
buffer with that name if it already exists. To kill a buffer, use the [CTRL][X] [K] command followed by the name
of the buffer to kill. As mentioned earlier, [CTRL][X] [CTRL][F] will load the specified file into a new buffer
and make that buffer the active window. The [CTRL][X] [CTRL][B] command can be used to display a window
showing all the current buffers.
To open a buffer in a new window, use the [CTRL][X] [4] [B] command followed by the buffer name. Specifying
an [F] instead of a [B] will load the specified file into a new window.
Accessing the On-line Help
The emacs editor comes with on-line help and a tutorial to help you use the editor. You can invoke the on-line
help by pressing [CTRL][H] . Now, emacs is waiting for you to specify what type of help you need. If you wish
to run the tutorial, press [T] . Pressing [A] followed by a string, will display all commands that contain string in
their name. You can display a list of all key commands by pressing [B] . Help for a particular key command can be
obtained by pressing [K] followed by the desired key command. For more help, you can press [CTRL][H] again.
Pressing [M] will display information about your current major mode.
Searching and Replacing
Facilities are provided for easily searching for and replacing text in the buffer. Pressing [CTRL][S] and then
typing the string for which to search, will cause emacs to place the cursor at the first occurrence of the string after
the current cursor position. As you enter the string, you will notice that emacs moves to the first occurrence as you
type. If the occurrence can not be found, emacs will display a message at the bottom of the screen that says:
Failing I-search: string
where string is the string for which you were searching. Pressing [CTRL][S] again will cause
emacs to search for the next occurrence of the string. You can also search backwards by using the [CTRL][R]
command in place of the [CTRL][S] command.
There are several ways to perform a search and replace operation in emacs. The easiest is to use the query-replace
command. To use this command, issue the key sequence [ESC] [%], the string to be replaced, press [ENTER] , the
new replacement string, and [ENTER] again. For example, to replace the next occurrence of the word UNIX with
AIX you would type:
[ESC] [%] UNIX [ENTER] AIX [ENTER]
When emacs finds the next occurrence, it will prompt for your next move. Pressing the spacebar will tell emacs to
make the change and advance to the next occurrence. Pressing the delete key will cause emacs to skip this change
and advance to the next occurrence. The [ESC] key will exit the query and replace command. Entering an
exclamation point at the prompt will cause emacs to replace all remaining occurrences. The caret (^) can be used to
backup to the previous occurrence and [CTRL][H] will bring up help on the subject.
HELPFUL HINTS
Anytime you wish to quit the execution of a command, enter the [CTRL][G] command.
You can customize emacs by creating a .emacs file in your home directory.
QUESTIONS/ANSWERS
Q. How can I make emacs be in type-over mode instead of insert mode?
A. Enter the command [ESC] [X] overwrite-mode.
Q. How do I make emacs automatically indent to the beginning of the previous line?
A. Enter the command [ESC] [X] indented-text-mode.
REFERENCES/MANUALS
Your local bookstore should have books explaining the use of emacs.
Certain anonymous ftp sites may carry the 300 page emacs manual. You can also purchase the manual directly
from the Free Software Foundation.
(c) Copyright UCF Computer Services I& R Support 08/17/93