A Simple Introduction to the Emacs Editor


Assumptions

This web page assumes that you are working on a UNIX machine, either directly or logged in through the X-Win32 program. On UMD machines that are running MS Windows, you can start up the X-Win32 program from the Start menu. It is also assumed that you have a terminal window open. This is automatic if you logged in with X-Win32. If you are logged in directly to a UNIX machine then you should open a terminal window. Keep in mind that the UNIX commands shown below are not executed until you have completed the command by hitting return.

Preparation for Using emacs for the First Time

For editing programs with emacs, it is helpful to have emacs configured to support automatic indentation. This makes program text much easier to read. Trust me on this: if you are writing programs you will spend a lot of time reading your code.

To configure emacs, you can copy my emacs configuration file into your home directory with the following command.

    cp ~gshute/.emacs ~
You only need to do this once, prior to the first time that you use emacs. The file was originally obtained from Tim Colburn, our local emacs guru.

Using emacs

The version of emacs that you are working with is quite easy to use. Most of your work can be accomplished if you know how to start up emacs, edit a file with it, save the file, use the buffer list to re-edit files that you worked with earlier, and quit. These skills are described in the following subsections.

Starting emacs

In your terminal window, type in the following command:

    emacs &
Shortly, a new window will appear. This is you emacs editor.
Editing a File

You can start editing a file by clicking on "Open File" on the "Files" menu. You can also use the keyboard shortcut CTRL-X CTRL-F to do this. At the bottom of the emacs window, you will see a "Find file:" prompt, followed by the name of the directory that you started emacs from. If that is the directory that you want, then you can just add the name of the file. When you have the name correct, hit the "Enter" key. If the name you entered is the name of an old file, then the emacs window will display its contents. If you entered the name of a new file, then the emacs window will be blank. You can now type in text.

If after typing in text, you see something that you want to change, you can move the cursor with the mouse or with the arrow keys. You can then delete text at the cursor with the backspace key and type in new text.

Saving a File

At any time, you can save the file by clicking on "Save Buffer" on the "Files" menu. If this menu item is dimmed it means that you have not made any changes since the last save, so you do not need to save the buffer.

Buffers

You can open as many files as you want during an emacs editing session. All of the files that you have edited will appear on the "Buffers" menu. If you want to redit a file, just click on its entry in the menu. This is a very useful tool for editing programs with more than one file. You can also save all of your buffers at once by typing CTRL-X s.

Quitting

You can quit emacs by clicking on the "Exit Emacs" item in the "Files" menu.

An Exercise

Log onto a UNIX machine either directly or using X-Win32. Create a new directory named "test" by giving the following command.

    mkdir test
Then move into the new directory by giving the following command.
    cd test
Now start up emacs with the following command.
    emacs &
Edit two new files named "xxx" and "yyy". Enter any text into them, as long as you can tell them apart. Save each file after you have entered the text you want. Now check the "Buffers" menu. Can you get back to the first file to change it?

When you are done with this exercise, you should remove the files and the new directory that you have created. You can see what files have been created by giving the following command in the terminal window.

     ls
You may have files named "xxx~" or "yyy~" in addition to the files "xxx" and "yyy". The "~" files are backup files that are automatically created by emacs whenever a file is modified.

You can remove the files by giving the following command in the terminal window.

    rm xxx yyy
If you had any "~" files you can add their names after the "yyy", putting a space between each file name. Now if you give the "ls" command again, you shouldn't see any files.

Next, change directory back to your login directory by giving the following command in the terminal window.

    cd
Now give the following command to remove the directory that you added.
    rmdir test

That completes your emacs exercise.