Emacs Cheat Sheet

This is a short tutor intended to get you started using emacs. In it,
you will learn the basics on how to move around within the file, how
to delete and add to the file, how to format the file, and how to save
and read files.

Emacs is a powerful screen editor. All commands are effected by a
combination of keystrokes making use of either the CONTROL or the
META keys. The CONTROL key will be designated as C-, the META key as
M-. Usually, the META key is the ESCAPE key. For example, you can
quit this session at any time by typing C-x C-c. If you make a
mistake entering a command, C-g will clear the command buffer. The
command buffer is displayed at the bottom of the screen, under the
line which has the file information.

Moving Within the File

It is important that one be able to move about the file quickly and
easily. To scroll from screen to screen, one uses C-v to move forward
and M-v to move backward. M-< will move to the beginning of the buffer and M-> will move to the end. C-l will center the current line
in the screen.

On some terminals, the arrow keys and/or the mouse may work, but this
is not true for all terminals. To move the cursor to the right or
left one character at a time, use C-b or C-f, respectively. To move
up one line, use C-p. C-n will move you down one line. When you move
up and down in the file, the cursor will remain in the same column if
possible. Now try moving the cursor around.

You can move the cursor from word to word, sentence to sentence, and
paragraph to paragraph. M-b will move the cursor to the beginning of
the previous word and M-f will move it to the beginning of the next
word. M-a will move to the beginning of the sentence and M-e will
move to the end of the sentence. M-[ will move backward to the
paragraph beginning and M-] will move to the paragraph end. Finally,
C-a and C-e will move the cursor to the beginning or end of the
current line. Use these commands to position the cursor at various
points within the file.

In emacs, one can also move the cursor to particular strings. This
search capability comes in several flavors. We will concentrate on
the incremental search. Position this paragraph so that all of it can
be seen on the screen and place the cursor at the end of this
sentence. Make sure that the cursor is not near the bottom of the
screen. Type C-r (backward search). You will find the following
message on the command line – ‘I-search backward:’. Now start typing
the word "particular", keeping the following questions in mind. What
happens to the cursor when you type the first character? The cursor
moves to the word "place" at the beginning of the sentence the
cursor was originally on. When you type the next character, the
cursor moves backward to the first word containing the string "pa".
In an incremental search, the search remains active until you hit
ESCAPE (leaves the cursor where it is) or C-g, which returns the
cursor to its original position. Try leaving the search using both
methods.

Now place the cursor at the beginning of this paragraph. Do a
backward search for "particular" again. The cursor moves to the first
occurence of the string that it finds. To move to the next occurence
while you are still in search mode, do another C-r. (Hit C-g to
return here.) C-s can be used to do a forward incremental search.

Deleting and Adding Text

Now that you know how to get around in the file, you will probably
want to do some real editting. This section will focus on the
deletion and addition of text, plus some aspects of formatting.

To delete a single character, type C-d to kill the current character.
The DELETE key will kill the character before the cursor. (NOTE: The
DELETE key and the BACKSPACE key are different on many keyboards.)
M-d will delete from the cursor to the end of the current word. Going
backward, M-DELETE will delete from the beginning of the current word
to but not including the cursor.

To delete to the end of the line, hit C-k. To delete to the beginning
of the line is somewhat trickier, you need to hit M-0 C-k (that’s a
zero, not the letter O). To delete to the end of a sentence, hit M-k.
Use C-x DELETE to delete to the beginning of a sentence.

Below is a sample paragraph which contains some extra characters or
words. Practice deleting on the mistakes in the next paragraph.

Deleting in emacs does not have to be difficult. For example, this
sentence can be deleted by placing the cursor at the beginning and
typing M-k. Another thing you can do is kill the word repeated in
this sentence twice twice. This sentence containes words with exxtra
letterrs in themm.

One can also mark a region to be killed. To do this, place the cursor
at one end of the region and set the mark by typing C-@. Now position
the cursor at the other end of the region and hit C-w. If you find
that you deleted something that you really wanted to keep, C-y will
yank back the last thing killed.

