- Emacs -

Emacs is more than just an editing tool. It can also be used to read and send email, read and post news, manage your files and directories, and as a time management system. It also contains features designed for use by programmers. It would be impossible to adequately explain all of these features, so only the basic editing functions will be covered here, while the mail and news features will be covered in later tutorials. If you would like further information about the many other features of Emacs, a great book to read is Learning GNU Emacs by By Debra Cameron, Bill Rosenblatt & Eric Raymond.

Unlike vi, Emacs doesn't have a separate command mode, instead all commands are given using the CTRL and ESC keys.

NOTE: Entering text into the file, is just a mater of typing it in.

Opening a File

When you want to open a new file in a directory, give a new filename with the emacs command. For example, if you wanted to open a new file called filename in the current directory, you would enter

hostname:~bugsy> emacs filename
Since this is a new file, the buffer is empty and the screen appears as follows:

Emacs startup appearence
The Emacs mode line

Just above the bottom of your screen Emacs prints a lot of information about what it's doing. This line is called the mode line. Near the left edge of the mode line, you may see two asterisks (**). These asterisks indicate that you've made changes since you last saved the file, (if you haven't made any changes, they won't be there). Next Emacs prints Emacs: followed by the name of the file you are editing (filename). This is followed by the editing mode you are in, (editing modes are not discussed in this tutorial), the line number you are on, and your position relative to the rest of the file. If you're at the beginning or end of the file, this will say Top or Bot respectively. If you are somewhere in the middle of the file, this will be a percentage, and if the entire file is visible, Emacs prints the word All.

At the bottom of the screen, under the mode line is the minibuffer, where Emacs echos the commands you enter, and displays error messages.

Another way to open a file within Emacs, is to type emacs without a filename at the prompt, and then use the find file command within Emacs to open the file. This is done using

^x ^f filename (TAB can be used to complete the filename)

Exiting a File

There is no single command to save and exit Emacs. To exit Emacs you should first save your file by typing ^x ^s. and then exit Emacs by typing ^x ^c. If you try to quit without saving, Emacs will prompt you to save the file before exiting. If you wish to undo all changes you have made to a file since you last saved it, typing ESC x revert-buffer RETURN will do this.

Moving the cursor

Once you've added some text to a file, you may wish to change, delete, or copy some of the text you've entered. In order to be able to do this, you need to be able to move to the text you want to work with as quickly as possible. The following movement commands will allow you to do this.

^f right one character
^b left one character
^p up one character
^n down one character
^v Forward one screen
ESC v Backward one screen
ESC < Beginning of file
ESC > End of file
ESC f Forward one word
ESC b Backward one word
^a Beginning of current line
^c End of current line
Search and Replace

Using Emacs, it is possible to search a file and replace occurances of one string with another string, this is done with the following commands

^s pattern Search forward for pattern
^r pattern Search backward for pattern
^s^s Repeat last search
RETURN Exit search
ESC % Query-Replace
SPACE, y Replace
DEL, n Don't Replace
! Replace all and don't ask
RETURN Exit Query-Replace
Changing Text

You can replace any text in your file using one of the change commands below.

^t transposes (swaps) two letters
ESC t transposes two words
^x^t transposes two lines
ESC c Capitalize the current letter
ESC u Uppercase to the end of the current word
ESC l lowercase to the end of the current word
Cutting and Pasting

To cut and paste text, use the following commands

^d Delete character under cursor
DEL Delete previous character
ESC Delete next word
ESC DEL Delete previous word
^k Delete from cursor to end of line
^- ^SPACE Mark the beginning of a region
^w Cut region
ESC w Copy region
^y Paste
ESC y After c-y, paste previous
INSERT Toggle overwrite mode
Useful Command

Here a couple of other commands you may find useful.

^x u Undo last change
^g Stop the command in progress
Getting Help

Emacs has extensive online help. To enter help type ^h, which will give you a list of options. Pressing ^h t starts a tutorial that is an excellent introduction to Emacs.



Page Created 14th February 2005.
Last Modified 1st April 2005.