A SHORT Primer for EMACS for Busy Scientists

Curt Michel


Emacs is an word processing program (plus lots more) that has the advantage
that it can be run over a dumb terminal (i.e., a PC or MacIntosh with an
elementary terminal program).  Thus you can edit your files away from the office
(or you can try to use 'vi' if you ever get the 'termcap' to work right).  Emacs
also has some very nice features such as keeping several files in buffers so
that you can view one while editing another, etc.  It also has editing modes for
latex, tex, C, etc. with features both for eliminating common errors and for
compiling the file in one window while editing (correcting) it in another.

A down side of emacs is that it is difficult to find out quickly how to do all
the STANDARD stuff that even the most elementary word processing program will do
in a trivial way.  Thus the minute you try to use emacs you suddenly cannot do a
lot of things you are used to being able to do.  This PRIMER is intended to
provide that knowledge.  Otherwise you waste a lot of time doing trivial tasks
with primitive tools, instead of finding out what great things you can do with
emacs.  Another down side of emacs is its obvious computer-nerdiness, which is
revealed in the excessive and pointless reliance on having to hit special
control keys numerous times.  There is no cure offered here.

Emacs has a simple tutorial which is advertized when 'emacs' is entered at the
prompt and RET(urn) is hit.  I will assume that the reader has taken the
tutorial, but I will be redundant in that I also want to remind the reader what
the standard actions are.

ENTERING TEXT: is at the cursor position - hitting a key enters at the position
of the cursor.  If the cursor is sitting on a character, the character is moved
to the right to make room.

DELETING TEXT: with the DEL key retains the character where the cursor is and
removes characters successively to the left, including newlines, so an entire
document could be removed by starting at the end and holding down DEL, which is
what I mean by doing trivial things with primitive tools.  If it's too much
trouble to move the cursor, C-d (control d) deletes what is at the cursor.
Useful for deleting the newline at the end of every sentence.

I DIDN'T MEAN TO DO IT: 'C-x u' undoes the last command, and may be envoked
repeatedly to undo successively earlier commands.

Ninety percent of editing can be done with these primitives, but the remaining
ten percent can become ninety percent if that's all you have.

FINDING TEXT: typically one is proofing text and wants to go to a specific point
in the document, most easily by searching for a nearby distinctive word.  To do
this, enter C-s (control s) and 'I-search:' appears in the bottom line
("minibuffer").  As you type in the word or words to be searched, emacs searches
DOWN through the file, so you may get to the word without typing it completely.
If you are starting too low, you get the "helpful" message 'Failing I-search:'
but don't give up, just hit C-s again and it will start again from the top of
the file, with the message 'Wrapped I-search:'.  If you pick a common word,
repeated C-s commands will step down through the file.  If you seem to be stuck
in 'search' just click in the file window.

DELETING/PASTING TEXT: now that you found it, you probably want to delete
something.  Emacs has all sorts of goofy 'delete' options not worth mentioning.
Almost always you want to simply delete everything between HERE and THERE.  You
mark the begining with the cursor (including the first symbol to be deleted) and
type C-SPC (control spacebar) and then move the cursor to the last character to
be deleted and type C-w.  The text vanishes and is stored.  If your intention is
to move the text somewhere else, you now simply move the cursor to the new point
and type C-y.  If your intention is to COPY the text, you mark it and type C-w
C-y (which briefly deletes it) and then move to where you want to PASTE it.

REPETITIVE OPERATIONS: often you need to do a lot of the same thing.

