This note is essentially an excerpt from A Tutorial Introduction to Emacs by Keith Waclena.
To set up emacs, you need to download a gzipped file, emacs.tar.gz, which we customized for the math prefresher. This zip file contains .emacs file as well as various mode files. .emacs file is the configuration file for your emacs. You can modify it to meet your own needs and preferences. The mode files such as ``Emacs Speak Statistics'' will be used in the Government Department quantitative courses as well as in the math prefresher.
Place this gzipped file in your home directory and issue the follow commands to unzip it; gunzip -v emacs.tar.gz and then tar -xvf emacs.tar or alternatively you can do tar -zxvf emacs.tar.gz to do it by one command.
To start emacs, simply type emacs & at the command prompt. You can also type emacs filename & to open an existing file or create a new one.
For the first time you open emacs, you need to compile your configuration. Open emacs at the home directory by typing emacs .emacs & and then compile this file by using the menu bar, Emacs-Lisp, and then choose Byte-compile This file. Close emacs by using the menu bar, Files.
The minibuffer is also known as the echo area, because Emacs echoes keystrokes here if you're typing really slowly. To see this, type any multi-character keystroke (like, ESC q) with a long pause between the keystrokes.
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.
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.
scroll-down. 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.
scroll-up. 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-window. Deletes the current window.
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.
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.
isearch-forward,
bound to C-s, does: it searches incrementally, one
character at a time, as you type the search string. This means that
Emacs can often find what you're looking for before you have to type
the whole thing. To stop searching, you can either hit RET
or type any other Emacs command (which will both stop the search and
execute the command). You can search for the next match at any point
by typing another C-s at any point; you can reverse the
search by typing C-r; and you can use DEL to
delete and change what you're searching for.
isearch-backward, bound to 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.)
Occasionally you may want to search non-incrementally (though I rarely do). You can do this by typing C-s RET text RET, where text is the text to search for.
Much more useful 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; this is an extremely powerful feature, but too complex to describe here.
query-replace (bound to M-%). 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.
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.
Killed text is saved on 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-next-word. Kills to the end of the word to the right
of the cursor (forward).
kill-last-word. Kills to the beginning of the
word to the left of the cursor (backward).
kill-forward-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-back-line. Kills to the beginning of the
current line, not including the newline.
kill-forward-sentence. Kills to the end of the current
sentence, including any newline within the sentence.
kill-back-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.
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.
undo,
invoked with C-_ (control underscore using space
key). 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 only one previous one, even undoing through saves. We say Emacs has redo because you can reverse direction while undoing, thereby undoing the undo.
It's very important to get comfortable with undo as soon as possible; I recommend reading the undo section of the manual carefully and practising.
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-@ (set-mark-command). This sets the
mark exactly where point is, but now you can move point backwards or
forwards, and any text between constitutes the region.
Each buffer has a distinct point and mark at any moment (though you can always change them--just by moving the cursor changes the point), and therefore a distinct region. (It's also possible for there to be no mark in a buffer, and therefore no region.)
The region is unaffected by whether it begins with point or mark (that is, whether the cursor is currently located somewhere before mark in the textm, or somewhere after it) ; it makes no difference, just do what's convenient.
The region is normally invisible (but see C-x C-x), so don't forget where you place mark. You'll get used to this. However, if you're running Emacs under a windowing system, you can make the region visible by executing M-x transient-mark-mode.
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.
There are some special commands that are specifically designed to set the region around some interesting text.
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.
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 (like a "copy" command). 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.
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.