6. The Emacs Editor

6.1. What is Emacs?

Emacs is a powerful text editor that is available on the Linux systems. It has many advantages over other text editors in that it can provide helpful formatting, such as indenting and syntax highlighting.

6.2. Starting Emacs to Edit a File

Before you start the next section, you should copy the file ‘stories’ into your directory. Type the following to copy the file.

[student@hostname ~]$ cp ~csci203/examples/stories stories

To run Emacs, type at a hostname % prompt the command emacs followed by a file name and ‘&’. To create the new file named myfile type the following:

[student@hostname ~]$ emacs myfile &

The & sign tells Linux that you wish to run this program in the “background”. Basically, what this does is give you the host-name % prompt back to allow you to issue others commands in the window.

Since you probably don't have a file called ‘myfile’ in your directory, Emacs creates a new file. You should see ‘(New file)’ at the bottom of the window. Notice the black bar near the bottom. It should tell you the name of the file, the time and other information. And the black rectangle in the upper left hand corner of the Emacs window. This ‘cursor’ moves along as you type. It is important for you to distinguish between the mouse pointer and the cursor. The pointer is used to move between windows and to select items to click on. The cursor is where you type text. Each Emacs and host-name % window has a cursor. The cursor is a black rectangle when the window is active and white when not active.

Now you are ready to edit the file. With the mouse pointer in the large area of the window, start typing. Don't worry about making mistakes. We will fix them up in a few moments. Type three or four lines worth of text. Use the four arrow keys to move the cursor around and insert text. Make corrections by deleting the character to the left of the cursor by using the Backspace key. You can also use the left mouse button to position the cursor where the pointer is.

6.3. Notation for Emacs' Key Sequences

Emacs uses special sequences beginning with the Control and Esc keys. The notation C-f means to hold the Control key down and press the f key. The notation M-f means to press the Esc key once and then f. Do not hold the Esc key down as that repeats the key.

6.4. Exiting From Emacs

To exit Emacs, from the menu select File – Exit Emacs (or type C-x C-c) and Emacs will ask you if you need to save any files.

6.5. Saving the Contents of the File

What you have typed for the file myfile has not been saved. To save the file, select File – Save (Current Buffer) (or type C-x C-s). You should see a message appearing at the very bottom of the window saying Emacs ‘wrote’ the file. This small area is where Emacs displays messages or requests input. As you are developing a program or typing in a data file, you should save it every few minutes.

6.6. Reading A File