One repetitive operation is to change existing text to something else.  If I
want to change 'Truman' to 'Clinton' I can type 'M-x replace-string' where M-x
is 'meta-character x'(yes, they want you to use TWO control keys).  On the Sun
workstations it is the black diamond key on either side of the spacebar
(if no such key, try Esc, and if that fails, try C-[ ); you
hold down the black diamond and hit x, but now you must literally type in
'replace-string'.  Since this is so brain-dead, emacs's creators have made the
spacebar an "accelerator" so that you can type in just part of a command and hit
spacebar and the rest of the word will be completed.  But if you type in 'M-x r'
and hit spacebar, emacs will display all known such commands that start out this
way whenever there are more than one.  You will need to type at least 'M-x repl'
and the spacebar will give you 'M-x replace-' so you still need to type 's' and
then spacebar gives you FINALLY 'M-x replace-string'.  You now need to do one
more brain-dead thing, namely hit RET to have displayed: 'Replace string:' and
NOW you can type in 'Truman', hit RET, and you now have 'M-x Replace string
Truman with:' and the rest is finally obvious.  BUT the replacement is from the
cursor to the end of file (it makes no sense, I know), so you have to go to the
beginning of the file with 'Shift M-<' (the SHIFT is to get '<' instead of ',')
BEFORE you make EACH substitution.  Emacs leaves you at the last point in the
file that it made a substitution, so you almost always have to back up to the
beginning of the file. (A general command to get out of garbled commands is
C-g).  Also, the minibuffer will smugly display 'Done' even if no substitution
was made!  A useful shortcut is the command 'SHIFT M-%', which gives you
'Query replace:' which works as above except it stops at every word to be
replaced and waits for you to type 'y' or 'n'.

A related command is 'M-x replace-regexp' which is useful if you need to move to
special places and then do something ('regexp' stands for REGular EXPression).
For example, to comment out a lot of text in a LaTeX document, you'd like to add
'%' to the beginning of each line.  This command is just like the one above
except enter 'r' instead of 's' at the appropriate point, and you get 'Replace
regexp:'.  What is different here is that this command will accept the special
characters telling it to find patterns other than simply word spellings.  So
here you can enter '^' which means "begnning of line" and hit RET and enter '%'
and ALL the lines below the cursor will be commented out.  Since this may be too
many, move to the end of the commented text, and with the same command find
'^%', and replace it with nothing.  (The special characters only apply to the
search, so you don't need to replace ^ with ^% unless you literally WANT ^% at
the beginning of each sentence!)  Cleally one needs a manual to consult for this
and emacs has one on-line; to get the manual type 'C-h i' (help-info) and move
the cursor down to the line starting "* Emacs: (emacs),..." and hit return.  Now
scroll down to "*syntax of regular expressions" (just hold down the 'down arrow'
key) and hit return.  There you are, more than you ever wanted to know!  But
this exercise shows you how to find out about all the fabulous features.  If you
had trouble finding the above section, type 'd' and then type 'h' for a tutorial
on how to use the manual. (A better command for the above example is 'M-x
query-replace-regexp' which instead of going to the front of every line and
putting a % there, simply goes to the front of the next line and sits there.  It
is waiting for you to type either 'y' or 'n' or quit out of the command (C-g).)

Often you may want to enter text that is repetitive, but it's not enough typing
to make it worth cutting and pasting.  For example, the '\begin{equation}' and
'\end{equation}' lines in LaTex.  That's a lot of pointless typing.  What you
can do here is store these lines in registers.  All we have to do is mark one of
these as before (move cursor, C-SPC, move cursor) and type 'C-x x b' and you
will get 'Copy to register:' BEFORE you hit the 'b', afterwards the line in the
minibuffer vanishes.  I just made up the 'b' as a mnemonic for "begin", any
SINGLE key will work - if you type more they appear in the file as text because
emacs has already quit the command.  If you marked '\begin{equation}' and put it
in the 'b' register, all you have to do is move the cursor to the desired
insertion point and type 'C-x g b' and that text is inserted.  (Plan ahead, you
can't move the cursor between typing the command and RET.)  Actually a better
solution in this case would be to use abbreviations (see manual), but until
then...

Finally (here) commands can be invoked numerous times by typing M-4 (to be
specific:4 times, any number can be used) and then the command, but the only
likely one we have so far is 'C-x u'.  A possibly useful one is 'C-x TAB' where
first you mark the text and then each invocation of the command indents the
marked text by one space (notice that entering the number is NOT M-x 4, but
M-4).  (No reason to be consistent.)

LOCATING WITHIN DOCUMENT: the registers provide a second function of remembering
locations. The command 'C-x / b' displays 'Point to register:' before you hit
the 'b' and then vanishes (notice that's the old slash, not the backslash).
Later we can type 'C-x j b' and we are right back there at the same point in the
file.

Another standard location problem is given by programs that list errors by their
line number, like LaTeX.  To move to a line number, type 'M-x goto-l' (lower
case L) and spacebar will complete it to 'M-x goto-line' and you now need RET to
actually get the command 'Goto line:' which we can now can type in.

WINDOW MANAGEMENT: 'C-x 2' opens two windows containing the same file, so you
can read what you said one place while writing another, 'C-x 1' removes the
window you are not working in. 'C-x C-f' puts what you are working on in a
buffer and loads the file you are queried for in the minibuffer.  If you want to
load the file as a separate window while keeping the file you are working on,
type 'C-x 4 C-f' (don't ask, I don't know).  If you forget and load a file that
is already in a buffer, emacs will use the one in the buffer and not the
(probably unedited) one in your directory.  'C-x C-s' then makes the two the
same (i.e., saves to disk).  The easy way then to get a file from buffer into a
second window is split the window (C-x 2) and then pretend you are asking for
the version on disk (C-x C-f).

SPELL CHECKING: for every useful function, emacs has a dozen goofball variants,
so you can check the spelling of a word, region, etc.  To check the spelling of
a single word, move the cursor to the space at the end of the word and type
'M-$' ("midle" produces a self-explanitory list of actions in English, plus the
choices "0 idle", "1 mile", and "2 middle"; type '2' and the misspelling is
corrected).  To spell the whole document (the usual use), type 'M-x spell-b'
spacebar to get 'M-x spell-buffer' and it will go through the words it doesn't
like (which often seem perfectly good: the spell checker didn't like "REGular"
as used in this document (of course), but complained instead about "regular").
Unfortunately, only if you edit the word will emacs go to the place in the
document where it is used, which makes it difficult to puzzle out what you meant
sometimes.  And you have to hit 'y' for the edit to take effect once the word is
located, RET just skips on.  Use 'Texspell' for LaTeX documents unless you want
things like '\frac' listed.  To do this, type 'M-x shell' which will pop up the
Unix command line, and after running Texspell you hit 'C-x b' and RET to get
back to the file.

ADVANCED FEATURES: the above seem to be the more-or-less standard things one
would like to do with files.  Emacs lets you pop out to the command line and
compile the appropriate files.  For example, if you are editing a LaTeX document
with a '.tex' extension, emacs will assume you are in TEX mode, so you need to
type 'M-x latex-mode' to manually correct it (or use the extension '.ltx'
instead).  The mode is listed in the reverse-video line at the bottom, which
usually says 'Fundamental'.  Now you can type 'C-c C-b' and a second window will
pop up in which the compilation will be shown.  To get rid of it, type 'C-x 1',
making sure the cursor has marked something in the window you want to
keep. Actually 'C-c C-f' also works and you will occasionally envoke it when
fumbling to type 'C-x C-f' which asks for a new file name to edit.

To pop up another file, type 'C-x C-b' to see what's available, then 'C-x b' to
enter the name of the desired buffer or C-x C-f to add another file to the
emacs.

If you are editing Tables, look up "*rectangles" in the manual (if you haven't
exited emacs, the manual will be stored in a buffer called *info*) - you can move
around columns of text ('rectangles') just like entire lines of text.  You mark
the upper left with C-SPC and move the cursor to the lower right.  Not useful
for text, but I copied the third paragraph above with C-w C-y, and then chopped
out the center by marking the colon and the period at the end to produce ('M-x
clear-rectangle'):

ADVANCED FEATURES						  things one
would like to do						 nd line and
compile the appro						  LaTeX document
with a '.tex' ext						 o you need to
type 'M-x latex-m						 n '.ltx'
instead).  The mo						 ttom, which
usually says 'Fun						 ond window will
pop up in which t						  type 'C-x 1',
making sure the c						 t to
keep. Actually 'C						 oke it when
fumbling to type						 .

COMMAND SUMMARY: (not all discussed above) rare mnemonic noted
C-h i			get the manual
C-d			(d)elete character at cursor
C-spacebar		mark text from present cursor position to next position
C-w			CUT marked text
C-y			PASTE in CUT text
C-x u			(u)ndo
C-g			escape command mode
C-x C-f 		load new file
C-x C-w                 copy existing file to a new name and edit that
C-x 4 C-f		load new file into second window
C-x C-s 		(s)ave file
C-x C-c			leave emacs
C-u 47 C-x f		set line length to 47 (nothing will happen to existing text)
M-q			now changes line length to 47 in paragraph containing cursor
C-s			(s)earch
C-x C-b			list buffers (other files that have been loaded)(splits screen)
C-x b			load buffer by its file name
C-x x r			store maked text in register "r"
C-x g r			(g)et text stored in register "r"
C-x / r			store cursor position in file in register "r"
C-x j r			(j)ump cursor to position stored in register "r"
		cannot store BOTH text and position in same register name
C-x 2			splits window in (2)
C-x 1			returns to (1) window, deleting inactive one
C-c C-b			compile in second window
C-c C-f			"	"	"
C-x TAB			indents marked lines of text one space
M-x replace-string	replace string
M-x replace-regexp	replace regular expression
M-x latex-mode		changes mode to latex (for example)
M-x auto-fill-mode	wraps lines to keep from running off screen
M-x goto-line		find line by number
M-x spell-buffer	spell check file
M-x shell		escape to command line (C-x b RET returns to where you were)
SHIFT M-$		spell check word just written
SHIFT M-%		replace string (asks permission each instance)
Shift M-<		go to top of file
Shift M->		go to bottom of file