MSSL Basic User Note 10:
Philip Smith 09/01/96 V0.1 (beta)
This document: http://www.mssl.ucl.ac.uk/www_computing/buns/emacs.html
defining a key example
Why is emacs so good?
If you have trouble with anything more complicated than capital letters then
emacs is probably not for you.
If you use the cursor keys to go to the bottom of a 30 page document then you
probably won't be interested in smart editors and shortcuts.
But if you use an editor a lot of the time and particularly if you are a
programmer, emacs is normally considered the best editor around (it interacts
closely with programming applications such as gcc, gdb, make, etc.).
Emacs has been described as an operating system disguised as an editor. Emacs
is not just an editor it's more a way of life: you start it up when you get to
work and just leave it going using it for everything from editing to compiling
and debugging. There is even a newsgroup called
Emacs is a really powerful editor which does the things other editors do
and then some. It is an acronym for `Editor MACroS'
Apart from its functionality, it is also useful because it is so widely
available not just on most Unix machines around the world but on many other
operating systems too. In fact it is probably the most ported editor.
More infomation about emacs
FAQs are lists of Frequently Asked Questions which are available for numerous
subject (not necessarily about computers) on the internet.
The Emacs FAQ is at http://scwww.ucs.indiana.edu/FAQ/Emacs/
It is really useful and answers most questions about emacs from the very simple
to the complicated.
Emacs is programmed in the lisp computer language. The Emacs Lisp Archive is
Documentation that comes with emacs
Some documentation comes with emacs. This includes
The following Usenet newsgroups exist and can be read with nn, Netscape and
many other news readers and browsers:
What Emacs does
- emulates other editors like edt, tpu, teco, Wordstar, vi
- fully configurable - all the keys can be configured to just about
- keys are the same for tcsh etc Many applications use emacs keys
themselves so Control-A takes you to the start of a command line as well as
to the start of a line in emacs
- language sensitive Emacs `knows' the systax for many languages
(including C, C++, Fortran, idl, html, latex, tex) and will help you format
the program/document and find errors. Programs can be compiled in the editor
and errors indicated and documents can be checked and errors indicated.
Where a key sequence takes you to the end of a sentence in a normal text
mode, if you are editing a C program, the same key sequence will take
you to the end of the C statement you are on.
- compiler sensitive compilers, debuggers, etc will interact with
emacs to complement each other
- terminal or X Emacs will use X-Windows if it finds it, otherwise it will
use a terminal mode. To force no X-Windows use
- multiple windows Supports multiple windows some of which can be for
non-documents like compiler output, operating system shells etc.
- Large Undo history Emacs will remember what you have done a very long
way back so half an hour of changes can be undone one at a time and then even
- Menus available with X Clickable menus are available when running in
X-Windows. There's also a scroll-bar: left mouse button for down; right
mouse button for up; middle mouse button for drag.
If you don't like or don't want to use emacs there are other Unix editors
- jed - a simple emacs clone
- jove - a simple emacs clone
- microemacs - a simple emacs clone
- oemacs for DOS - GNU emacs for DOS
- emacs for windows - emacs for Windows
- emacs on vms - GNU emacs for VMS
- vi - a simple visual editor - very common
- ed - a simple line editor - very common
- textedit - Sun Openwindows editor
- xedit - simple X-Windows editor
- ... and many more ...
The emacs configuration file is ~/.emacs which contains lisp code.
Read the emacs manual for information on how to set up
emacs but for now use it without any customization or use pjs1's which knows
about a few more languages:
cp ~pjs1/.emacs ~
cp ~pjs1/elisp/* ~/elisp
There are a few important keys and typo conventions.
Control This is wither of the keys on the keyboard marked `Control' and
is held down while pressing another key so `Control a' would be accomplished by
holding down `Control' and pressing the `a' key.
Meta This key depends on the keyboard and choice. It is either the
Excape key or the Alt key or some key that has been defined to have the
desired effect. If the keyboard being used does not have an escape key, you
can define a key to be such or use `Control [' or `Control 3'. On machines running
Linux, the `Alt' key will work as well as `Escape' but on some machines only `Escape'
Alt is held down while pressing another key so `Alt a' would mean holding down
`Alt' and pressing the `a' key.
Escape is pressed once before pressing another key so `Esc a' would mean
pressing the `Escape' key followed by the `a' key.
C- means control
so C-a means "hold down `Control' and press lower case `a'"
and C-x C-c means `Control x' followed by `Control c'
M- means Meta
so M-a means `Meta a'
- M-- M-l means `Meta -' followed by `Meta l'
- C-M-v means Meta Control v which
- using `Escape' would be done by pressing
`Escape' then holding down `Control' and pressing `v', and
- using `Alt'
would be accomplished by holding down both `Control' and `Alt' and pressing `v'
- C-x 4 C-f means `Control x' followed by `4' followed by `Control f'
- M-x replace-string means `Meta x' followed by `replace-string' typed literally
SPC is the Spacebar
DEL is the Delete key
ESC is the Escape key
RET is the return key
Help is started with Control h (C-h). Press `?' then for help on help or if
you know what sort of help you want already press the appropriate key.
A tutorial is started with C-h t
Control G (C-g) cancels most operations so if emacs is waiting for something
you don't want at a prompt or doing something unwanted, try C-g.
Point and Mark
The point is the current location of the cursor.
The mark is the last mark (there can be many.
The minibuffer is the mini buffer at the bottom of the window into which
commands are typed.
The normal search is the incremental search using C-s. After typing
control-s the following keys will form a growing sequence of characters to
search for and the point will jump to the first occurence of the sequence so
far after each key press. So if you type C-s h e l l o emacs will first
move the point (text cursor) to the first occurence of `h', then the first
occurence of `he', then `hel' and so on.
To replace, use M-x replace-string to replace without questioning. Use
M-% or M-x query-replace to replace after asking yes/no.
A Short List of Good keys
See the Emacs Quick Reference card for a list of keys.
Here is another list of keys which might be useful. It assumes you know the
really simple keys.
The cursor keys work as you would expect.
If you don't understand some of the following key descriptions don't worry -
it is probably for some language you don't know and so you will probably not
need that key.
|M-f ||move forward one word|
|M-b ||move backward one word|
|M-x goto-line ||read n and go to line n|
|M-a ||beginning of sentence|
|M-e ||end of sentence|
|M-[ ||beginning of paragraph|
|M-] ||end of paragraph|
|C-M-u ||back up list|
|C-M-d ||down list|
|C-M-n ||forward over list|
|C-M-p ||backward over list|
|C-x ( ||start macro|
|C-x ) ||end macro|
|C-x e ||execute macro|
|C-x q ||query in keyboard macro|
|C-x name-last-keyboard-macro ||name-last-keyboard-macro|
|C-x insert-kbd-macro RET macronameRET ||insert keyboard macro|
Deleting, Killing, Yanking
|M-d ||kill word|
|M-DEL ||kill-previous word|
|C-x DEL ||kill to beginning of sentence|
|M-k ||kill to end of sentence|
|C-M-k ||kill sexp|
|M-y ||yank previous|
|M-w ||save region|
|C-M-w ||append next kill|
|M-^ ||join, deleting spaces, indentation|
|M-\ ||delete spaces/tabs around point|
|C-o ||open line|
|C-x C-o ||kill black lines|
|C-SPC ||set mark|
|C-@ ||set mark|
|C-x h ||mark-whole-buffer|
|C-u C-SPC ||return to mark|
|C-u C-@ ||return to mark|
|C-x ESC ||re-execute|
|M-r ||search previous minibuffer|
|C-s ESC string RET ||non-incremental search|
|C-r ESC string RET ||non-incremental search backward|
|C-s ESC C-w words RET ||ignore spaces, punctuation|
|C-r ESC C-w words RET ||ignore spaces, punctuation backward|
|M-% or M-x query-replace ||query-replace|
|M-x replace-string ||replace-string|
|M-- M-l ||downcase previous word|
|M-- M-u ||upcase previous word|
|M-- M-c ||capitalize previous word|
|C-x C-l ||downcase-region|
|C-x C-u ||upcase-region|
|C-x C-f ||find-file|
|C-x 4 C-f ||find-file-other-window|
|C-x C-s ||save-buffer|
|C-x s ||save-some-buffers|
|C-x 4 b ||select buffer in other window|
|C-x 2 ||split window vertically|
|C-x 3 ||split window horizontally|
|C-x ^ ||enlarge window vertically|
|C-x } ||enlarge window horizontally|
|C-M-o ||split at point|
|C-u TAB ||shift list rigidly|
|C-x C-n ||semipermanent goal|
|C-u C-x C-n||cancel|
|C-o ||insert line|
|C-x C-o ||delete blank lines|
|M-= ||count lines in region|
|C-x u ||undo|
|M-h ||mark paragraph|
|C-M-h ||mark defun|
|C-x h ||mark buffer|
|C-x C-p ||mark page|
|C-u C-SPC |
|M-^ ||join line|
|M-y ||replace ynaked text with previous|
|C-x a buffer ||append region to buffer|
|M-x x insert-buffer ||insert buffer|
|C-x x reg ||copy region to register|
|C-x g reg ||insert register|
|C-x r reg ||copy rectangle to register|
|C-x C-v ||find alternate file|
|M-~ ||forget file has been modified|
|C-M-a ||beginning of function|
|C-M-e ||end of function|
|M-( ||open ()|
|M-) ||close ()|
|C-x n ||narrow to region|
|C-x w ||widen|
|C-x load-file ||load lisp program|
|C-r ||recursive editing level|
|C-M-c ||exit recursive editing level|
|n ||go back|
|ESC ||get out|
|DEL ||don't replace|
|M-x occur ||list occurences|
|C-x $ ||arg = number of cols, no arg = cancel|
|f ||visit file|
|o ||visit file other window|
These capabilities should be remembered. The information here is only to act
as a reminder. Full descriptions can be found in the manual.
There are many modes available for languages and the like. You can use M-x
c-mode to go into C-mode which makes emacs work in a useful way for C
programmers. Modes are normally picked up automatically from the filename.
Other properties can be attached, for example, wrapping can be linked to the
text-mode so when editing any normal text (not programs!), emacs will wrap the
text. Some keys will work differently depending on the mode. For example, the
tab key will produce a tab in fundamental-mode but indent correctly in C-mode.
Emacs can fontify your document/program which makes different language elements
appear in different fonts or colours.
Emacs works well with the Gnu Debugger (gdb). Just edit your source code and
type M-x gdb and run gdb with the executable (compiled appropriately)
and a cursor will follow the debugger in your source code window.
Emacs can be used to view info documents. Just type M-x info.
Use C-x ( to start a macro and C-x ) to finish defining a macro then C-x e to
execute the macro. To execute it 10 times use C-u 10 C-x e.
Rectangles of text may be cut and pasted. The main rectangle commands are:
- C-x r k to kill rectangle (so that is may be yanked later)
- C-x r y to yank the previously killed rectangle
Use M-x ispell-word to check the spelling of the word near the cursor
and M-x ispell-buffer to check the whole document.
Use M-/ to try to complete a word or variable/function name (so if you want
long_variable_name, just try
and if that variable name has appeared already it will complete it.
Emacs can be told to hide deeply indented parts of text (usually a program).
Use C-x $.
Emacs can execute shell command in a window making the output `editable'. Use M-!.
- gnus - news reader
Mouse buttons and Regions
Use the left mouse button to select.
Use the middle mouse button to insert.
Emacs doesn't highlight a marked region because, to put it simply, it is above
that sort of thing. Emacs nearly always has a region defined between the last
mark and the point (cursor position) but it isn't that simple: there are
numerous marks and it would not be appropriate to highlight the region from any
one of them to the point.
Swapping DEL Backspace
From the FAQ:
* Under X or on a dumb terminal, it is possible to swap the Backspace and
Delete keys inside Emacs:
(keyboard-translate ?\C-h ?\C-?)
see question 123 for further details of "keyboard-translate".
* Another approach is to switch key bindings and put help on "C-x h"
(global-set-key [?\C-h] 'delete-backward-char)
(global-set-key [?\C-x ?h] 'help-command)
;; overrides mark-whole-buffer
(put these in your .emacs file).
Extract from netnews post:
[If you use:, pjs1]
keycode 22 = BackSpace
in your ~/.Xmodmap file.
This is not good if you want to use emacs. If you want to delete the char
before the cursor, you hit the KEY 22, and it generates BackSpace->^H, then
you get a help screen in emacs.
The fix for emacs is to put the following in your .emacs file:
;; Make the bloody backspace key work properly
(define-key global-map "\C-h" 'backward-delete-char)
;; Make sure backspace works in searches, too
(setq dearch-delete-char (string-to-char "\C-h"))
[think this should be s, pjs1]
Emacs window size and font
If you want to change the font size (which will change the window size
emacs -fn 10x20
or some other font like the following three:
To change the window size but keeping the same font use
emacs -geometry 80x30
for example where 80 is the number of columns and 30 is the number of rows.
You can of course use -fn and -geometry together
emacs -fn 7x14 -geometry 80x20
and alias this by putting in your .cshrc or .tcshrc file
alias emacs 'emacs -fn 7x14 -geometry 80x20'
Stopping adding lines at end
Put the following line in your .emacs file:
(setq next-line-add-newlines nil)
To make C-x SPACE set the mark use
(define-key ctl-x-map " " 'set-mark-command)
To make M-g goto a line use
(define-key global-map "\M-g" 'goto-line)
Redefining other keys can be done by following this last example with the
appropriate key-code and function name (\M-g and goto-line respectively in this case).
Try M-x hanoi and watch emacs solve the Tower of Hanoi puzzle. Not exactly
useful except as an example of the possible complexity of emacs programs:
Hanoi is a lisp program just like any of the other emacs commands, e.g., sort.
Just in case you're wondering what garbage colection is see http://www.cs.cmu.edu/Web/Groups/AI/html/faqs/lang/lisp/part2/faq-doc-10.html
You may think emacs has everything but the kitchen sink but you'd be wrong. It
has a kitchen sink too: just use
and when you iconize emacs you will
get the kitchen sink icon!
P.J.Smith --- firstname.lastname@example.org