ESC escape or C-[
Used for commands that are specific to particular modes, so they are free
to be used for different commands depending on context. These are the
most variable of Emacs commands.
Used for Help commands.
This prefix is used mostly for commands that manipulate files, buffers and windows.
Files, Buffers and Windows
A file is the actual Unix file on disk. You are never editing this file.
Rather, you can read a copy into Emacs to initialize a buffer, and
write a copy of a buffer out to a file to save it.
A buffer is the internal data structure that holds the text you
actually edit. Emacs can have any number of buffers active at any
moment. Most, but by no means all, buffers are associated with a file.
Buffers have names; a buffer that has been initialized from a file is
almost always named for that file, and we say that the buffer is
visiting the file. This means, in particular, that when you save the
buffer, it's saved to the proper file. At any given time exactly one
buffer is selected: this is the buffer that your hardware cursor is
in, and this is where commands you type take effect (including
self-insert commands). Buffers can be deleted at will; deleting a
buffer in no way deletes the file on disk (though you may lose any
editing changes you made if you don't save first).
A window is your view of a buffer. Due to limited screen real-estate,
you may not have room to view all your buffers at once. You can split
the screen, horizontally or vertically, into as many windows as you
like (or at least have room for), each viewing a different buffer.
It's also possible to have several windows viewing different portions
of the same buffer. Windows can be created and deleted at will;
deleting a window in no way deletes the buffer associated with the
window. Each window has its own Mode Line, but there's still only
A frame is like a window, but is treated as a separate entity under a
windowing system like X. I won't be discussing frames.
Commands to Manipulate Files
find-file. This is the main command used to read a file into a
buffer for editing. It's actually rather subtle. 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).
Commands to Manipulate Buffers
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.
kill-buffer. Prompts for a buffer name and removes the entire data structure
for that buffer from Emacs. If the buffer is modified you'll be given an
opportunity to save it. Note that this in no way removes or deletes
the associated file, if any.
vc-toggle-read-only. Make a buffer read-only (so that attempts to modify it
are treated as errors), or make it read-write if it was read-only. Also,
if the files is under version control, it will check the file out for you.
Commands to Manipulate Windows
scroll-up. The basic command to scroll forward (towards the end of the file)
one screenful. By default Emacs leaves you two lines of context from the
scroll-down. Just like C-v, but scrolls backwards.
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-x o
would switch to.
The state of the buffer,
one of modified (indicated by a pair of asterisks **),
unmodified (hyphens --), or
read-only (indicated by a pair of % signs)
The blank line below the mode line is the minibuffer. The minibuffer is used
by Emacs to display messages, and also for input when Emacs is prompting
you to type something.
keyboard-quit (like escape in normal editors)
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.
C-_ (control underbar)
To invoke completion, you usually type TAB.
Emacs offers various forms of completion: http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Completion
To give a command a numeric argument of, say, 12, type C-u 12 before typing the command.
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.
Sometimes one needs to insert control characters into a file. But how can you
insert an ESC, say, when it's used as a prefix command? The answer is to use
the command 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.
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-page. Moves to the beginning of the current page.
forward-page. Moves to the end of the current page.
end-of-buffer. Moves to the end of the buffer.
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.
Emacs has commands to move over functions. Like the sexp commands, these commands
work appropriately in most programming language modes. Emacs calls this generic
notion of function or procedure defun, again after Lisp.
beginning-of-defun. Move to the beginning of the current defun.
end-of-defun. Move to the end of the current defun.
Kill means Cut
Delete means Delete
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.
C-u 0 C-k
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.
C-u -1 M-k
kill-sentence. Kills to the beginning of the current sentence, including any
newlines within the sentence.
kill-sexp. Kills the sexp (balanced parentheses) after the cursor.
C-u -1 C-M-k
kill-sexp. Kills the sexp (balanced parentheses) before the cursor.
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.
Searching and Replacing
C-s isearch-forward incremental search
To stop searching, you can either hit RET or type any other Emacs command
(which will both stop the search and execute the command).
C-r isearch-backward, incremental searche backward.
(Use C-r to search for the next match and C-s to reverse the search.)
To invoke word search, type C-s RET C-w word word word RET.
Replacing text is called query-replace (bound to M-%).
Perform this replacement.
Don't perform this replacement.
Terminate query-replace without performing this replacement.
Same as RET.
Perform this replacement but then terminate the query-replace.
Perform this replacement and all the rest in the buffer without querying (ie unconditionally).
replace-string (simple unconditional replacement),
query-replace-regexp (which use regular expressions),
tags-query-replace, which replaces all identifiers in a collection of source code files.
query-replace and the other replacement commands are, by default, smart about case.
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 Mark and The Region
Emacs can manipulate arbitrary chunks of text as well as distinct textual objects.
The way this is done is to define a region of text; many commands will operate
on this region.
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 is set with a special command C-@ (set-mark-command). This sets the
mark exactly where point is, but now you can move point elsewhere and
you have: the region.
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.
mark-word. Sets the region around the next word, or from point to the end of
the current word, if you're in the middle of one.
mark-paragraph. Sets the region around the current paragraph.
mark-sexp. Sets the region around the same sexp that C-M-f would move to.
mark-defun. Sets the region around the current defun.
mark-page. Sets the region around the current page.
mark-whole-buffer. Sets the region around the entire buffer.
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.
In programming language modes, Emacs uses the TAB key to indent a line
automatically, in accordance with indentation rules for your programming
language. For example, in C Mode Emacs understands if, while, do, for,
functions, switch, etc and indents appropriately. Of course, there are no
hard and fast rules for indentation in most languages, so Emacs allows you
to customize the indentation for your own style.
The main way Emacs customizes commands for different kinds of text is through
major and minor modes. Every buffer has a major mode, and may have zero or more
minor modes. Sometimes Emacs chooses a major mode for you automatically,
typically based on a file extension (e.g., files ending in .c will automatically
be in C Mode; files ending in .tcl will automatically be in Tcl Mode). But you
can always set the mode explicitly.
Some Major Modes
The basic mode in reference to which all specialized modes are defined.
Perfectly fine for editing any kind of text, just doesn't provide
any special features.
For editing text. Has special commands for spell-checking,
centering lines, etc.
For editing a stylized type of outline. Implements folding of
outline levels, etc.
For editing Common Lisp source code. Has an interactive link to a
Common Lisp interpreter in another buffer.
For editing Tcl source code. Has an interactive link to a Tcl
interpreter in another buffer.
For editing C source code. Has special indentation, etc.
There are many other major modes, some very specialized (e.g., modes for
editing sending email, reading Usenet news, browsing directories,
browsing the World Wide Web, etc).