You can read (find or load) a file after you are in Emacs by selecting File – Open File… (or typing C-x C-f). In the bottom small area, you should see find file: ~/ where you type in the pathname of the file. For now, you should type in stories after the / and a file with stories should be loaded in the window for editing (If the stories file is missing, you didn't copy it. Exit Emacs and copy the file into your directory as described in an earlier section). Since you will be experimenting and modifying your own copy of the file, feel free to modify it as you wish. The stories file is too large to fit on the screen all at once. Notice the black bar on the bottom, it says ‘top’. This means you are at the top of the file. At other times by displaying a percentage, it indicates how far you are through the file. When you are at the end, it says ‘bot’ for bottom. If all the file can be displayed, then it says ‘all.’

6.7. Moving the Cursor and Moving Text Within the Screen

To scroll forward and backward, use Page Up and Page Down respectively and notice that the percentage changes on the bar across the bottom. Below are some cursor commands to try on the stories file. Practice them all several times before going on.

Table 1. Cursor Movement and Screen Motion

M-fForward one word
M-bBack one word
C-eEnd of line
C-aBeginning of line
M-<Beginning of file
M->End of file
C-vScroll forward a page
M-vScroll back a page


6.8. Cutting and Pasting a Region of Text

To move a block of text, you drag the cursor from the beginning of the region to the end of the region and cut the text with Edit –> Cut (or type C-w). Now you move the cursor to the place where you want and paste with C-y.

To copy or duplicate a region of text is similar except instead of cut we copy with Edit –> Copy (or type M-w). Move a whole story from one part of the file to another. Copy or duplicate a saying or two. The cut operation can be used to delete a region as well. Remove from the file any stories you don't like! Here is a summary of the killing and deleting operations to practice on the stories file.

Table 2. Killing and deleting

DELDelete character to left
M-DELKill word to left
C-kKill from cursor to end of line
C-SPACEMark start of region
C-wCut region (Kill region)
M-wCopy region
C-yPaste region (Yank back last thing killed)
M-yYank back thing before last thing killed (do after a C-y). Repeat to retrieve older text killed.


Notice the distinction between ‘deleted’ and killed.’ Deleted text can not be ‘yanked’ back or retrieved while killed text can.

6.9. Recovering From Errors

You just typed an Emacs command and it didn't do as you wanted. Perhaps, you deleted a large, but the wrong, section of text!! While in Emacs you can undo it by selecting Edit – Undo (or type C-_). You can continue to use this option to slowly undo your other changes.

Another cause of frustration is when you type in a wrong Emacs command but haven't pressed the Return key yet. You have great dread that this unknown command will wreak havoc on your file. Just type C-g to cancel a partially completed command.

6.10. Using Auto-save's Backup Copy of File

If you do not save your file for several minutes, Emacs automatically saves the file (auto-save feature) in a file name the same as yours with a ‘#’ before and after. In case of disaster, e.g. the electrical power goes out, you can retrieve the latest auto-save copy by copying that file to your file. Inside Emacs type M-x recover-file then type in the name of your file you want to recover. Then you can save with C-x C-s to put the recovered text into the file itself.

6.11. Searching for Character Strings

Emacs has a character searching feature that is a joy! It is called an incremental search as it finds the next match from where the cursor is as you type in each character! Select Edit –> Search –> Search… (or type C-s) and slowly type a common word such as ‘and’ to see this in action.

To find further occurrences of the string, type C-s repeatedly. When you get to the end of the file, C-s will wrap around to the beginning and continue searching. To get out of search mode type C-g. You can always search for the last thing searched by hitting C-s C-s. Select –> Search –> Search Backwards… (or C-r) does the same thing except the search goes backwards in the file.

6.12. Splitting the Screen and Multiple Files

Many times you would like to look at two different portions of a file but they both are not displayed at the same time. Emacs lets you split the window into two parts horizontally by selecting File –> Split Window (or typing C-x 2). Now both parts can be scrolled and operated on. For example, you may cut and paste from one part to the other. You move between the split windows by clicking in either pane with the mouse or by typing C-x o. You remove all split windows other than the window with the cursor by selecting File – Unsplit Windows (or typing C-x 1).

A powerful technique is to use the split windows on two different files. Just move to the other part and read (find) a file, say myfile. Again, you can scroll the two files and cut and paste between them.

Here is a summary of the split and multiple window commands.

Table 3. Split Windows and Multiple Files

C-x 2Split window in two horizontally
C-x 0Delete this window
C-x 1Delete all other windows
C-x oSwitch cursor to other window


6.13. On-line Help and On-line Emacs Tutorial

There is on-line help in Emacs which you will probably find a little advanced. You receive the help by C-h. If you want some more practice with the Emacs commands, there is a tutorial which you begin by selecting Help –> Emacs Tutorial (or by typing C-h t).

6.14. Summary of Some Emacs Commands

On the following page(s) is a summary of many of the Emacs commands mentioned in the previous sections.

C-f means hold Control key down and press f.

M-a means press Esc key then press a.

Table 4. Emacs Command Summary

Cursor movement:
C-fForward one space.
C-bBack one space.
C-nNext line.
C-pPrevious line.
C-aBeginning of line.
C-eEnd of line.
M-fForward one word.
M-bBack one word.
M-<Beginning of file.
M->End of file.
Screen Motion:
C-vScroll forward a page.
M-vScroll back a page.
Error Recovery:
C-gAbort partially typed in command.
C-_Undo an unwanted change.
Killing and deleting:
DELDelete character to left.
M-DELDelete word to left.
C-kKill from cursor to end of line.
C-SPACEMark start of region.
C-wCut region (Kill region).
M-wCopy region.
C-yPaste region (Yank back last thing killed).
M-yYank back thing before last thing killed (do after a C-y). Repeat to retrieve old text killed.
Split Windows and Multiple Files:
C-x 2Split window in 2 horizontally.
C-x 0Delete this window.
C-x 1Delete all other windows.
C-x oSwitch cursor to other window
General Commands:
C-x C-cTo quit Emacs.
C-hFor help.
C-x C-fTo find or read a file (used to create new ones too).
C-x C-sTo save a file.
C-x iTo insert contents of file.
C-x C-bList buffers.
C-sIncremental search for a string. C-s for next occurrence. ESC to stop search.
M-x repl sTo replace a string with another.
C-x C-g or (M-x goto-line)To go to a line number.