An Emacs Primer
Preamble
If you use email, do any programming, use TeX and LaTeX to typeset an
essay or thesis, or design a web page you're going to want to use a
text editor. By default, the computer centre supports the use of an
editor by the name of joe
or Joe's Own Editor.
However, joe
is not the only text editor available on
potter
but is almost certainly the simplest to get used
to (mainly because of the fact that, by default, all the key strokes
are shown to you). It is however, the least useful.
There are two far more flexible text editors available,
vi
and emacs
. This document will try to
introduce you gently to emacs. Emacs has a built-in tutorial which
this short document is not intended to replace, and you are strongly
advised to follow it (it doesn't take long). To start the tutorial,
after starting emacs press "C-h t". If this doesn't mean anything
to you, keep reading this and all will hopefully become clear.
Index
- What is Emacs?
- Why Emacs?
- Basic Text Editing
- Working With Files
- Working With Buffers and Windows
- What Else Will Emacs Do?
- How to Set Up Other Programs to Use
Emacs As An Editor
- Emacs Philosophy
- Emacs Internet Resources
Emacs is the most
powerful text editor available. More accurately, Emacs is a LISP
interpreter that pretends to be the most powerful text editor
available. If you really want to know where Emacs came from and how
it came to be, you are referred to the GNU Emacs FAQ
list. That document, which is much more thorough than this brief
overview, will provide you with all the information you'll ever need,
and is available as part of emacs' extensive online help.
Emacs has two major
advantages over just about every other text editor available at Keele.
- It is a fully featured LISP interpreter.
What this means is that it can be extended, modified, altered
reprogrammed and generally messed about with in any way your heart
desires, assuming you can bluff your way in LISP. Common uses of this
include
- Redefining keys to perform any function built-in to emacs
- Creating your own custom functions.
Anyone who has used
macros in Microsoft Office applications will appreciate the
power and flexibility of this.
- It runs in modes which understand what you're doing
If you're writing in a programming or markup language (or Fortran,
Ada, Pascal, C, TeX, LaTeX etc...) Emacs will - Check you're
code/markup for common errors (mismatched brackets etc)
-
Change the colour of the text to show the context
(e.g. comments in red, strings in brown) which enables you to
see at a glance if you've failed to close some quotes or
comment delimiters.
If you wish to view files on remote machines, delete and rename files
in a more flexible way than is
In addition to this, it contains extremely comprehensive online
help, a complete manual and a tutorial to get you started.
Emacs is started at the shell prompt, which will usually look something
like this:
potter%
At this prompt type "emacs
" and press the enter key. (Don't
type the quote marks.) Emacs will now start. If you type the name of
the file after emacs
such as "emacs my-file
" you
will be presented with the contents of the file.
Alternatively, many programs such as the email program elm
and the news-reader tin
can be set up to start emacs whenever
you need to edit an email or news posting. See the section on other programs
The Panic Buttons
If, at any time, you start emacs doing things you didn't want (asking
questions you don't understand, deleting bits of text you were quite
fond of etc..) there are to things to do :-
- Undo: To undo the last thing you did type "C-x u"
- Abort: To abort the present process, such as being asked
impertinent questions, type "C-g" (repeatedly if necessary).
Emacs has a number of conventions about describing key presses which
it would be best to describe here. In the table below, the "x" can
stand for any key.
Conventions For Key Strokes In Emacs
Notation | Meaning
|
---|
C-x | Hold down "Control" and press "x"
|
M-x | Press "Escape" and then "x"
|
ESC x | Press "Escape" and then "x"
|
RET | Press "Return" or "Enter"
|
On a X terminal or a dedicated UNIX workstation, M-x can also be
generated by holding down the "Alt" or "Meta" key and pressing "x".
Any thing that appears in emacs is a buffer. In particular, the
contents of a file you are editing is a buffer. When Emacs starts,
you will be presented with either an empty buffer waiting for content
or a buffer containing the contents of the file that you are editing.
In that buffer will be a cursor, known as the point.If type a
letter, that letter will be inserted into the buffer at the point,
much like every other text editor, (except vi, which will probably
just beep at you).
With luck, the arrow keys, on your keyboard will move the point around
the buffer a character at a time, and holding control and pressing the
arrow keys will move the point by a single word. Similarly, the "Page
Up" and "Page Down" keys should behave intuitively. Unfortunately, for
reasons lost in this mist of time, some terminals don't do this
correctly, and you need other keystrokes, which are tabulated
below.
Keystroke | Moves point..
|
---|
C-p | Up one line
|
C-n | Down on line
|
C-f | Forward one character
|
C-b | Back one character
|
C-v | Down one screen (Page Down)
|
M-v | Up one screen (Page Up)
|
In addition to these standard keys, there are keys for moving the
point by larger units of text.
Keystroke | Moves point..
|
---|
C-a | Beginning Of Line
|
C-e | End Of Line
|
M-} | Forward One Paragraph
|
M-{ | Backward One Paragraph
|
In addition to the point (the cursor) emacs remembers another place in
the file known as the mark. When "C-space" is pressed the mark
is set at the present position of the point. The area of text between
the point and the mark is known as the region.
Like all text editors emacs enables you to easily delete sections of
text and re-insert them else where, and the region is often used for
this purpose. In emacs terminology this is known as killing
and yanking text.
These are the commonest keystrokes for killing and yanking text.
Keystroke | Function
|
---|
C-space | Set Mark
|
C-k | Kill Line
|
C-w | Kill Region
|
C-y | Yank Last Killed Text
|
So say you wish to cut out a large section of text in your document
you would perform the following steps.
- Move the point to the beginning of the section you wish to delete.
- Press "C-space" to set the mark there.
- Move the point to the end of the section you wish to delete
The region (the section between the point and mark) is now the section
you wish to delete.
- Press "C-y" which kills the region. Done.
If you wish to now re-insert the the killed text somewhere else, move the
point to where you want the text and press "C-y". The kill and yank functions
are actually more flexible than most comparable "cut and paste" features in
editors since, if after pressing "C-y" you press "M-y" the recently yanked
text will be replaced by the text killed previous to that one.
That feature (known as "yank popping") is handy. Registers are also handy.
If you want to insert the same bit of text in many places you can store it in
a place called a register. Here's how:-
- Move the point to the beginning of the section you wish to delete.
- Press "C-space" to set the mark there.
- Move the point to the end of the section you wish to store
The region (the section between the point and mark) is now the section
you wish to store.
- Press "C-x r s" and then a letter. The text will now be stored in the
register with the name of the letter you type.
You can insert the text in the register with named "letter" using "C-x r i"
and then the "letter".
Having edited your file, you will now wish to save the file, quit emacs and
get on with your life. To save the file, press "C-x C-s". If you wish to
save the file with a new name, press "C-x C-w". (A handy mnemonic for these is
s for `save' and w for `write').
Now, to quit emacs type "C-x C-c". If you have modified any files you have
not saved, you will now be prompted if you wish to save them and then emacs
will quit.
Whilst it is possible to use emacs to edit single files exclusively, most
people will want to edit multiple files at one time or another. To open a
file in a currently running emacs session type "C-x C-f" and, when prompted,
type the name of the file you wish to edit. A new buffer will be created with
the contents of the file in it, ready for you to edit. When you are done with
a file, save it and kill the buffer (see below).
Each file you are presently editing exists as a buffer, and the file on disk
is not changed until you save that buffer with "C-x C-s" or "C-x C-w". If you
have more than one buffer open at a time you can switch between with "C-x b"
which will prompt you for the name of the buffer you wish to view.
Alternatively, you can split your screen into a number of windows and view
different buffers, or different parts of the same buffer, at the same time.
Emacs will often split the screen into multiple windows (e.g. when giving
help.) Here are the most important keys to use multiple windows.
Key | Function
|
---|
C-x 0 | Close this Window
|
C-x 1 | Close all Windows except this one
|
C-x 2 | Split Window Horizontally
|
C-x 3 | Split Window Vertically
|
C-x o | Move the point (cursor) to the next window
|
Note that closing a window does not close the buffer that the window
is displaying, you can still switch to it with "C-x b"
To kill a buffer, make sure the point is in that buffer and type "C-x
k". If the buffer is a file that been changed but the changes not
saved you will be asked to confirm this. Type "yes" to confirm. This
is the best way to abandon any changes you have made but thought
better of.
Just about everything, or at least too much to cover here, so here are
some commands to look for in the online manual ("C-h i" then follow the
instructions)
Topic | Command Name
|
---|
Moving and renaming files | dired
|
Editing remote files and ftp | angeftp
|
Reading and replying to email | RMAIL
|
Reading and posting UseNet news | gnus
|
Finding text | isearch-forward
|
Replacing text | query-replace
|
Psychoanalysis | doctor
|
- Elm
Elm is probably the most commonly used email program on potter
and can be easily configured to use emacs as it's editor
- After elm has started, when the index is on the
screen, press "o" to get to the options screen.
- Press "e" to edit the `Primary Editor' line. Delete
what is there at the moment and in it's place type
"emacs" and press "enter".
- Press the ">" key to save your new preferences.
- Press "q" to return to the index. Done.
- Tin
Tin, along with Netscape, is the most popular Usenet
Newsreader in use at Keele. It is less easy change the
default editor for tin :( and it is possible to get in a
terrible mess since it is necessary to edit one of your
startup files. For the brave then, here is how to do it,
assuming you are using potter and haven't changed your shell.
(If you don't know what this means, you haven't done it) :-
- Backup your .cshrc.solaris file. This is really
important, as you'll need the backup if it goes wrong. To do
this enter these commands at the
potter%
prompt
cp .cshrc.solaris cshrc.bak
and then
chmod 777 .cshrc.solaris
- Now start emacs to edit this file, like so
emacs .cshrc.solaris
- Move the point to the end of the file and, on a new line
type
setenv EDITOR emacs
and make sure there is at least one blank line underneath this
line.
- Save the file and quit emacs. ("C-x C-s" then "C-x C-c",
remember). The next time you log in, emacs will be the default
editor for tin (and many other programs besides.)
If however, next time you log in Bad Stuff Happens,
which stops you using your account, you have corrupted your
.cshrc.solaris
file. This is unlikely, but
might happen if you really screw up, so here's how to fix it
(assuming you performed step (i) above)
Grab a friend and ask them to log on to their account and
enter the commands for you.
rm ~< your user name >/.cshrc.solaris
cp ~< your user name >/cshrc.bak .cshrc.solaris
where instead of < your user name >
type your
user name (the name you use to log in to potter). This will
restore you old startup file, and everything will be fine.
[Disclaimer: I take no responsibility for your actions.]
If you do get in a mess, you could try to
email me
Feel free to skip this if you're not remotely interested :)
Part of emacs' flexibility comes from it's fundamental structure.
Emacs commands are all LISP functions, and every keystroke simply runs
one of those commands. Pressing a letter key, runs the command
called `self-insert-command' which inserts the letter pressed.
Pressing "C-x C-c" runs the command called `save-buffers-kill-emacs'
and so on...
Any emacs command can be run by pressing "M-x" and then typing the
command's name. This is not terribly efficient but is the only way to
run many of the more obscure commands such as `insert-zippyism' or
`doctor-strangelove', and also some useful ones :)
The GNU Emacs reference card is available as a
PostScript document which can be printed out, or a TeX file and contains all of the most common
keystrokes and function names, and many others.
Be aware that some browsers (notably lynx) will sometimes
automatically print the PostScript file if you click on the link.
There are a number of UseNet news groups which discuss various emacsen
and where emacs gurus will gladly help you, provided your question is
not in the FAQ or in the online manual (press "C-h
i" whilst in emacs to read this.)
You may wish to try these first :-
For the less serious minded there is also
alt.religion.emacs
You should, however, the each of the newsgroups FAQ lists (which are
frequently posted both there and on
news.answers to be sure your question is relevant to the group
As a final resource the book Learning GNU Emacs published by
O'Reilly and Associates covers everything you'll need to know, from
basic editing through reading and sending email to writing your own
custom LISP functions, and is available in Crewe library.
Emacs is free software. It is freely available from many ftp sites on
the Internet as pre-built programs or source code for a number of
platforms. For further information click on the platform you're
interested in and take Emacs home today...
It is quite big, (about 5 Megabytes for the DOS executables) though.
- DOS
- Linux
and other Unices
Praise for this page:
"quite helpful" -- Ian Marlier (thanks Ian).
If you have any suggestions or comments about anything Send Me Some Email
This document may be freely distributed, but probably won't be.
This document contains the opinions of Gareth Owen only.
No warranty or suitability for anything is implied.
Microsoft Office and Windows are trademarks of Microsoft.
Home
Mail Me