Header image: Architectural widgetsSociology at Duke
Navigation Bar: Resources

Research and Training

Sociological Sites

Computing Resources

Departmental Publications

University Libraries

Quick Links

  
 

Emacs Guide

Emacs is a powerful and flexible editor. It is convenient to edit a program in one terminal window, save it to disk and then submit the disk copy for batch execution from a second window. This reference summarizes how to perform basic editing functions.

Special Keys and Their Notation

Emacs edit operations are driven by special keystroke combinations. Many involve use of the control key [Ctrl] typed simultaneously with another key, herein denoted for example as [Ctrl-x]. A leading control key combination may constitute a complete command or may be followed by additional keystrokes or keywords.

Other operations involve use of the META key, which is pressed, released and then followed by other keystrokes. The META key is programmable, but is typically mapped to the [Esc] key on your keyboard. Such combinations are denoted here as [Esc][x] to indicate separately typed keys.

X-Window Versus Terminal Modes

As described below, Emacs may be launched in a separate GUI X-window or used from within the current terminal window. The X-window mode provides drop-down menu support for many of the common editing functions. These menus are fairly intuitive and can be mastered with some experimentation.

Structure of the Emacs Screen

The screenshot below illustrates an Emacs session in terminal mode. The features illustrated here are common to both X-Window and terminal invocations. All but the last two lines of the screen comprise the edit workspace. The line in reverse video is the mode line. It indicates the name of the file being edited. If two asterisks (**) appear to the left of the filename, the contents of the edit buffer have changed since the last save. Emacs operates in one of several pre-defined major modes, with additional minor mode options affecting major mode performance when applied. By default Emacs will open in Text major mode (for editing regular text files) with the Fill minor mode feature turned on. Fill mode is equivalent to word-wrap in a word processor. When you reach what Emacs considers end-of-line, the editor automatically inserts a line feed and starts the next line. Fill mode may not be desirable when editing program code and can be toggled off (the command is described below). When fill mode is off, lines which extend beyond the visible screen width are wrapped with a backslash (\) at the end to indicate that the next line is a continuation. The All indicates that the complete file is displayed in the edit buffer.

The last line of the screen is the minibuffer. It provides general information, displays Emacs commands as you type them and prompts for various types of information as needed.

Start, Stop and File Operations

Command Operation
emacs & Launch empty buffer (no file) in a new, menued, GUI window - PC X-server must be loaded.
emacs filename & Open filename in a new, menued, GUI window - PC X-server must be loaded.
emacs -nw Launch with empty buffer (no file) in current terminal window - without X-window support.
emacs -nw filename Open filename in current terminal window - without X-window support.
[Ctrl-x][Ctrl-s] Save file without quitting - if filename was unspecified at startup, you are prompted for a filename.
[Ctrl-x][Ctrl-w] Save current work to a different file - you are prompted for the location and name of the file.
[Ctrl-x][Ctrl-c] Quit Emacs - if current work is unsaved, you are prompted whether to save work.
[Ctrl-x][Ctrl-v] Find an alternate file and replace it as the file currently being edited - you are prompted for location and name of the file.
[Ctrl-x]i Insert a file at the current cursor position - you are prompted for the location and name of the file.

NOTE: Each time you perform a save operation in Emacs, the previously saved copy of the file is saved with the same name and a tilde (~) symbol appended. This provides a running backup that you can go back to in the event of a major editing blunder.

Cursor Movement and Scrolling

Backward Forward Unit of Movement
[Ctrl-b] or left arrow [Ctrl-f] or right arrow character
[Esc][b] [Esc][f] word
[Ctrl-p] or up arrow [Ctrl-n] or down arrow line
[Ctrl-a] [Ctrl-e] beginning (or end) of line
[Esc][v] [Ctrl-v] screen - this is how you page through a documnent
[Esc][<] [Esc][>] beginning (or end) of file

Insert, Delete and Restore Operations

Command Operation
[Insert] key Toggles between insert and overwrite mode - note changes to mode line at bottom of screen as toggled.
[Delete] or [Backspace] key

Delete character to left of cursor.

[Ctrl-d] Delete character under the cursor.
[Esc][Delete] Delete previous word.
[Esc][d]

Delete word the cursor is on.

[Esc][0][Ctrl-k] Delete from the beginning of the line to the cursor location.
[Ctrl-k] Delete from cursor to the end of the line - using a second time will delete the carriage return.

[Ctrl-Space] at start of block
[Ctrl-w] at end of block

Delete a block of text - place cursor at beginning of block and mark it with the first command; move cursor to end of block and type second command to delete.
[Ctrl-y] Yank back (restore) text from last deletion - the results of several delete operations performed in succession, without intervening commands or typing, are restored in one yank operation. It can also be used to repeatedly paste the same block of text to different locations in the file.

Search and Replace Operations

Command Operation
[Ctrl-s]

Initiates a forward search operation from the current cursor location. You are prompted at the bottom of the screen for a search string. As you type the string, the cursor positions after the last character of the first instance of the string being typed. To find the next instance of the same string, enter [Ctrl-s] again and type the [Enter] key twice.

[Ctrl-r]

Initiates a backward search operation from the current cursor location. You are prompted at the bottom of the screen for a search string. As you type the string, the cursor positions on the first character of the first instance of the string being typed. To find the next instance of the same string, enter [Ctrl-r] again and type the [Enter] key twice.

[Esc][%] Initiates a query replace operation. You are prompted for the search string. After entering the string, type [Enter]. You are prompted for the replace string. Type it and press [Enter]. You are prompted for confirmation. To replace one instance and move on to the next, type [Space]. For a menu of other options, type [?].
[Esc][x]replace-string Initiates a global replace operation without confirmation. The replace-string (typed exactly as shown) is part of the command. You are prompted for the search string. Type it and press [Enter]. You are prompted for the replacement string. Type it and press [Enter]. All instances of the search string are replaced.
[Esc]

Terminates search and search-replace operations.

Miscellaneous Commands

Command Operation
[Ctrl-g]

General comand to abort any operation in progress or partially entered command.

[Ctrl-x][u]

Undo last edit. This command may be entered repeatedly to undo a series of edits in reverse order. Undo operations may be applied up to the last save operation.

[Esc][x]line-number-mode A toggle that turns line numbering on or off on the mode line. When on, the mode line will display which line in the file the cursor is located on.
[Esc][x]auto-fill-mode

A toggle that turns Fill mode on or off. When on Fill will be displayed on the mode line as the minor mode setting.

[Esc][q] Reformats a paragraph when Fill mode is toggled on. This command is particularly useful when editing text as opposed to program code. It reformats a paragraph that you have edited and aligns text within the fill margin.

For information on the many advanced features of Emacs, consult the authoritative online documentation from the GNU Project or the published guide from O'Reilly and Associates.


People Graduate Program Undergraduate Program Resources Home Duke University Home