Emacs file editor
=============================================================================
Overview: Vi isn't the only editor in UNIX. Another extremely popular one
is emacs. This tutorial gives only the briefest hint of how to
get started with emacs.
=============================================================================
Section Topic
------- -----
Running emacs
Moving around, inserting and deleting text
Getting help, splitting the screen
Undoing mistakes, cutting and pasting
Searching and replacing
Stopping emacs
Running emacs
-------------
Emacs runs in two modes: either normal full-screen mode if you are using
a "dumb" terminal or in an augmented full-screen mode if you are using Open-
windows. The augmented version has pull-down menus and scroll bars like many
windows applications. You can still, however, use the function keys of emacs,
so only the normal full-screen mode will be discussed herein.
To start emacs, just type
% emacs filename
If the file doesn't exist, emacs will create it.
Unlike vi, emacs doesn't have the two separate modes of insert and command.
Instead, you are always in insert mode, and commands must be entered via special
keys like ESC or CONTROL. You also use the arrow keys to move around on the
screen instead of h,j,k and l as in vi. Remember that vi was written for termi-
nals that often didn't have special keys, not even arrow keys, so it had two
modes to be able to reuse the keyboard for either text input or commands.
In the terminology that follows, ^ stands for the CONTROL key and ESC for
the ESCape key. ^x means "while holding down the CONTROL key, press x".
ESC x means "press the ESCape key, release, and then press the x key".
Moving around, inserting and deleting text
------------------------------------------
As mentioned previously, you are always in insert mode, so just start
typing to put text into your file or insert it into an existing document. To
break a line, just move the cursor over to a character and press the RETURN key.
Similarly, you can delete text by using the delete key. This key rubs out
the character immediately before the cursor, moving all the text to the left.
To join two lines, press delete when the cursor is at the beginning of a line,
and it will join that line to the previous one.
Arrow keys move you around in the file. To immediately go to the begin-
ning of the file, press ESC <. Similarly, ESC > goes to the end of the file.
To move the cursor to the beginning of the current line, press ^a and to
move it to the end, press ^e.
There are many emacs commands, not many of which will be explained in
this short document. Each emacs command has both a long English-like name
and a short keystroke sequence. The latter can be reassigned to different
commands, and their assignment is called a key binding. To see what the
current key bindings are, type ^h b. That is, first press ^h (CONTROL-H)
and then type a "b". You do not need to press RETURN.
Getting help, splitting the screen
----------------------------------
The ^h b command to see the key bindings is a version of on-line emacs
help. Another one is ^h t which brings up the emacs tutorial.
Sometimes emacs splits your screen, as it does when you ask to see the
key bindings. To unsplit the screen, press ^x 1, which means "give me only
1 window".
In the case of the tutorial, it just replaces the current file. You can
return to editing your file by ^x b which switches "buffers". A buffer is
a chunk of text that you are editing. Your main file is one of the buffers,
as is the "kill" buffer where deleted text goes. To see what buffers exist,
do ^x ^b.
In general you can split the screen for the file you are editing, unlike
vi, and edit the two windows separately. This allows you to view one part of
the file while editing another part of it. To split the screen, do ^x 2,
which you can think of as "give me 2 windows". To unsplit, ^x 1. The cursor
is only ever in one of the windows, so you need to be able to reposition it,
which you can do with ^x o (other window). You can split ANY window into 2,
so you could have many windows on the screen simultaneously.
Undoing mistakes, cutting and pasting
-------------------------------------
If you make a mistake at some point, you can undo your last action with
^x u. If you type ^x u again, it undoes your second to the last action, and
so forth.
Cutting and pasting, or copying and pasting, is done by setting a mark and
moving the cursor to a new point. Emacs uses the term "point" to refer to the
character under the cursor. To set a mark, move the cursor to some point and
type ^@ (this is CONTROL-SHIFT-2). Then move your cursor either forward in
the file or backward.
Once at the new point, you can cut text using ^w which inserts the text
into the kill buffer. If you want to move the text elsewhere, move your cursor
to the next spot and press ^y (yank) which yanks it out of the kill buffer and
inserts it into the main buffer. To copy without cutting, use ESC w instead
of ^w.
Yank doesn't actually get rid of the text, so you can press ^y multiple
times to insert multiple copies of the text.
Searching and replacing
-----------------------
There are two directions to search: forward and backward. ^s is the
forward search while ^r (reverse) is the backward. After you press ^s, the
cursor jumps to the control line at the bottom of the screen. As you type in
your string, emacs nervously tries to "jump the gun", finding your text
as you type it in! When you are done, press ESC.
To search and replace, type ESC %. (Emacs calls this query replace.)
You will be prompted for the old string to search for, and the new replacement
string. Press RETURN after typing the old string, and RETURN after typing the
new. Then emacs searches for the old string and positions the cursor there,
waiting for your action. You can type "y" or the spacebar to have it replace
the old string, or "n" to not change this occurrence. Then emacs finds the
next one and does the whole thing over. If you want to quit early, before
emacs finds all occurrences in the file, press =. To go ahead and have emacs
silently change all, press the exclamation mark !.
Stopping emacs
--------------
If you want to write the buffer back to the disk file, use ^x ^s for
"save". This is like :w in vi.
When you are done, type ^x ^c to stop emacs. If you haven't saved, but
you did make changes, emacs will warn you and give you a chance to return to
editing. If you persist by typing "q", emacs will say that some buffers were
modified. "Go ahead and quit anyway?" To which you type "yes" and press
RETURN.