The best short emacs reference is found in the emacs chapter of OReillys UNIX in a Nutshell. Other references may be found below.
Basic commands
Commands for searching in lexica
Moving around
Every command has a long name, which you can look up in the
documentation, like kill-line
,
delete-backward-char
, or
self-insert-command
. These commands are bound
to keystrokes for convenient editing. We call such a pairing of
keystroke and command a key binding.
The standard prefix commands are:
There's one Emacs command that can be used to execute any other command by typing it's long name: M-x. When you type M-x Emacs prompts you for the name of any command, and then executes it.
Not all keyboards provide a Meta key that sets the high order bit. On a PC running Emacs natively, the ALT key is used for Meta. But when using a PC to talk to a Unix box via some telecommunications program -- well, you guessed it -- the ALT key may not work for this.
But if you have no Meta key, all is not lost. You just use the ESC prefix. M-a becomes ESC a; C-M-f becomes ESC C-f (remember the equivalence of C-M-f and M-C-f and this will make sense).
There's only one trick: ESC is not a shift key. It's actually an ASCII character, not a key modifier. This means that you don't try to hold down ESC at the same time as the other key: use it as a prefix character and type it separately and distinctly. If you lean on it it's likely to autorepeat (like any other key) and you'll get very confused.
A true Meta is a wonderful thing for Emacs (it makes typing much faster), but it is possible to use ESC for years with no trouble.
find-file
. This is the main command used to read a
file into a buffer for editing. When you
execute this command, it prompts you for the name of the file (with
completion). Then it checks to see if
you're already editing that file in some buffer; if you are, it simply
switches to that buffer and doesn't actually read in the file from
disk again. If you're not, a new buffer is created, named for the
file, and initialized with a copy of the file. In either case the
current window is switched to view this buffer.
save-buffer
. This is the main command used to save a
file, or, more accurately, to write a copy of the current buffer out
to the disk, overwriting the buffer's file, and handling backup
versions.
save-some-buffers
. Allows you to save all your
buffers that are visiting files, querying you for each one and
offering several options for each (save it, don't save it, peek at it
first then maybe save it, etc).
switch-to-buffer
. Prompts for a buffer name and
switches the buffer of the current window to that buffer. Doesn't
change your window configuration. This command will also create a new
empty buffer if you type a new name; this new buffer will not be visiting
any file, no matter what you name it.
list-buffers
. Pops up a new window which lists all
your buffers, giving for each the name, modified or not, size in
bytes, major mode and the file the buffer is visiting.
scroll-up
. The basic command to scroll forward
(toward the end of the file) one screenful. By default Emacs leaves
you two lines of context from the previous screen.
scroll-down
. Just like C-v, but scrolls
backward.
other-window
. Switch to another window, making it
the active window. Repeated invocation of this command moves through
all the windows, left to right and top to bottom, and then circles
around again. Under a windowing system, you can use the left mouse
button to switch windows.
delete-other-windows
. Deletes all other windows
except the current one, making one window on the screen. Note that
this in no way deletes the buffers or files associated with the
deleted windows.
delete-window
. Deletes just the current window,
resizing the others appropriately.
split-window-vertically
. Splits the current window
in two, vertically. This creates a new window, but not a new
buffer: the same buffer will now be viewed in the two windows. This
allows you to view two different parts of the same buffer
simultaneously.
split-window-horizontally
. Splits the current window
in two, horizontally. This creates a new window, but not a new
buffer: the same buffer will now be viewed in the two windows. This
allows you to view two different parts of the same buffer
simultaneously.
scroll-other-window
. Just like C-v, but
scrolls the other window. If you have more than two windows,
the other window is the window that C-o would switch to.
emacswhen it comes up, you won't be editing any file. You can then use the file commands to read in files for editing. Alternatively, you can fire up Emacs with an initial file (or files) by saying:
emacs newfile.txt
To exit emacs, use the command C-x C-c. It will offer to save all your buffers and then exit.
You can also suspend Emacs (in the Unix sense of stopping it and
putting it in the background) with C-x C-z (which is bound
to suspend-emacs
). How you restart it is up to your
shell, but is probably based on the fg
command.
It may seem annoying to have to hit return at the end of long lines, but this is actually just the default for certain modes. The reason for this is that Emacs is a programmer's editor, and any editor that will insert line breaks without your telling it to isn't safe for editing code or data. In modes oriented towards text, Emacs does insert line breaks for you (Auto fill is such a mode)
When this happens, you just need to type C-g, which interrupts what Emacs is doing. This will get you out of any questions that Emacs may be asking you, and it will abort a partially typed key sequence (say if you typed C-x by mistake).
Because Emacs is fully recursive, you may occasionally need to type C-g more than once, to back out of a recursive sequence of commands. Also, if Emacs is really wedged (say, in a network connection to some machine which is down), typing three C-g's quickly is guaranteed to abort whatever's wedging you.
command-apropos
. Prompts for a keyword and then lists
all the commands with that keyword in their long name.
describe-key
. Prompts for a keystroke and describes
the command bound to that key, if any.
info
. Enters the Info hypertext documentation reader.
describe-mode
. Describes the current major mode and
its particular key bindings.
finder-by-keyword
. Runs an interactive
subject-oriented browser of Emacs packages.
help-with-tutorial
. Run the Emacs tutorial. This is
very helpful for beginners.
undo
,
invoked with C-_ (control underbar). C-_ is a
valid ASCII character, but some keyboards don't generate it, so you
can also use C-x u -- but it's more awkward to type, since
it's a two-character command.
The undo command allows you to undo your editing, back in time. It's handy when you accidentally convert all of a huge file to uppercase, say, or delete a huge amount of text. One keystroke changes everything back to normal.
We say Emacs has infinite undo because, unlike some editors, you can undo a long chain of commands, not just one previous one, even undoing through saves. We say Emacs has redo because you can reverse direction while undoing, thereby undoing the undo.
Once you get used to this feature you'll laugh at any editor that doesn't have it (unless you're forced to use it...). It's very important to get comfortable with undo as soon as possible; I recommend reading the undo section of the manual carefully and practicing.
There's one kind of argument that's so commonly accepted that there's a
special way to provide it: numeric arguments. Many
commands will interpret a numeric argument as a request to repeat that
many times. For example, the delete-char
command (bound
to C-d), which normally deletes one character to the right
of the cursor, will delete N characters if given a numeric
argument of N. It works with self-inserting commands too: try
giving a numeric argument to a printing character, like a hyphen.
To give a command a numeric argument of, say, 12, type C-u 12 before typing the command. If you type slowly, you'll see:
C-u 1 2-in the echo area. Then type C-d and you'll have given
delete-char
an argument of 12. You can type any number
of digits after C-u. A leading hyphen makes a negative
argument; a lone hyphen is the same as an argument of -1. If you
begin typing a numeric argument and change your mind, you can of
course type C-g to abort it.
Since one often isn't interested in precisely how many times a
command is repeated, there's a shorthand way to get numeric arguments
of varying magnitudes. C-u by itself, without any
subsequent digits, is equal to a numeric argument of 4. Another
C-u multiplies that by 4 more, giving a numeric argument of
16. Another C-u multiplies that by 4 more, giving a
numeric argument of 64, etc. For this reason C-u is called
the universal-argument
.
Note that commands aren't required to interpret numeric arguments as specifying repetitions. It depends on what's appropriate: some commands ignore numeric arguments, some interpret them as Boolean (the presence of numeric argument -- any numeric argument -- as opposed to its absence), etc. Read the documentation for a command before trying it.
quoted-insert
,
which is bound to C-q. C-q acts like a prefix
command, in that when you type it it waits for you to type another
character. But this next character is then inserted into the buffer,
rather than being executed as a command. So C-q ESC
inserts an Escape.
C-q can also be used to insert characters by typing C-q followed by their ASCII code as three octal digits.
All these motion commands take numeric arguments as repetitions.
The most basic textual object is the character. Emacs understand many other objects, sometimes depending on what mode you're in (a C function textual object probably doesn't make much sense if you're not editing C source code).
The exact definition of what makes up a given textual object is often customizable, but more importantly varies slightly from mode to mode. The characters that make up a word in Text Mode may not be exactly the same as those that make up a word in C Mode for example. (E.g., underbars are considered word constituents in C Mode, because they are legal in identifier names, but they aren't considered word constituents in Text Mode.) This is extremely useful, because it means that you can use the same motion commands and yet have them automatically customized for different types of text.
forward-char
. Moves forward (to the right) over a
character.
backward-char
. Moves backward (to the left) over a
character.
forward-word
. Moves forward over a word.
backward-word
. Moves backward over a word.
next-line
. Moves down to the next line.
previous-line
. Moves up to the previous line.
beginning-of-line
. Moves to the beginning of the
current line.
end-of-line
. Moves to the end of the current line.
backward-sentence
. Moves to the beginning of the
current sentence.
forward-sentence
. Moves to the end of the
current sentence.
backward-paragraph
. Move to the beginning of the
current paragraph.
forward-paragraph
. Move to the end of the
current paragraph.
backward-page
. Moves to the beginning of the current
page.
forward-page
. Moves to the end of the current
page.
beginning-of-buffer
. Moves to the beginning of the
buffer.
end-of-buffer
. Moves to the end of the
buffer.
But sexps are more than just balanced parens: they're defined recursively. A word that doesn't contain any parens also counts as a sexp. In most programming language modes, quoted strings are sexps (using either single or double quotes, depending on the syntax of the language). The sexp commands move in terms of all these units.
These commands may seem confusing at first, but for editing most programming languages they're fantastic. Not only do they move you around quickly and accurately, but they help spot syntax errors while you're editing, because they'll generate an error if your parens or quotes are unbalanced.
backward-sexp
. Moves backward over the next sexp.
If your cursor is just to the right of a left paren,
C-M-b will beep, because there's no sexp to the left to
move over: you have to move up.
forward-sexp
. Moves forward over the next sexp.
Same deal if your cursor is just to the left of a right paren.
backward-up-list
. Move backward up one level of
parens. In other words, move to the left paren of the parens
containing the cursor, skipping balanced sexps.
down-list
. Move down one level of
parens. In other words, move to the right of the next left paren,
skipping balanced sexps. E.g., if your cursor is sitting on the return
type of a C function declaration, C-M-d moves to the inside
of the formal parameter list.
beginning-of-defun
. Move to the beginning of the
current defun.
end-of-defun
. Move to the end of the current defun.
Killed text is saved on what is called the kill ring. The kill ring holds the last N kills, where N is 30 by default, but you can change it to anything you like by changing the value of the variable kill-ring-max. The kill ring acts like a fifo when you're killing things (after the 30th kill, kill number one is gone), but like a ring when you're yanking things back (you can yank around the ring circularly). kill-ring-max doesn't apply to the amount of text (in bytes) that can be saved in the kill ring (there's no limit), only to the number of distinct kills.
delete-char
. Deletes the character to the right of
(under, if the cursor is a block that covers a character) the cursor.
delete-backward-char
. Deletes the character to the left of
the cursor.
kill-word
. Kills to the end of the word to the right
of the cursor (forward).
backward-kill-word
. Kills to the beginning of the
word to the left of the cursor (backward).
kill-line
. Kills to the end of the current line, not
including the newline. Thus, if you're at the beginning of a line it
takes two C-k's to kill the whole line and close up the
whitespace.
kill-line
. Kills to the beginning of the
current line, not including the newline.
kill-sentence
. Kills to the end of the current
sentence, including any newline within the sentence.
kill-sentence
. Kills to the beginning of the current
sentence, including any newlines within the sentence.
forward-kill-paragraph
and
backward-kill-paragraph
exist, but are not bound to any
keys by default.
kill-sexp
. Kills the sexp after the cursor.
kill-sexp
. Kills the sexp before the cursor.
backward-kill-sexp
exists, but is not bound
to any key by default.
yank
). Since Emacs has only one kill ring (as
opposed to one per buffer), you can kill in one buffer, switch to
another and yank the text there.
To get back previous kills, you move around the kill ring. Start with C-y to get the most recent kill, and then use M-y to move to the previous spot in the kill ring by replacing the just-yanked text with the previous kill. Subsequent M-y's move around the ring, each time replacing the yanked text. When you reach the text you you're interested in, just stop. Any other command (a motion command, self-insert, anything) breaks the cycling of the kill ring, and the next C-y yanks the most recent kill again.
C-r, works the same way, but searches backward. (Use C-r to search for the next match and C-s to reverse the search.)
Another possibility is word search, which lets you search for a sequence of one or more words, regardless of how they're separated (e.g, by any number and combination of newlines and whitespace). To invoke word search, type C-s RET C-w word word word RET.
Emacs can also search incrementally (or not) by regular expressions. The command is C-u-s, and it understands regular expressions.
query-replace
(bound to M-%, type ESC,
and thereafter %). This command prompts you for the text to
replace, and the text to replace it with, and then searches and
replaces within the current buffer. query-replace
is
interactive: at each match, you are prompted to decide what to do; you
have the following options:
query-replace
without performing this
replacement.
query-replace
.
There are also more replacement commands you
should look into, including replace-string
(simple
unconditional replacement), replace-regexp
and
query-replace-regexp
(which use regular expressions), and
tags-query-replace
, which replaces all identifiers in a
collection of source code files.
query-replace-regexp
is very important to
us. The shortest way to type it is ESC que TAB - TAB. If you
e.g. want to add the text K ; to the end of each line of the
rest of the file, type ESC que TAB - TAB, then $ RET
(dollar followed by return, dollar means end-of-line), then K
; followed by return.
query-replace
and the other replacement commands are, by
default, smart about case. For example, if you're replacing
foo with bar and find Foo,
Emacs replaces it with Bar; if you find FOO,
Emacs replaces it with BAR, etc.
The region is the text between point and
mark. Point is actually the Emacs term for what we've been
calling the cursor up to now. The mark, on the other hand, is set with
a special command C-SPACEBAR (mellomromstasten,
välilyönti) (set-mark-command
). This sets the
mark exactly where point is, but now you can move point elsewhere and
you have: the region.
The region is the same regardless of whether point comes first in the buffer or mark does; it makes no difference, just do what's convenient.
Many commands that move point a significant distance (like M-< and C-s, for example) leave the mark set at the spot they moved from. You'll see "Mark set" in the echo area when this happens.
When using Emacs under a windowing system like X, the mouse can be used to sweep out the region, but many Emacsers find it faster to keep their hands on the keyboard and use the familiar motion commands.
So now you know how to define the region: what can you do with it?
exchange-point-and-mark
. Swaps mark and point.
Repeated rapid execution of this command makes it easy to see the
extent of the region.
kill-region
. Kills the region. It goes on the kill
ring, of course.
kill-ring-save
. Saves the region to the kill ring
without removing it from the buffer. This is exactly equivalent to
typing C-w C-y.
indent-rigidly
. Rigidly indents the region by as many
characters (columns) as you provide as a numeric argument (default is 1 column).
downcase-region
. Convert the entire region to
lowercase. This command is disabled by default.
upcase-region
. Convert the entire region to
uppercase. This command is disabled by default.
fill-region
. Fills, i.e., justifies with a ragged
right margin, all the paragraphs within the region.
C-q
followed by the octal code of the
character. For example, C-q 341 RET
gives the lower
case a with acute (á).
M-x
load-library
and respond to prompt with
iso-insert
. Then use commands M-x
insert-A-acute
for inserting capital A with acute
(Á).
un-define
M-x
insert-ucs-character
and giving the character's unicode number.