Being able to delete text is important, but it is also nice to add
text. This can be done by placing the cursor where you want to insert
text and start typing. When you place text in the middle of a line,
you will notice that the line will spill over onto the next line. You
can format the paragraph containing the cursor by typing M-q.

Another tool that comes in handy when editting is the search and
replace option. To replace with at every
occurrence after the cursor, simply type M-x replace-string RETURN,
then type in the string to be replaced followed by a RETURN, and then
the new string followed by a RETURN. The cursor will remain at the
last occurrence of replacement. To move back to the original
location, type C-u C-SPACEBAR. If you want to replace only certain
occurrences of the old string, use the command M-x
query-replace-string.

Try search and replace in the following example. In this example, you
will search for the term "rust-capped" and replace all occurrences with the
term "chestnut-sided". After you are done, use the M-q command to
re-format the paragraph.

In the spring time, the great migration is underway. Birds of all
sizes and shapes head northward to their breeding grounds. We enjoy
the sparrows, especially the rust-capped sparrows. Of course, the
warblers add much color to the audible fest. The rust-capped warblers
seem to be most populous in our area.

Saving and Reading Files

Now that you know how to edit a file, you will want to be able to save
the file. There are several ways to do this. The easiest is to
simply exit emacs using C-x C-c. You will be prompted by emacs to let
it know whether you really want to save the file or not.

At times, especially when editting a long file, you may wish to save
the file without exitting. This can be done by typing C-x C-s. You
will be prompted before the file is saved. You will also be prompted
when you commence editting. Should you wish to save the buffer into
another file, use C-s C-w.

Emacs allows you to work on more than one buffer. To read in a file
while in emacs, hit C-x C-f and enter the file name. This will open a
new buffer and insert the file into that buffer. You can list all of
the buffers currently open by typing C-x C-b. This will split the
screen and display the buffer information on the bottom. To get rid
of the bottom window, type C-x 1. To select another buffer, you can
use the C-x b command. C-x k will kill the buffer.

Other Emacs Commands To Know

Emacs allows you to suspend the editting session and return to the
shell. You can do this by typing C-z. This actually puts emacs in
the background. To get it back into the foreground, type fg or
%emacs.

Emacs has an extensive help facility. To access it, type C-h. Follow
the directions given in the command buffer. C-h C-h will provide help
on using the help facility. While in help, you will be able to a list
of commands whose names contain a string, display a table of all key
bindings in effect, print the name of the command that a key runs,
etc. C-h i will run Info, a program for browsing documentation files.
Info includes the complete Emacs manual. There is also an extensive
tutorial for emacs in the help facility. Type C-h t to access the
tutor.

When the help facility is invoked, it is brought into its own buffer.
You can see this by typing C-x C-b to display the buffers. To exit
help and get back to your original buffer, type C-x b and enter the
buffer name if it is different from the default name given.

Emacs has many other features that are not described here. You will
find many listed in the tutorial and many more in the manual. Below
is a synopsis of the commands as given in the GNU Emacs Manual.
——————————————————————–
Summary of Emacs Commands

Starting emacs:
start up only emacs
start up with a file emacs filename

Leaving emacs:
suspend Emacs C-z
exit Emacs permanently 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 a file into this buffer C-x i
replace this file with another C-x C-v
write buffer to a specified file C-x C-w
run the directory editor C-x d

Getting help:
starting HELP C-h
get rid of HELP window C-x 1
scroll HELP window ESC C-v
apropos C-h a
show function a key runs C-h c
describe a function C-h f
get mode-specific information C-h m

Error Recovery:
abort partially typed command C-g
recover file lost by system crash M-x recover-file
undo an unwanted change C-x u or C-_
restore buffer to original 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
exit incremental search ESC
undo effect of last character DEL
abort current search C-g

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/end C-a C-e
sentence M-a M-e
paragraph M-[ M-]
page C-x [ C-x ]
sexp C-M-b C-M-f
function C-M-a C-M-e
go to buffer beginning/end M-< M->

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) DEL C-d
word M-DEL M-d
line(to end of) M-O C-k C-k
sentence C-x DEL M-k
sexp M– C-M-k C-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-@ or C-SPC
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 sexp C-M-@
mark function C-M-h
mark entire buffer C-x h

