Getting started with GNU Emacs

History and culture

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.

Why use Emacs?

Emacs has the subtitle:

The self-documentating programable programming environment.

Self-documentating 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 to learn new Emacs extensions and also just more about Emacs itself.

That Emacs is a programable programming environment means that every function that Emacs is composed of is 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:

Accessibility

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.

Support

Emacs has basic support for most languages and tasks. It is often the first editor that new languages provide support for. Over the years I have used it for: C, C++, CSS, Common Lisp, Erlang, Haskell, Java, JavaScript, LaTeX, Markdown, OCaml, PHP, Python, SML, Scheme, and XML/XHTML/HTML. Tasks I use it for include my calendar, TODO tracking and email.

Target users

Emacs is written by real hackers for real hackers!

How to stop worrying and learn to love Emacs

Emacs is too large to simply learn. You learn by doing and the parts you like will 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 using the Emacs help system. Before we look at the help system, lets look at the terminology and basics of Emacs.

Terminology

Key bindings

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.

Buffers and files

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 end with ~, just in case your operating system crashes.

Windows

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).

Modes

A mode is a package of features related to some task. Modes are typically activated when you open a special type of file (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.

Minibuffer

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 the file name is entered.

A basic Emacs cheat sheet

Basic cheat sheet for Emacs
Key bindingCommand
Basic file/buffer commands
C-x C-fNew buffer from file
C-x C-sSave buffer to file
C-x C-wWrite buffer to new file
C-x kKill buffer (closes the buffer)
C-x bSwitch buffer
Basic region commands (copy/paste)
C-yYank from kill-ring (paste)
M-yYank next from kill-ring (cycle paste)
C-kKill line (cut line)
C-SPCSet mark
M-wCopy region
C-wKill region
Basic navigation
C-pPrevious line
C-nNext line
C-fForward one character
C-bBackward one character
M-fForward one word
M-bBackward one word
Other useful commands
C-gCancel action / quit action
C-sSearch forwards
C-rSearch backwards
C-x uUndo — also bound to: C-_ and C-/
C-lCenter line (twice: top line, thrice: bottom line)
C-tTranspose character
M-tTranspose word
M-/Complete word
M-%String replace
M-qWrap paragraph

The help system

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. Do you need a basic introduction then you can find the tutorial; do you need 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:

KeySubsystem
aSearch through Emacs commands
tStart the great Emacs tutorial
mSee info about commands for the current mode
?See all the subsystems of Emacs help

Basic Scheme

(Assuming Scheme is properly set up. If not see: ???)

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

Or

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
>
Useful commands in scheme-mode
Key bindingCommand
C-M-fForward one s-expression
C-M-bBackward one s-expression
C-M-tTranspose s-expression
C-c dSend last definition and switch to REPL
C-c C-bSend entire buffer and switch to REPL

Challenges

As mentioned, to Emacs learn you must use it. Here are a few challenges to have fun with.

  1. After splitting a window (C-x 2 or C-x 3) how do you switch to the other window without using your mouse?

  2. 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?

  3. 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!”.

Useful utilities

DOS-style key bindings

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.

Browsing the kill-ring

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

Resources

An official and more detailed tour covering most of the above is available at:
http://www.gnu.org/software/emacs/tour/

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 a nice and complete Windows package: http://ourcomments.org/Emacs/EmacsW32.html

A community wiki with lots of references and customizations: http://www.emacswiki.org/