Computer Science

Emacs Guide

Department of Computer Science (COS)
Guide to Emacs (version 18)

 

Conventions

 

C = Ctrl key, as in C-x.

M = Meta key, as in M-v. Meta key is the left Alt key on DEC stations and X terminals; it's the Esc key on PCs.

 

Starting Emacs and Leaving Emacs

 

To enter Emacs, just type emacs.

To edit an existing file or create a new file, type emacs filename.

To exit Emacs, press C-x C-c.

 

 

Files

 

read a file into Emacs C-x C-f
save a file back to disk C-x C-s
insert contents of another file into current buffer C-x i
replace current file with another file C-x C-v
write current buffer to a specified file C-x C-w

 

Getting Help

 

The Help system is simple. Type M-? and follow the directions. If you are a first-time user, type M-? t for a tutorial.

get rid of Help window C-x 1
scroll Help window M-C-v
apropos(show commands matching a string) M-? a
show the function a key runs M-? c
describe a function M-? f
get mode-specific information M-? m

 

Error Recovery

 

abort partially typed or executing command C-g
recover a file lost by a system crash M-x recover-file
undo an unwanted change C-x u (or C-_)
restore a buffer to its original contents M-x revert-buffer
redraw garbaged screen C-l

 

Incremental Search

 

search forward C-s
search backward C-r
regular expression search C-M-s

Use C-s or C-r again to repeat the search in either direction;

exit incremental search Meta key
abort current search C-g

If Emacs is still searching, C-g will cancel the part of the search not done; otherwise it aborts the entire search.

 

Motion

 

Cursor motion 

entity to move over backward forward
character C-b C-f
word M-b M-f
line C-p C-n
go to line beginning (or end) C-a C-e
go to line n M-x goto-line Return key n
sentence M-a M-e
paragraph M-[ M-]
page C-x [ C-x ]
go to buffer beginning (or end) M-< M->


Screen motion

scroll to next screen C-v
scroll to previous screen M-v
scroll left C-x <
scroll right C-x >

 

Killing and Deleting

 

entity to kill backward forward
character(delete, not kill) Backspace C-d
word M-Backspace M-d
line, to beginning of (to end of) M-0 C-k C-k
sentence C-x Backspace M-k

 

kill region C-w
kill to next occurrence of char M-z char
yank back last thing killed C-y
replace last yank with previous kill M-y

Marking

set mark here C-Space (or C-@)
exchange point and mark C-x C-x
set mark arg words away M-@
mark paragraph M-h
mark page C-x C-p
mark entire buffer C-x h

 

Query Replace

 

interactively replace a text string M-%
using regular expressions M-x query-replace-regexp

Valid responses in query-replace mode are:

replace this one, go on to next Space
replace this one, don't move ,
skip to next without replacing Backspace
replace all remaining matches !
back up to the previous match ^
exit query-replace Meta key
enter recursive edit (C-M-c to exit) C-r

 

Multiple Windows

 

delete all other windows C-x 1
delete this window C-x 0
split window in 2 vertically C-x 2
split window in 2 horizontally C-x 5
scroll other window C-M-v
switch cursor to another window C-x o
shrink window shorter M-x shrink-window
grow window taller C-x ^
shrink window narrower C-x {
grow window wider C-x }
select a buffer in other window C-x-b

 

Formatting

 

indent current line Tab
indent region C-x Tab
unindent region C-M-\
move rest of line vertically down C-M-o
delete all whitespace around point M-n

 

Case Change

 

uppercase word M-u
lowercase word M-l
capitalize word M-c
uppercase region C-x C-u
lowercase region C-x C-l
capitalize region M-x capitalize-region

 

Buffers

 

select another buffer C-x b
list all buffers C-x C-b
kill a buffer C-x k

 

Transposing

 

transpose words M-t
transpose lines C-x C-t

 

Spelling Check

 

check spelling of current word M-$
check spelling of all words in region M-x spell-region
check spelling of entire buffer M-x spell-buffer

 

Shells

 

execute a shell command M-!
run a shell command on the region M-|
start a shell in window *shell* M-x shell

 

Rmail

 

enter Rmail M-x rmail
scroll forward Space
scroll reverse Backspace
beginning of message .(dot)
next non-deleted message n
previous non-deleted message p
next message M-n
previous message M-p
delete message d
delete message and back up C-d
undelete message u
reply to message (C-c C-c to finish) r
forward message to someone (C-c C-c to finish) f
send mail (C-c C-c to finish) m
get newly-arrived mail g
output message to another Rmail file o
output message in Unix-mailstyle C-o
show summary of headers h
quit Rmail q

 

Key Mapping

 

The following Emacs commands have been mapped to keys on a station or on an X terminal.

Scroll to previous screen PrevScreen
Scroll to next screen NextScreen
Move to beginning of file 7 on keypad
Move to end of file 1 on keypad
Goto line Select
Search Find