Page i
GNU Emacs
Pocket Reference
Debra Cameron
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
Page ii
GNU Emacs Pocket Reference
by Debra Cameron
Copyright 1999 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Editor: Gigi Estabrook
Production Editor: Claire Cloutier LeBlanc
Production Services: Omegatype Typography, Inc.
Cover Design: Edie Freedman
Printing History:
January 1999:
First Edition
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered
trademarks of O'Reilly & Associates, Inc. The association between the image of a gnu and the
topic of GNU Emacs is a trademark of O'Reilly & Associates, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O'Reilly &
Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or
5
3. Search and Replace Operations
10
4. Using Buffers and Windows
15
5. Emacs as a Work Environment
19
6. Email and Newsgroups
24
7. Emacs and the Internet
27
8. Simple Text Formatting and Specialized Editing
29
9. Marking Up Text with Emacs
Emacs Commands
Emacs commands consist of a modifier, such as CTRL (CONTROL) or ESC (ESCAPE),
followed by one or two characters. Commands shown in this book abbreviate CTRL to C:
C-g
Hold down the CTRL key and press g.
Most Emacs manuals refer to the META key in addition to the CTRL key. Since most
keyboards don't have a META key, this book refers to ESC instead of META:
ESC x
Press ESC, release it, then press x.
It is entirely possible that your keyboard has a META key. On many keyboards, the ALT keys
function as the META key. If your keyboard does have a META key, it works like the CTRL
key described here—that is, you hold down the META key and press the desired key, such as
g.
Page 2
Conventions
UNIX commands, Emacs keystrokes, command names, menu options, and variables are shown
in boldface type.
Filenames are shown in italic type.
Buffer names, LISP code, C code, Emacs messages, and other excerpts from programs are
shown in constant width type.
Dummy parameters that you replace with an actual value are shown in italic type. (If they
appear within code, they are shown in constant width italic type.)
1.
Emacs Basics
A Word About Modes
(table continued from previous page)
Major Mode
Function
RMAIL mode
For reading and organizing mail
View mode
For viewing files but not editing
Shell mode
For running a UNIX shell within Emacs
Telnet mode
For logging in to remote systems
Outline mode
For writing outlines
Indented text mode
For indenting text automatically
Nroff mode
For writing LISP programs
LISP interaction mode
For writing and evaluating LISP expressions
Minor modes
In addition to major modes, there are also minor modes. These define a particular aspect of
Emacs behavior and can be turned on and off within a major mode.
Minor Mode
Function
Auto-fill mode
Enables word wrap
Overwrite mode
Replaces characters as you type instead of
inserting them
Auto-save mode
Saves your file automatically every so often
in an auto-save file
(table continued on next page)
Start Emacs
emacs
Edit a specific file in Emacs
emacs filename
Exit Emacs
C-x C-c
save-buffers-kill-emacs
Suspend Emacs temporarily
C-z
suspend-emacs
Working with Files
To
Keystrokes Command Name
Open a file
C-x C-f
find-file
Open a different file instead
To
Keystrokes Command Name
Move to an existing buffer
C-x b buffername
switch-to-buffer
Display the buffer list
C-x C-b
list-buffers
Letting Emacs Fill in the Blanks
Emacs has a very helpful feature known as completion. If you open an existing file, type only
the first few letters of the name, enough to make a unique filename. Press TAB, and Emacs
completes the filename for you. Completion also works for long command names.
2.
Editing Files
Working in Text Mode
Text mode is the standard mode for typing text. By default, Emacs does not do word wrap,
instead creating very long lines. To enable word wrap, type ESC x auto-fill-mode RETURN.
You may decide that you want to enter auto-fill mode automatically whenever you edit. If so,
add this line to the Emacs startup file, .emacs, which is located in your home directory. (If the
startup file doesn't exist, create it.)
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
creates a newline)
C-n
next-line
Forward one word
ESC f
forward-word
Backward one word
ESC b
backward-word
To the beginning of the line
C-a
beginning-of-line
To the end of the line
C-e
end-of-line
Forward one screen
C-v
scroll-up
universal-argument
Repeat the following command 16
times
C-u C-u
universal-argument
(table continued on next page)
Page 7
(table continued from previous page)
To
Keystrokes Command Name
Repeat a complex command (can edit
arguments)
C-x ESC ESC
repeat-complex-command
Recall previous command in
minibuffer
ESC p
previous-history-element
Cutting Text
C-k
kill-line
Marking Text to Delete, Move, or Copy
In Emacs, you mark regions of text, which you can then delete, move, or copy. A region is the
area between the point (the cursor) and the mark (which you set).
Page 8
To
Keystrokes
Command Name
Set the mark (beginning or end of a
region)
C-@ or C-SPACE
set-mark-command
Delete marked text
C-w
kill-region
Copy a region
ESC w or C-Insert
kill-ring-save
Copy region to the kill ring
Right mouse button
x-cut-text
Cut region
C-middle button
x-cut-and-wipe-text
Split window vertically
C-right button
x-mouse-select-and-split
Copy region to the kill ring
S-middle button
x-cut-text
Paste text
S-right
x-paste-text
Delete all other windows but this one
C-S-right button
x-mouse-keep-one-window
ESC u
upcase-word
Lowercase word
ESC l
downcase-word
Stopping Commands
When you want to stop any command that's in progress, press C-g. The word Quit appears in
the command area.
Undoing Edits
What happens if you make a mistake while you're editing? You can undo your changes by
pressing C-x u.
What if you'd like to redo a command after you type undo? There is no formal redo command,
but you can use undo in the following way. Move the cursor in any direction, and type C-x u
again. Emacs redoes the last command. You can repeat it to redo previous undos.
Although undo is an important command, it can be slow if you want to undo a large number of
changes. The following table summarizes three methods for undoing changes and the
circumstances for their use.
Page 10
If you
Use this command
Don't like the recent changes you've
made and want to undo them one by one
3.
Search and Replace Operations
Incremental Search
An incremental search begins when you type the first letter and
continues searching as you add characters.
Page 11
To
Keystrokes Command Name
Incremental search forward
C-s
isearch-forward
Incremental search backward
C-r
isearch-backward
Exit incremental search
RETURN
Cancel incremental search
C-g
keyboard-quit
Delete incorrect character of search
Keystrokes Command
Name
Enter query-replace
ESC %
query-replace
Replace and go on to the next instance
SPACE or y
Don't replace; move on to next instance
DEL or n
Replace the current instance and quit
.
Replace and pause (SPACE or y to move on)
,
(table continued on next page)
Page 12
(table continued from previous page)
To
ESC C-c
Exit recursive edit and exit query-replace
C-]
Regular Expression Search and Replace
This section lists characters for creating regular expressions, followed by commands for
replacement operations using regular expressions.
Characters for creating regular expressions
Symbol
^
Matches
The beginning of a line
The end of a line
.
Any single character (like ? in filenames)
.*
Any group of zero or more characters (like ∗ in filenames)
\
Search incrementally forward for a
regular expression
ESC C-s
isearch-forward-regexp
Repeat incremental regular
expression search
C-s
isearch-forward
expression search
isearch-forward
Search incrementally backward for a
regular expression
ESC C-r
isearch-backward-regexp
Query-replace a regular expression
ESC x query-replace-regexp
Globally replace a regular expression
unconditionally (use with caution)
ESC x ispell-continue
Kill the spellcheck process
ESC x ispell-kill-ispell
List possible completions for current
word (text mode)
ESC TAB
ispell-complete-word
Page 14
Word Abbreviations
Abbreviations can be used as shortcuts for long words or defined as the correct spelling for
commonly misspelled words.
Global abbreviations work in every major mode; local abbreviations work only in the mode in
which they were defined. To define abbreviations, you must first enter word abbreviation
mode.
To
Keystrokes Command Name
Enter (or exit) word abbreviation
mode
ESC x abbrev-mode
Define a global abbreviation
ESC x kill-all-abbrevs
To add word abbreviations to your startup, insert these lines in your .emacs file:
(setq-default abbrev-mode t)
(read-abbrev-file “~/.abbrev_defs”)
(setq save-abbrevs t)
After making these modifications, restart Emacs and define an abbreviation. Ignore the error
message that appears. During this first session, you will need to issue the following
Page 15
command: ESC x write-abbrev-file RETURN~/.abbrev_defs RETURN. After this initial
session, this file will be loaded and saved automatically.
4.
Using Buffers and Windows
Using Buffers
To
Keystrokes
Command Name
Move to the buffer specified
C-x b buffername
switch-to-buffer
Display the buffer list
C-x C-b
Move to the next buffer
SPACE or n
Move to the previous buffer
p
Mark buffer for deletion
d or k
Save buffer
s
Unmark buffer
u
Execute other one-letter commands on all marked
buffers
x
Unmark the previous buffer in the list
DEL
o
Mark buffers to be displayed in windows
m
Display buffers marked with m; Emacs makes as
many windows as needed
v
Quit buffer list
q
Using Windows
Windows subdivide the current Emacs window. To create new Emacs windows, see the
following section on Frames.
To
Keystrokes Command Name
Create two windows, one on top of
the other
C-x 2
split-window-vertically
Find a file in another other window
C-x 4 f
find-file-other-window
Delete buffer and window (asks for
confirmation)
C-x 4 0
kill-buffer-and-window
Page 17
Using Frames
Frame commands apply only when Emacs is running under a GUI, such as X Windows.
To
Keystrokes
Command Name
Make a new frame
C-x 5 2
make-frame-command
Move to another frame
C-x 5 o
other-frame
C-x r m
bookmark-set
Jump to a bookmark
C-x r b
bookmark-jump
Rename a bookmark
ESC x bookmark-rename
Delete a bookmark
ESC x bookmark-delete
Save bookmarks
ESC x bookmark-save
Move to bookmark list
C-x r l
bookmark-bmenu-list
(table continued on next page)
Page 18
(table continued from previous page)
d
Rename bookmark
r
Save all bookmarks listed
s
Display the bookmark the cursor is on
f
Mark bookmarks to be displayed in multiple windows
m
Display marked bookmarks or the one the cursor is on if none
is marked
v
Toggle display of paths to files associated with bookmarks
t
Display location of file associated with bookmark
Command Name
Enter shell mode
ESC x shell
Interrupt current job; equivalent to
C-c in UNIX shells
C-c C-c
comint-interrupt-subjob
Delete a character; if at end of buffer
send an EOF character
C-d
comint-delchar-or-maybe-eof
Send EOF character
C-c C-d
comint-send-eof
comint-send-eof
Erase current line; C-u in UNIX
shells
C-c C-u
comint-kill-input
(table continued on next page)
Page 20
(table continued from previous page)
To
Keystrokes
Command Name
Delete output from last command
C-c C-o
comint-kill-output
Move last line of output to bottom of
window
C-c C-e
comint-show-maximum-output
Working with Dired
Dired is Emacs's directory editor. It provides a convenient way to manage files and
directories.
To
Keystrokes
Command Name
revert-buffer
Change group permissions
G
dired-do-chgrp
Remove line from display (don't
delete it)
k
Mark with ∗
m
dired-do-kill-lines
dired-mark
Move to the next line
n
dired-next-line
Find file in another window; move
there
o
dired-find-file-other-window
Rename file
R
dired-do-rename
Unmark file
u
dired-unmark
View file
v
dired-view-file
dired-view-file
Delete files flagged with D
x
dired-do-flagged-delete
Compress file
Z
dired-do-compress
Unmark all files (no matter what the
mark was)
=
Compare this file with its backup file
ESC =
dired-backup-diff
Execute shell command on this file
!
dired-mark-directories
dired-diff
dired-do-shell-command
Move to the next file marked with ∗
or D
ESC }
dired-next-marked-file
(table continued on next page)
Page 22
(table continued from previous page)
To
>
dired-next-dirline
Move to previous directory
calendar-print-day-of-year
Scroll the other window
SPACE
scroll-other-window
Quit calendar
q
exit-calendar
Add a weekly entry based on the day
of the week
iw
insert-weekly-diary-entry
of the week
Add an annual entry
insert-weekly-diary-entry
iy
insert-yearly-diary-entry
Add an entry for a particular day
id
insert-diary-entry
horizontally
C-SPACE or C-@
calendar-set-mark
Page 24
6.
Email and Newsgroups
Sending Mail
To
Keystrokes
Compose a mail message
C-x m
Compose a mail message in another window
C-x 4 m
Compose a mail message in another frame
C-x 5 m
Insert contents of the .signature file
C-c C-w