Emacs

From FarmShare

Jump to: navigation, search

Contents

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

corn:~> emacs Filename

Exiting Emacs

C-x C-c

System prompts for file save before exiting

Editing

Cursor Movement

Arrow keys

Back, forward

C-b, C-f

Up one line, down one line

C-p, C-n

Delete previous character DEL

For new users

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

corn:~> man emacs

Emacs tutorial

C-h t

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.

Lists commands related to function

C-h a function

Get help about the specified function

C-h F function

Get rid of Help window

C-x 1

Scroll Help window forward

ESC C-v

Display advanced emacs information browser

C-h i

Error recovery

Cancel partially typed or accidental command

C-g

Redraw screen

C-l

Undo the last major change

C-x u

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

C-x b [Return Key]

Recover lost file

ESC x recover file

Restore buffer to original contents

ESC x revert buffer

Re-enter buffer

ESC x switch to buffer

Screen motion

Scroll to next screen C-v

Scroll to previous screen

ESC v

Cursor movement

Move

Backward or forward one character at a time

C-b

C-f

Backward or forward one word at a time

ESC b

ESC f

Up or down one line at a time

C-p

C-n

To beginning or end of a line

C-a

C-e

To beginning or end of a sentence

ESC a

ESC e

To beginning or end of a paragraph

ESC [

ESC ]

To top or end of a file

ESC <

ESC >

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.

Select another buffer

C-x b

List all buffers

C-x C-b

Get rid of buffer list

C-x 1

Kill a buffer

C-x k

Files

Save the file

C-x s

Save a file with a new name

C-x C-w

Read a different file into emacs, replacing the current buffer

C-x C-v

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

C-x C-f

Insert contents of another file into this buffer

C-x i

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.

Chararacter moving the cursor backward or forward

DEL

C-d

Word

ESC DEL

ESC d

Line to beginning or end

ESC 0 C-k

C-k

Sentence

C-x DEL

ESC k


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.

Restore last text to be killed

C-y

Replace that restored text with previously killed text

ESC y

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

Undo the last major change

C-x u

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.

Set mark here (or ESC _)

C-<space>

Exchange cursor and mark

C-x C-x

Used to verify you have marked the desired region)

Mark current paragraph

ESC h

Mark current page

C-x C-p

Mark entire buffer

C-x h

Kill the marked region

C-w

Copy the marked region

ESC w

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.

Incremental search forward

C-s

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.

Incremental search backward

C-r

Alternative search forward command (see note above)

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

Use C-x Esc [return key] for next occurrences.

Query before replacing each old string with new string

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

Respond y to replace this one, and go to next one; n to skip to next without replacing; ? for help

Replace all occurrences of old string with new string

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

Formatting

Reformat current paragraph

ESC q

Reformat marked region

ESC g

Undo the last major change

C-x u

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

C-L

Automatically wrap lines at the 76 character mark

ESC x auto fill

Doing this command again turns off word wrap

Change wrap mark from 76 to #

C-u # C-x f


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

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.

Personal tools
Toolbox
LANGUAGES