http://www2.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html
C-x Ctrl+x
M-x Alt+x
C-M-x Ctrl+Alt+x
RET RETURN
SPC Spac
ESC escape or C-[
C-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.
C-h
Used for Help commands.
C-x
This prefix is used mostly for commands that manipulate files, buffers and windows.
Files, Buffers and Windows
--------------------------
File
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.
Buffer
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).
Window
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
one minibuffer.
Frame
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
----------------------------
C-x C-f
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.
C-x C-s
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.
C-x s
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
------------------------------
C-x b
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.
C-x C-b
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.
C-x k
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.
C-x C-q
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
------------------------------
C-v
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
previous screen.
M-v
scroll-down. Just like C-v, but scrolls backwards.
C-x o
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.
C-x 1
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.
C-x 0
delete-window. Deletes just the current window, resizing the others appropriately.
C-x 2
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.
C-x 3
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.
C-M-v
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 Minibuffer
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.
C-g
keyboard-quit (like escape in normal editors)
C-h a
command-apropos. Prompts for a keyword and then lists all the commands with
that keyword in their long name.
C-h k
describe-key. Prompts for a keystroke and describes the command bound to
that key, if any.
C-h i
info. Enters the Info hypertext documentation reader.
C-h m
describe-mode. Describes the current major mode and its particular key bindings.
C-h p
finder-by-keyword. Runs an interactive subject-oriented browser of Emacs packages.
C-h t
help-with-tutorial. Run the Emacs tutorial. This is very helpful for beginners.
C-_ (control underbar)
C-x u
undo
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.
C-u
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.
C-q
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.
C-f
forward-char. Moves forward (to the right) over a character.
C-b
backward-char. Moves backward (to the left) over a character.
M-f
forward-word. Moves forward over a word.
M-b
backward-word. Moves backward over a word.
C-n
next-line. Moves down to the next line.
C-p
previous-line. Moves up to the previous line.
C-a
beginning-of-line. Moves to the beginning of the current line.
C-e
end-of-line. Moves to the end of the current line.
M-a
backward-sentence. Moves to the beginning of the current sentence.
M-e
forward-sentence. Moves to the end of the current sentence.
C-x [
backward-page. Moves to the beginning of the current page.
C-x ]
forward-page. Moves to the end of the current page.
M-
end-of-buffer. Moves to the end of the buffer.
C-M-b
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.
C-M-f
forward-sexp. Moves forward over the next sexp. Same deal if your cursor
is just to the left of a right paren.
C-M-u
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.
C-M-d
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.
C-M-a
beginning-of-defun. Move to the beginning of the current defun.
C-M-e
end-of-defun. Move to the end of the current defun.
NOTE:
Kill means Cut
Delete means Delete
C-d
delete-char. Deletes the character to the right of (under, if the cursor is
a block that covers a character) the cursor.
DEL
delete-backward-char. Deletes the character to the left of the cursor.
M-d
kill-word. Kills to the end of the word to the right of the cursor (forward).
M-DEL
backward-kill-word. Kills to the beginning of the word to the left of the cursor (backward).
C-k
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.
M-k
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.
C-M-k
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.
C-y (yank)
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.
Replacement
-----------
Replacing text is called query-replace (bound to M-%).
SPC
Perform this replacement.
DEL
Don't perform this replacement.
RET
Terminate query-replace without performing this replacement.
ESC
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),
replace-regexp
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.
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 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.
C-@ set-mark-command
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.
M-@
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.
M-h
mark-paragraph. Sets the region around the current paragraph.
C-M-@
mark-sexp. Sets the region around the same sexp that C-M-f would move to.
C-M-h
mark-defun. Sets the region around the current defun.
C-x C-p
mark-page. Sets the region around the current page.
C-x h
mark-whole-buffer. Sets the region around the entire buffer.
C-x C-x
exchange-point-and-mark. Swaps mark and point. Repeated rapid execution of
this command makes it easy to see the extent of the region.
C-w
kill-region. Kills the region. It goes on the kill ring, of course.
M-w
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.
C-x C-i
indent-rigidly. Rigidly indents the region by as many characters (columns)
as you provide as a numeric argument (default is 1 column).
C-x C-l
downcase-region. Convert the entire region to lowercase. This command is
disabled by default.
C-x C-u
upcase-region. Convert the entire region to uppercase. This command is
disabled by default.
M-x fill-region
fill-region. Fills, i.e., justifies with a ragged right margin, all the
paragraphs within the region.
Indentation
-----------
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.
Modes
-----
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
----------------
Fundamental Mode
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.
Text Mode
For editing text. Has special commands for spell-checking,
centering lines, etc.
Outline Mode
For editing a stylized type of outline. Implements folding of
outline levels, etc.
Lisp Mode
For editing Common Lisp source code. Has an interactive link to a
Common Lisp interpreter in another buffer.
Tcl Mode
For editing Tcl source code. Has an interactive link to a Tcl
interpreter in another buffer.
C Mode
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).
________________________________________________________________________________
* http://www2.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html
* http://www.cs.cmu.edu/cgi-bin/info2www?(emacs)
* http://www.amazon.com/Learning-Emacs-Third-Debra-Cameron/dp/0596006489
* http://www.amazon.com/Gnu-Emacs-UNIX-Editing-Programming/dp/0201563452