Query Replace:
interactively replace a string M-%
using regular expressions M-x query-replace-regexp

Valid responses in query-replace mode are

replace this one, go on to next SPC
replace this one, don’t move ,
skip to next without replacing DEL
replace all remaining matches !
back up to the previous match ^
exit query-replace ESC
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 4 b
find file in other window C-x 4 f
compose mail in other window C-x 4 m
run Dired in other window C-x 4 d
find tag in other window C-x 4 .

Formatting:
indent current line TAB
indent region C-M-\
indent sexp C-M-q
indent region rigidly arg columns C-x TAB
insert newline after point C-o
move rest of line vertically down C-M-o
delete blank lines around point C-x C-o
delete all white space around point M-\
put exactly one space at point M-SPC
fill paragraph M-q
fill region M-g
set fill column C-x f
set prefix each line starts with C-x .

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

The Minibuffer:
The following keys are defined within the minibuffer.
complete as much as possible TAB
complete up to one word SPC
complete and execute RET
show possible completeions ?
abort command C-g
Type C-x ESC to edit and repeat the last command that
used the minibuffer. The following keys are then
defined.
previous minibuffer command M-p
next minibuffer command M-n

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

Transposing:
transpose characters C-t
transpose words M-t
transpose lines C-x C-t
transpose sexps C-M-t

Spelling Check:
check current word M-$
check region M-x spell region
check buffer M-x spell buffer

Tags:
find tag M-.
find next occurrence of tag C-u M-.
specify a new tags file M-x visit-tags-table
regexp search on all files in tags table M-x tags-search
query replace on all the files M-x tags-query-replace
continue last tags search or query-replace M-,

Shells:
execute a shell command M-!
run a shell command on the region M-|
filter region through a shell command C-u M-|
start a shell in window *shell* M-x shell

Rmail:
scroll forward SPC
scroll reverse DEL
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 r
forward message to someone f
send mail m
get newly arrived mail g
quit Rmail q
output message to another Rmail file o
output message in Unix-mail style C-o
show summary of headers h

Regular Expressions:
The following have special meaning inside a regular expression.
any single character . (dot)
zero or more repeats *
one or more repeats +
zero or one repeat ?
any char in set [ … ]
any char not in set [^ … ]
beginning of line ^
end of line $
backslash \\
alternative ("or") \|
grouping \( … \)
nth group \n
beginning of buffer \`
end of buffer \’
word break \b
not beginning or end of word \B
beginning of word \< end of word \>
any word-syntax char \w
any non-word-syntax char \W
char with syntax c \sc
char with cyntax not c \Sc

Registers:
copy region to register C-x x
insert register contents C-x g
save point in register C-x /
move point to saved location C-x j

Info:
Enter the Info documentation reader C-h i
Moving within a node:
scroll forward SPC
scroll backward DEL
beginning of node . (dot)

Moving between nodes:
next node n
previous node p
move up u
select menu item by name m
select nth menu item by number (1-5) n
follow cross reference (return with l) f
return to last node you saw l
return to directory node d
go to any node by name g

Other:
run Info tutorial h
list Info commands ?
quit Info q
search nodes for regexp s

Keyboard Macros:
start defining a keyboard macro C-x (
end keyboard macro definition C-x )
execute last-defined keyboard macro C-x e
append to last keyboard macro C-u C-x
name last keyboard macro M-xname-last-kbd-macro
insert lisp definition in buffer M-x insert-kbd-macro

Commands Dealing with Emacs Lisp:
eval sexp before point C-x C-e
eval current defun C-M-x
eval region M-x eval-region
eval entire buffer M-x eval-current-buffer
read and eval minibuffer M-ESC
re-execute last minibuffer command C-x ESC
read and eval Emacs Lisp file M-x load-file
load from standard system directory M-x load-library

PJV 4/20/1990

Leave a Reply

Your email address will not be published. Required fields are marked *