Emacs Guide

This brief introduction to use of the emacs editor covers basic features.

Emacs may be launched in a separate graphical window or in textual mode within the current terminal window. When starting graphically, an X-server must be available, so Windows users should preload X-Win32. The command shown below loads a graphical emacs window with the file probtest.sas for editing.

> emacs probtest.sas &

The Help menu provides learning and support aids ranging from keyword command searches to online manuals. 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.

To edit a file non-graphically in the terminal window emacs is called from, use the following type of invocation. The -nw parameter indicates no windows.

> emacs -nw probtest.sas

1. Structure of the Emacs Display

The resulting emacs window is shown below. A text mode emacs window has many of the same features, but lacks the top menu bar and the taskbar icons. All but the last two lines of the screen comprise the edit workspace. The first of these two lines 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 the 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 (as described below under miscellaneous commands). 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. L1 indicates that the cursor is positioned on the first line of the file. 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.

attachment:emacs.png

2. Special Keys and Their Notation

Emacs edit operations are driven by special keystroke combinations. Many involve the 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.

3. 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 - in textual mode without X-window support.

emacs -nw filename

Open filename in current terminal window - in textual mode 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.

4. Cursor Movement and Scrolling

Backward

Forward

Unit of Movement

[Ctrl-b] or left arrow

[Ctrl-f] or right arow

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 used to page through a large document

[Esc][<]

[Esc][>]

beginning (or end) of file

5. 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. NOTE: the second key is zero.

[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.

6. 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.

7. 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.


Computing Home | Sociology Home | Trinity College Home | Duke Home