College of Arts and Sciences

Department of Mathematics

The Emacs Text Editor

Table of Contents
Using Emacs
Special keys
The environment
Moving the cursor
Cutting and Pasting Text
Multiple windows and files
Searching and Replacing
Bells and whistles


Using Emacs

Emacs was written originally by Richard Stallman, although additions have been made by a number of programmers since. It is GNU software, meaning that it is free, and its code is in the public domain, among other things.

The nicest thing about emacs is that you can start it up and use it without knowing a thing. To start emacs, type

your computer> emacs filename

where filename is the name of the file you wish to edit. On Sun computers, one may use lucid emacs. Type

arcsun> xemacs filename

All one needs to do to insert text is to start typing. If one wants to insert text at some point in the file other than the first character, then one may move the cursor to the spot using the arrow keys (or the mouse), and then start typing. Ultimately, this is what many emacs partisans like best about it. It is not necessary to give the editor any command to go into insert mode - it is always in insert mode. In order to delete characters, use the backspace key. To exit emacs, use the file menu, or else hold down the Ctrl key and type "xc".

Special keys

The problem with an editor that is always in insert mode is that it is more complicated to give commands to the editor. This is done in emacs through the use of two special keys. The first is the Ctrl ("control") key, and the second is the "Meta" key. The Ctrl key is usually found at the lower left (and sometimes the lower right as well) of the keyboard. The Meta key varies, but is usually the Esc ("escape") key, found at the upper left of the keyboard. These two keys are used in combination with others to give commands to emacs.

For example, one moves to the beginning of the next line by holding down the Ctrl key and typing "n". This sequence of key strokes is usually written using the notation C-n. Control key combinations always require holding down the Ctrl key while typing another. Combinations using the Meta (Esc) key are different. For example, one may move forward in the file by one word by pressing the Esc key, letting go nof it, and then typing "f". This key combination is denoted M-f.

In this notation, one writes the command to exit emacs as C-x C-c. Note that one should never type C-M. There are some commands that use a sequence like M-C-x, but this is interpreted as <Esc> followed by C-x.

One very useful command is C-g. This tells emacs to call off whatever command it is in the middle of executing. It is a good way to get out of a situation you don't want to be in. Remember that to exit emacs, one types C-x C-c.

The environment

There are a variety of emacs environments available, depending on the version of the program one is using. Most versions of emacs run in a special window. At the top of the window is a menu, the use of which is similar to the use of menus in any windowing environment. Just use the mouse to click the menu item of interest, which causes a pull-down menu to appear. On the latter, one may click the desired action. For example, one may exit emacs by clicking the File menu at the upper left of the screen, and then the Exit item in the pull-down menu.

Emacs can work on more than one file at a time. It reads copies of the files into buffers, and then operates on the buffers. It saves the contents of the buffer back into the file only when told to do so, through a command or the menu (File-Save).

Near the bottom of the screen is the mode line. The mode line provides information about the editing session. Below the mode line is the minibuffer. This is where certain diagnostic messages appear, and emacs prompts the user for command input.

Moving the cursor

The simplest ways to move the cursor involve the arrow keys and the mouse. Their use is intuitive, however touch typists will resent having to move their hands back and forth to the mouse constantly. Emacs does provide additional facilities for moving using the keyboard. These are summarized in the following table.


Command keys          Action                                   

M-f                   Move to the beginning of the next word   

M-b                   Move to the beginning of the previous    
                      word                                     

C-a                   Move to the beginning of the current     
                      line                                     

C-e                   Move to the end of the current line      

M-<                   Move to the beginning of the buffer      

M->                   Move to the end of the buffer            

C-v                   Scroll forward one screen                

M-v                   Scroll backward one screen               



Cutting and Pasting Text

There are a number of ways to delete text from a file. As mentioned previously, the simplest is to use the backspace (or Delete) key. Doing so deletes characters in front of the cursor. A summary of deletion commands follows.


Command keys            Action                                        

Backspace               Delete character before the cursor            

C-d                     Delete character under the cursor             

M-DEL                   Delete to the beginning of the current word   

M-d                     Delete to the end of the current word         

C-k                     Delete to the end of the current line         

M-k                     Delete to the end of the current sentence     



There are numerous nuances in the use of these commands. Experiment with them on a test file before using them extensively on a file that is important. On the other hand, there is no reason to take extreme precautions. It is possible to undo the effects of commands after they are executed. This can be done either using a menu item (Edit-Undo), or by typing C-x u. One may undo as many actions as one wishes, simply by using C-x u repeatedly.

There is a related feature of emacs that is actually somewhat complicated. Items that are deleted are stored in a form called the kill ring. At the risk of misrepresenting the ring structure, think of the kill ring as a stack of pancakes, where each pancake is a section of text that was deleted. Each time text is deleted, it is added to the top of the stack (another pancake). One may yank back as many sections of text from the kill ring as one wishes. It is possible to yank that text off the kill ring and put it anywhere it is needed by using the C-y command. C-y puts the text from the top of the kill ring at the current cursor position. Try this a few times to note the many ways in which C-y differs from C-x u.

