Emacs was brought to life in the mid 1970’s in the Artificial Intelligence lab at MIT. It was initially created by RMS (of GNU fame) and later refined by Guy Steele (of Scheme fame). Many versions of Emacs have been created since then. Gosling (of Java fame) made the first UNIX port. Later RMS made GNU Emacs, which is the most popular version in use today.
Emacs has the subtitle:
The self-documenting programable programming environment.
Emacs is self-documenting because (almost) all functions in Emacs contain their own documentation. This information can be accessed from within Emacs as we will later see. It is a cool feature that will help you learn more about Emacs and its many libraries.
Emacs is a programable programming environment because (almost) all Emacs is made easily available for you to use and extend. You can program your own tools using Emacs Lisp. This lets you customize Emacs to provide exactly the environment you want! Some other good reasons for using Emacs are:
Emacs is still the most prevalent editor in use today. It is supported on many architectures and on all operating systems that you would consider using (GNU/Linux, *BSD, OS X, even Minix 3), even the more abstruse development platforms such as the Windows family.
Emacs is written by real hackers for real hackers!
Emacs is too large to simply “learn”. You learn by doing. The parts you like become part of you Emacs jargon. Even skilled Emacs users (> 10 years experience) find new features.
To use Emacs effectively, you should learn how to find the features you need with the Emacs help system. Before we look at the help system, lets look at the terminology and basics of Emacs.
The most often used functions in Emacs are bound to key combinations. This allows quick invocation without breaking your typing position (read: no mouse). Most key bindings are activated using the Control and/or Meta keys (meta is the Emacs word for Alt).
Key bindings are written as: C-x C-f (or in shorthand: C-x-f) which reads “Hold control and press x, hold control and press f”. Key bindings using meta are written as: M-x. We use RET to denote hitting return.
Emacs is comprised of a series of buffers. A buffer can be anything from the content of a file to an interactive shell. Everything you see visually is a buffer.
When you open a file in Emacs it amounts to creating a fresh buffer and putting the content into that buffer. You edit text in the buffer and can save it back to the file. Emacs does not save back to the file for you! However, it does do autosaves in files that start and end with #, just in case your operating system crashes.
A window in Emacs is the view in which a buffer is displayed. It is not to be confused with the application “windows” of your OS. By default you have only one window and can therefore only see one buffer. You can split a window using C-x 2 (horizontal) or C-x 3 (vertical). This gives you two views of the same buffer. A pretty nice feature if you need to edit something at the end but want to see something at the top while doing so).
A mode is a package of features related to some task. Specific modes are typically activated when you open a specific type of file (eg, foo.java). The java-mode is an example of such a mode. It defines some syntax rules and provides a series of commands for compiling and so on, most of which are inherited from c-mode.
The minibuffer is a special buffer in Emacs. It is always located at the bottom of the frame and is used for among other things typing in commands. For example, when opening a file (C-x-f) it is here that the file name is entered.
|Basic file/buffer commands|
|C-x C-f||New buffer from file|
|C-x C-s||Save buffer to file|
|C-x C-w||Write buffer to new file|
|C-x k||Kill buffer (closes the buffer)|
|C-x b||Switch buffer|
|Basic region commands (copy/paste)|
|C-y||Yank from kill-ring (paste)|
|M-y||Yank next from kill-ring (cycle paste)|
|C-k||Kill line (cut line)|
|C-f||Forward one character|
|C-b||Backward one character|
|M-f||Forward one word|
|M-b||Backward one word|
|Other useful commands|
|C-g||Cancel action / quit action|
|C-x u||Undo – also bound to: C-_ and C-/|
|C-l||Center line (twice: top line, thrice: bottom line)|
The help system in Emacs together with the built-in documentation helps to locate the parts of Emacs you need. By mastering it you have access to all of Emacs. If you want a basic introduction then you can find the tutorial; if you want some way of performing a task then you can search for it here, read how to use it and see if it has a key binding.
In short, all you need to remember is: C-h.
The help system is grouped in subsystems accessed after typing: C-h. For example, searching in commands is done with: C-h a. Some important subsystems are:
|a||Search through Emacs commands|
|t||Start the great Emacs tutorial|
|m||See info about commands for the current mode|
|?||See all the subsystems of Emacs help|
(Assuming Scheme is properly set up. If not see: http://users-cs.au.dk/danvy/dProgSprog/Lecture-notes/emacs.html)
When programming in Scheme first open a file to contain your Scheme code (C-x C-f ~/my/scheme/code.scm). Now to start the Scheme interpreter do:
M-x run-scheme RET
M-x run-petite-chez-scheme RET
This will start the Scheme system and switch your buffer as well. You switch back with C-x b
Now in your Scheme code buffer you can type in code, such as:
(define foo (lambda (x) x))
Now running C-c d will send the definition to the Scheme system, split your window in two and move the cursor to the Scheme prompt. Now at the prompt you can run your function:
> (foo 42) 42 >
|C-M-f||Forward one s-expression|
|C-M-b||Backward one s-expression|
|C-c d||Send last definition and switch to REPL|
|C-c C-b||Send entire buffer and switch to REPL|
As mentioned, to Emacs learn you must use it. Here are a few challenges to have fun with.
After splitting a window (C-x 2 or C-x 3) how do you switch to the other window without using your mouse?
Also, what key binding will let you delete a window? In other words, when you have split a window how do you get back to just one again?
If you have to words that are separated by say 10-20 spaces (or tabs) how can you trim the white space so they are separated by just one space? I.e., from “hello world!” to “hello world!”.
To enable DOS-style shortcuts you can run: M-x cua-mode RET. This will enable C-c for copy, C-v for paste and C-z for undo. Running M-x cua-mode RET again will disable the key bindings. Beware that enabling this mode will (temporary) disable other standard bindings such as C-v for page-down.
The browse-kill-ring command is a nice extension that enables you to visually browse and select items in the kill-ring. This is not part of the default Emacs so you need to install it yourself. On debian based systems simply do:
sudo apt-get install emacs-goodies-el
An official and more detailed tour covering most of the above is available at:
The official GNU Emacs webpage: http://www.gnu.org/software/emacs/
Subsection on GNU Emacs for windows:
http://www.gnu.org/software/emacs/windows/ntemacs.html and its installation binary: http://ftp.gnu.org/pub/gnu/emacs/windows/
For Mac OS X, the following provides a vanilla Emacs installation package: http://emacsformacosx.com/
A community wiki with lots of references and customizations: http://www.emacswiki.org/