Mathematics Department - Using Emacs

Using Emacs

Creating and editing files using the EMACS text editor

Files may be created and edited using an editor called Emacs. Suppose for example that you wish to create or edit a file named test.Emacs is available locally, on your machine, or remotely, on a server such as math.In either case a terminal window needs to be brought up. If you want to use emacs locally just type emacs filename &.The & lets others things open while working in this file.

Basic editing commands

We now describe some commands to edit a new or existing file. You use these once you are "in" Emacs. Although there are exceptions, generally speaking, they cannot be used to edit commands to the Shell.

Recall that ctr-B means: hold down the key marked CTRL and press the b key while keeping the CTRL key pressed. By esc-V, we mean first press the key marked ESC (do not hold it down) and then press the v key.

There are many commands other than the ones listed below. To get more information about Emacs, type (once in Emacs) ctr-h i and follow the instructions. To get a tutorial on Emacs, type ctr-h t. In an X-terminal Emacs window, you can also get help by clicking on help.

ctr-B     moves the cursor back one character
ctr-F     moves the cursor forward one character 
ctr-P     moves the cursor up one line  
ctr-N     moves the cursor down one line  
ctr-A     moves the cursor to the beginning of a line
ctr-E     moves the cursor to the end of a line

ctr-V     moves the cursor to the next page  
esc-V     moves the cursor to the previous page  
ctr-D     deletes character at the position of the cursor
ctr-K     deletes line to right of cursor
ctr-O     opens blank line to right of cursor moving text down
esc-<     moves the cursor to the beginning of the file

esc->     moves the cursor to the end of the file
ctr-G	    cancels running or partially typed Emacs command
ctr-SHIFT-_  undo last command
DEL       (i.e., hitting the DEL key) deletes the character to the left of the cursor

Cutting and pasting

First, we describe a procedure that uses the mouse in an Emacs window on a X-terminal. To delete text in one region and move it to another region, move the mouse to the beginning of the region to be moved and click the left mouse button. Now move the mouse to the end of the region to be moved and click the right mouse button twice. This deletes the text (and stores it in the kill buffer). Now move the mouse to the desired location and click the left mouse button (thereby moving the window cursor to that location) and then click the middle mouse button to place the text at that position. To copy a region instead of moving it, use the same procedure, but clicking only once with the right mouse button at the end of the region to be copied. Another way to move or copy text is by clicking and holding down the left mouse button, and then dragging the mouse to highlight the desired text. Now click on Edit and use the copy, cut, and paste commands to move or copy the text.

We next describe a procedure that only uses keys, so is possible on any terminal running Emacs. To delete or move an entire region, move the cursor to the beginning of the region to be deleted or moved. Type ctr-@ . This sets a mark at that point of the text. Now move the cursor to the end of the region to be deleted and type ctr-W . You have now deleted the region from your text and stored it in the kill buffer of Emacs. To restore it (possibly in a different place, in effect moving the region) move the cursor to the beginning of the new position and type ctr-Y . The ctr-Y command can be used repeatedly to yank this text from the kill buffer, giving a way of moving something to various places. To copy an entire region, use the same procedure as in moving a region, except that the command ctr-W should be replaced by esc-W, which stores the region in the kill buffer without deleting it from the text.

Additional commands

There are many other Emacs commands which make typing and editing much easier. Some which are especially helpful are:

ctr-X ctr-F Find file. (The Emacs editor will prompt you for a file name.) Thus, you may work on a new file without leaving and reentering Emacs and bring up a different file if you are using multiple windows.

ctr-X i Insert file (The Emacs editor will prompt you for a file name.) The named file is inserted at the position of the cursor. This is especially useful when sending files in a mail message (see the section on reading and sending mail using Emacs).

ctr-X b Return to previous buffer. Switches to the previous file you had displayed in Emacs.

esc-Q Fill (partially justify) paragraph. (This command is used to eliminate blank space in a paragraph.)

esc-X shell This commands opens a shell running under Emacs. The advantage of this is that you can issue commands to the shell without leaving Emacs. In particular, you can use the Emacs editor to edit your commands (e.g., by copying parts of previous commands).

esc-% or esc-X query-replace This command will prompt you for the character string you wish to replace and then for the character string you wish to replace it by. The cursor will then stop at each occurrence of the character string in the file. Hit the space bar if you wish replacement and the Delete key if you don't.

There are also commands to allow you to work on more than one file at once, using multiple windows. The basic commands for this are:

ctr-X 1 change to only one window

ctr-X 2 change to two windows

ctr-X o move cursor to other window

There are also many other mouse commands. Once in Emacs, click on help, and release the mouse on info. Select emacs. Use ctr-S to search for mouse commands. Then click the middle mouse button on mouse commands to bring up a list of commands.

To save a file

Type ctr-X and then ctr-S . This can be done as often as you wish. You do not need to wait until the file is complete to save part of it. This is important since you might hit the wrong key and lose part of what you have typed if you have not saved it. Also, the system is not 100% reliable, and you may lose all your work due to a crash if you do not save periodically. Old versions of the file will be automatically deleted when you do a new save. If you want to save under a different file name from the one you started with (the name of the file you are working on is displayed at the bottom of the screen during Emacs editing), use the command ctr-X ctr-W instead.

To exit and return to the shell

When using a terminal, type ctr-X ctr-C to exit Emacs and return to the shell. (If you forget to save your file, Emacs will ask you some questions before letting you exit.) When using X-windows, it is rarely necessary to exit Emacs -- just move your cursor to an xterm window to communicate with the Shell.

This page was last updated on August 01, 2017 at 11:02 am and is maintained by
For questions regarding courses and/or special permission, please contact
For questions or comments about this site, please contact
© 2021 Rutgers, The State University of New Jersey. All rights reserved.