Suppose that the word "old" was deleted from a given buffer, and then the word "new" was deleted after that. Now suppose that one needs to put the word "old" into a new place. It can be done. Place the cursor in the spot where the word "old" is to be inserted. Now use C-y to put the top entry in the kill ring ("new") in that spot. Finally, use the M-y command to replace "new" with the previous entry on the kill ring, namely "old". In summary, the M-y command replaces a section of text which was yanked from the kill ring with the entry on the kill ring that preceded it. We warned you that it was complicated. One may use M-y as often as one wishes to get even earlier entries off the kill ring. When the first entry on the kill ring is reached, one more M-y command starts the process over at the top entry (that's why it is called the kill ring, instead of the kill stack).

There is another way to delete text (and thereby get it onto the kill ring). One may set a mark, move the cursor to the other end of a block of text, and then delete all the text between the two spots. To set the mark at a given point, use the C-SPC command (SPC denotes the space bar). Move the cursor to the other end of the block of text to be deleted (forward or backward), and use the C-w comand. C-w deletes all text between the mark and the cursor. Many versions of emacs change the appearance of the text between the mark and the cursor before it is deleted.

Note that the kill ring provides a facility for cutting, pasting, and copying text from one place to another. To copy text from one location to another, just delete it, use C-y to undelete it, and then use C-y to place a copy of the text anywhere you wish.

Multiple windows and files

One can open multiple windows within a single emacs session screen. In these windows one may display different views of the same buffer, or entirely different buffers. To make an exact copy of the current window in the right half of the screen, type C-x 2. Using this procedure, one may make up to four vertical windows on a standard terminal, and more on an X-terminal. To move to the other window, type C-x o, or just click it with the mouse. One may subdivide the current window further by typing C-x 2 again. No - C-x 4 does not make four windows... To go back to a one window screen, use the command C-x 1.

A second file can be opened in emacs either by using the File-Open menu item, or by using the C-x C-f command. In either case, watch the command line at the bottom of the screen. It will prompt you for the file name. Enter the file name and press <Enter>. The new file will appear in the current window. In X versions of emacs, one can have more buffers than windows. One then tells emacs which buffer should be displayed in a given window by moving the cursor to that window, and then using the Buffers menu to select the desired buffer.

Now one may copy text easily from one file to another, or from one part of a file to another part of the same file, by deleting it (and replacing, if necessary) from the buffer in one window, and using C-y in the other.

There are other useful file related commands. One should save one's buffer regularly. This is done using the C-x C-s command. To read the contents of a file into the current cursor position, use the C-x i command. To close the current buffer and load a new file into the window, use C-x C-v.

Searching and Replacing

To search from the current position forward in the file, use the command C-s. This causes emacs to prompt you for the string to be sought. Emacs looks for the string as you type it in, and shows the result in the current window. Emacs waits for you to enter more characters to refine the search until you execute some other command, or until you call off the search with the C-g command. To search for the next occurence of the same string, type C-s again. One may use C-s as often as desired, until the end of the buffer is reached. To search backwards, use the C-r command. Its operation is similar to that of C-s.

Perfoming search-and-replace operations is more complicated. One must use an emacs lisp command. To enter an emacs lisp command, type M-x. In the minibuffer, note that emacs has written M-x. Type search-replace, and press the <Enter> key. Watch the minibuffer as you do so. After entering this command, emacs will prompt you for the string to search for. Type it in, then hit <Enter>. Next emacs will prompt you for the string to replace the first one with. Type that, hit <Enter> and hold on. Every occurrence of the first string from the cursor position to the end of the file will be replaced with the second string.

Often one wishes to replace some occurences of a string, but not all. Emacs has a function for this: query-replace. This function searches for each occurrence of the string to be replaced, and asks the user whether it actually should be replaced or not. To use it, one again types M-x, and then enters query-replace at the prompt in the minbuffer. Then one enters the string to be replaced, followed by <Enter>, and the string to replace it with, followed by <Enter>. Emacs finds the first occurence of the string below the cursor position, highlights it, and waits for user input. The user has several possible actions, as summarized in the following table.


Keystroke    Action                                                       

Delete       Go to the next occurence of the search string without        
             changing this one.                                           

Spacebar     Make the change in this occurrence, then go to the next.     

^            Back up to the previous occurrence.                          

!            Replace all remaining occurrences                            

Esc          Exit the query-replace function.                             



Remember that in order to change every occurrence of a string in a given file, one must first place the cursor at the beginning of the file (M-<). The replace functions only work on the part of the file below the current cursor position.

If one needs to find and replace regular expressions, emacs provides two more functions called replace-regexp and query-replace-regexp that allow this to be done. The details of this are beyond the scope of this document.

Bells and whistles

As noted in the introduction, emacs comes with a number of fancy features. These include a spell checker, mail reader, web browser, and even a vi emulator. These features are available through the menu on some versions of emacs. Xemacs allows syntax highlighting (all of your Tex commands could appear in red type, or all C commands could be purple). It is these features that make emacs well liked by its users, but it is these features that make emacs take up the enormous amount of memory that it does. The details of their use lies beyond the scope of this document.

Department of Mathematics, PO Box 643113, Neill 103, Washington State University, Pullman WA 99164-3113 Phone: 509-335-3926 Fax: 509-335-1188 Contact Us