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

  1. What is Emacs?
  2. Why Emacs?
  3. Basic Text Editing
  4. Working With Files
  5. Working With Buffers and Windows
  6. What Else Will Emacs Do?
  7. How to Set Up Other Programs to Use Emacs As An Editor
  8. Emacs Philosophy
  9. Emacs Internet Resources

1. What is Emacs

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.

2. Why Emacs?

Emacs has two major advantages over just about every other text editor available at Keele.

  1. 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
  2. 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 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.

3. Editing Basics

Starting Emacs

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 emacssuch 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 tincan 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 :-
  1. Undo: To undo the last thing you did type "C-x u"
  2. Abort: To abort the present process, such as being asked impertinent questions, type "C-g" (repeatedly if necessary).

Conventions

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".

Moving About

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

Cutting and Pasting

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.

  1. Move the point to the beginning of the section you wish to delete.
  2. Press "C-space" to set the mark there.
  3. 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.
  4. 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:-
  1. Move the point to the beginning of the section you wish to delete.
  2. Press "C-space" to set the mark there.
  3. 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.
  4. 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".

Quitting Emacs

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.

4. Working With Files

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).

5. Working with Buffers and Windows

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.

6. What Else Will Emacs Do?

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

7. Setting Up Other Programs To Use Emacs

8. Emacs Philosophy

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 :)

9. Internet Resources

Help and Advice

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.

Where To Get Emacs

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.

  1. DOS
  2. 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