Minibuffer At the bottom of the emacs window, there is an area big
enough for one line of text below the status bar. This area is called the
Minibuffer, and is used when commands needs to interact with you. For example,
when you go to open a file, emacs prompts you for the file name in the
Minibuffer, and you type your answer there.
Getting yourself out of trouble/getting help
- C-_ Undo! (Note: emacs will let you undo multiple things)
- C-g Cancel
- M-x describe-key Prompts you for a keystroke and then explains what it does.
File/Buffer Related Commands
- C-x C-c Quit Emacs
- C-x C-s Save Current Buffer
- C-x C-w Save Current Buffer As
- C-x i Insert contents of file at point
- C-x C-f Open File
- C-x b Switch buffers
- C-x 2 Split window horizontally
- C-x 3 Split window vertically
- C-x 1 Unsplit window
- C-x o Go to other split part of window (if using graphical emacs,
you can just mouse click in it)
Selection, copying, and pasting
In emacs, selection is done by setting the mark (Control-Space) at the start
of the region, then moving the point to to the end of the region.
Note that if you are using a graphical emacs, you can select
things with the mouse as you normally would- so anything that
says "from mark to point" can also mean "mouse selected region"
- C-k Kill (cut) from point to end of line
- C-y Yank (paste)
- M-y (Must immediately follow C-y or M-y) un-yank whatever was just
yanked, and yank the previously killed text instead.
- C- (control-space) Set mark at point
- C-w Kill from mark to point
- M-w Copy from mark to point
Miscellaneous
- ESC-# Escape (somenumber) before another command specifies that
the next command should be done that many times. A simple example would be
ESC 15 a which would insert 15 of the letter a (as if you had
typed a 15 times).
- M-/ Complete word at point (Note: when emacs needs completions for
commands, like which file to open with C-x C-f, use TAB instead-
M-/ completes with words found in the current buffer (and then other buffers), which is not
what you want in that case)
- M-x check-parens This extended command asks emacs to check your (),
{}, and [] for proper matching. It will tell you if everything is ok, or
show you which ones are not matched right.
Search and replace
The primary way to search is " incremental search"
If you aren't familiar with this type of search, it basically
means that it searches as you type. So when you type H, it will
search for H, following that by e will make refine your search to He,
and so on- this way you can type only as much as you really need.
- C-s Incremental search forward. Also used to search for the next
occurence when already doing incremental search (i.e. C-s Hello C-s C-s will
find the third occurence of Hello going forwards).
- C-r Incremental search backward.
- M-x replace-string This extended command will prompt you for the string
for, and the string to replace it with.
- M-x replace-regexp This extended command will prompt for a regexp to
search for, and the string to replace it with.
- M-x query-replace Like replace-string, but ask about each
occurence. (Emacs will show you each match, and you can choose y
to replace, n to not replace, ! to replace all remaining, or C-g to quit replacing).
- M-x query-replace-regexp Like replace-string, but ask about each
occurence.
Moving around
- (Arrow keys) Direction you would expect
- C-a Move to start of line (also HOME)
- C-e Move to end of line (also END)
- M-> Move to end of buffer
- M-< Move to start of buffer
Macros
One of the really cool things you can do with emacs is define a keyboard macro.
Lets say that you have a file with a few hundred lines that look like this:
field1:field2=field3
and for whatever reason, you want to swap them around to be formatted
like this:
field3:field2=field1
Observing that you can do 1 line by the following steps:
- Go to the start (C-a)
- Set mark (C- )
- Search forward for : (C-s :), then go backwards (left arrow)
- Kill (C-w)
- Search forward for = (C-s = RET)
- Yank (C-y)
- Set mark (C- )
- Go to end of line (C-e)
- Kill (C-w)
- Go to start of line (C-a)
- Yank (C-y)
- Go to next line (down arrow)
It would be really nice to be able to tell emacs to take all of those
commands and remember them so you can do them with one keystroke.
This is where keyboard macros come in. To tell emacs
to start remembering what you are doing, type C-x (
(Control-x open paren). Emacs will respond by saying
"Defining kbd macro...." in the mini-buffer.
Next, do all the commands you want to do, and then
type C-x ) (Control-x close paren) to finish the definition.
Emacs should respond by saying "Keyboard macro defined"
(if it says "Not defining kbd macro" you either did not
start it properly, or did something that canceled the definition).
Once your keyboard macro is defined, you can execute it by
typing C-x e. Furthermore, since execution of the
macro is a single command, you can repeat it multiple
times by prefixing it with ESC-x #, for
example ESC-100 C-x e would execute that macro
100 times.
Additionally, once you have created a macro,
you can give the macro a name, and use
that name as an extended command until you quit
emacs. To do this, use the command M-x name-last-kbd-macro.
For example, if the command M-x name-last-kbd-macro RET reorder-fields
RET were issued after the above macro were defined, M-x
reorder-fields would be a new extended command that would
execute the field swapping commands shown above.
Elisp: Programming/customizing emacs
Emacs has its own built in programming language called
elisp, which is primarily like Lisp. I don't personally
do too much elisp hacking, although I have dabbled
around in it and written some simple stuff.
In the simplest case, you can edit your .emacs
customization file (in C: or your home directory depending
on if you use Windows or Mac/Linux/Unix). You
can throw in some commands like
(global-set-key "\C-c\C-v" 'compile)
which makes it so that C-c C-v will
be like doing M-x compile,
or like these:
(setq line-number-mode t)
(setq column-number-mode t)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
which set various variables to true
(you could also set them to nil for false).
If you are interested in more serious elisp hacking,
google for references.