Skip navigation

STANFORD UNIVERSITY

INFORMATION TECHNOLOGY SERVICES

Emacs Reference

Introduction

This document describes the basic commands for using emacs, a text editor on the shared Unix workstations (Cardinal, Tree, Power, Wisdom, and the Sweet Hall workstations).

Bare Essentials

Emacs displays a certain number of characters per line until it gets to the right margin of the display. Longer lines will wrap to the next physical line; this is indicated by a \ at the end of each wrapped line. To avoid wrapping lines, either hit the Return key before you reach the right margin of the display, or choose the auto-fill> option. (See the Formatting section.)

Note: Commands designated by C-<letter> are called Control commands. To execute Control commands, hold down the Control key while typing the letter indicated. Commands designated by ESC < letter > are Escape commands. Execute these by pressing the Escape key ONCE and then typing the letter indicated.

Starting Emacs:
host> emacs Filename [Return Key]

Exiting Emacs:

C-x C-c

System prompts for file save before exiting

Editing:

arrow keys

Cursor movement keys

C-b, C-f

Back, forward

C-p, C-n

Up one line, down one line (for use if the keyboard arrow keys do not operate correctly)

DEL

Delete previous character

For New Users

host> man emacs [Return Key]

About emacs

C-h t

Emacs tutorial

To learn about the many sophisticated features of Emacs you can buy Richard Stallman's GNUemacs manual at the Stanford Bookstore.

Getting Help

You can invoke the general Emacs help files by typing C-h C-h and following the instructions at the bottom of the screen.

C-h a function

Lists commands related to function

C-h F function

Get help about the specified function

C-x 1

Get rid of Help window (Note: that's a"one," not an "L")

ESC C-v

Scroll Help window forward

C-h i

Display advanced emacs information browser

Error Recovery

C-g

Cancel partially typed or accidental command

C-l

Redraw garbaged screen (Note: that's an"L", not a "one")

C-x u

Undo the last major change

C-x b [Return Key]

If you accidentally get into the tutorial, this returnsyou to the default buffer

ESC x recover file

Recover lost file

ESC x revert buffer

Restore buffer to original contents

ESC x switch to buffer

Re-enter buffer

Screen Motion

C-v

Scroll to next screen

ESC v

Scroll to previous screen

Cursor Movement

 

 

Move

C-b

C-f

Backward or forward one character at a time

ESC b

ESC f

Backward or forward one word at a time

C-p

C-n

Up or down one line at a time

C-a

C-e

To beginning or end of a line

ESC a

ESC e

To beginning or end of a sentence

ESC [

ESC ]

To beginning or end of a paragraph

ESC <

ESC >

To top or end of a file

Buffers

The buffer is the basic editing unit. One buffer corresponds to one section of text being edited. You can have several buffers open at once, but can only edit one at a time. Several buffers can be visible at the same time when you are using multiple windows.

C-x b

Select another buffer

C-x C-b

List all buffers

C-x 1

Get rid of buffer list

C-x k

Kill a buffer

Files

C-x s

Save the file

C-x C-w

Save a file with a new name

C-x C-v

Read a different file into emacs, replacing the current buffer

C-x C-f

Read a different file into emacs, put it into another buffer

C-x i

Insert contents of another file into this buffer

Killing or Deleting Text

In emacs, to "kill" means to delete something in a way that lets you recover it if you change your mind. DEL refers to the key on your keyboard that deletes backward one character--this may be the Backspace, Delete, or Rubout key depending on your particular keyboard.

Backward

Forward

 

DEL

C-d

Character

ESC DEL

ESC d

Kill word

ESC 0 C-k

C-k

Kill line (to beginning/end)

C-x DEL

ESC k

Kill sentence

All killed text (except single characters deleted with DEL or C-d) can be restored to the buffer. The most recently killed text is the first to be restored; previously killed text blocks are restored in reverse order.

C-y

Restore last text to be killed

ESC y

Replace that restored text with previously killed text. Repeated use will cycle through killed text blocks in reverse order, and must always follow either the original C-y or another ESC y.

C-x u

Undo the last major change.

Cutting and Pasting Text

To move or copy a region of text in emacs, you must first "mark" it, then kill or copy the marked text, move the cursor to the desired location, and restore the killed or copied text. A region of text is defined by marking one end of it, then moving the cursor to the other end.

C-<space>

Set mark here (or ESC _)

C-x C-x

Exchange cursor and mark (used to verify you have marked the desired region)

ESC h

Mark current paragraph

C-x C-p

Mark current page

C-x h

Mark entire buffer

C-w

Kill the marked region

ESC w

Copy the marked region

Searching and Replacing

In both forward and backward incremental searching, you can repeat the same command for next occurrences. -> exits the current search without moving the cursor. C-g aborts the current search and moves the cursor back to the initial position.

C-s

Incremental search forward. Note: On some terminals and serial connections, C-s causes the display to freeze. If this happens, use C-q to remedy the problem and use the alternative emacs commands listed below to avoid C-s.

C-r

Incremental search backward

ESC x search f [return key] string [return key]

 

Alternative search forward command (see note above); use C-x Esc [return key] for next occurrences.

ESC % old string  [return key] new string [return key]

 

Query before replacing each old string with new string. Respond y to replace this one, and go to next one; n to skip to next without replacing; ? for help

ESC x replace s [return key] old string  [return key] new string  [return key]

 

Replace all occurrences of old string with new string.

Formatting

ESC q

Reformat current paragraph

ESC g

Reformat marked region

C-x u

Undo the last major change

C-L

Redraw screen, moving the line with the cursor to the center

ESC x auto fill

Automatically wrap lines at the 76 character mark. Doing this command again turns off word wrap

C-u # C-x f

Change wrap mark from 76 to #

The last two commands set the format and must be followed by one of the first two in order to apply it to a particular section of text. If you have a .emacs file in your directory, you may already have auto-fill (i.e. word-wrap) enabled. If you don't have an emacs file, you can get one by copying /usr/skel/.emacs into your home directory from any Sweet Hall workstation.

Printing

When you're logged in, you can print files on a Sweet Hall printer by typing the following command:

host> lpr -PNAME FILENAME [Return Key]

Replace NAME with the name of the printer (e.g., sweet0) and FILENAME with the name of the file you want to print.

To print on your own printer, you must first download the file to your desktop computer. Exactly how you do this depends on which communication package you use, as well as which type of computer you have.

This document is based on originals prepared by Lynn Gale and Patrick Goebel of the Center for Advanced Study in the Behavioral Sciences.

Last modified Thursday, 19-Jan-2006 11:26:14 AM

Stanford University Home Page