Tag Archives: programming

Emacs and lisp


The Emacs text editor uses lisp as an extension language. This article will attempt to explain enough lisp to do basic emacs customization, to someone who knows imperative programming languages.

Evaluating lisp

Lisp consists of balanced pairs of parantheses, filled with tokens, separated by space, eg. like this:

(somefun1 1 2 3 "four")
(somefun2 5 6)

The flow of a lisp program is to start from the top, and evaluate the balanced parantheses and their contents. When doing this, lisp expects the first item in the list to either be a function (actually the name of a function (or actually a symbol bound to a function (but now it is time to stop digressing and nest up the parantheses and continue))), or what’s called “a special form”.

Evaluating functions

For now, let’s assume that the first items are all functions. Then the example above, would be to just invoke “somefun1” with three integer arguments and one string argument, then invoke “somefun2” with two integer arguments.

Ie. basically, the same as this C fragment, except with the parantheses surrounding the name of the function, and no semicolon to terminate each statement:

somefun(1, 2, 3, "four");
somefun(5, 6);

But a more interesting, or typical, lisp example, would be something like this:

(somefun1 (+ 0 1) (+ 1 1) (+ 1 1 1) (concat "f" "o" "u" "r"))
(somefun2 (- 11 6) (- 11 5))

What happens when lisp starts evaluating the first line, is that it will evaluate each of the balanced parantheses, before it eventually serves them up as arguments to “somefun1”.

Then lisp will move on to the next line and do the same thing there, and continue with this, until it reaches the end of input.

Evaluating “special forms”

Evaluating a special form differs from evaluating a function in the way the arguments are evaluated.

Examples of special forms, are the flow control constructs of lisp, eg. the “if” special form:

(if (= 2 2)
    (message "If test was true!")
  (message "If test was false!"))

When lisp encouters an “if” special clause, it will start by evaluating the first argument.

If the result of the first arguent is other than nil, the second argument will be evaluated. If the there are more arguments to “if”, they will not be evaluated.

If the result of the first argument is nil, the third, and following arguments to “if” will be evaluated (this is the “else” part of the if clause). In this case, the second argument to if will not be evaluated.

Trying out lisp

In emacs, go to the scratch buffer. This can be done with `C-x b *sc TAB RET’.

Type the following text into the buffer:

(message "Hello minibuffer!")

Place the cursor at the end of the line, and press `C-j’. Two things will happen:

  1. The minibuffer will show the text “Hello minibuffer!” (without the quotes)
  2. The line below the line you typed in will get the text “Hello minibuffer!” (this time with the quotes)

The `C-j’ command will evaluate the lisp expression that ends on the same line as the cursor, and the evaluation result will be printed on the next line.

The evaluation result is useful when debugging an expression, such as the test argument to an “if” special form.

If you don’t wish to move the cursor to the end of the clause being tested, you can instead press `C-M-x’, which will evaluate the outermost clause, under the cursor.

Using `C-M-x’ won’t output the evaluation result to the scratch buffer

Setting variables

In the .emacs file you can set variables that affect how emacs operates. On unix-ish systems, the .emacs file resides in the home directory. On windows systems the default is to look for the file in the “%USERPROFILE%AppDataRoaming” directory (unless the HOME environment variable, in which case emacs looks for it in “%HOME%”).

One of my oldest settings is one that stops emacs from saving “~” backup files:

(setq make-backup-files nil)

In today’s emacs one can set a simple settings like this interactively, using the customize mechanism.

The most important thing to be able to do, is to set various configuration variables. Today this can be done interactively for many variable using the “customize” mechanism.

But it’s still useful to be able to know how to set variables in lisp, eg. for setting variables differently depending of the type of file loaded, the version of emacs, or the OS one is running on.

To set variables, use the special form “setq”. Type the following into the scratch buffer, and evaluate it:

(setq a-variable-used-as-an-argument t)

Now a-variable-used-as-an-argument is a variable, and you can use it in lisp code. The value is t.

Defining functions

Functions are defined with the “defun” special form. An example, is:

(defun somefun1  (firstarg secondarg)
  "This is a function to do something.
If firstarg is non-nil it will output that firstarg was true.
If firstarg is nil it will output that firstarg was false.
The secondarg argument isn't used for anyting at all"
  (if firstarg
      (message "Firstarg was true!")
    (message "Firstarg was false!")))

First try `C-h f somefun1 RET’. Emacs will say “[No Match]” when RET is pressed.

Then move to the scratch buffer and paste in the code of the example, and use `C-j’ or `C-M-x’ to evaluate the defun clause.

Then try `C-h f somefun1 RET’ again. This time the emacs window will split in two and the lower half will show:

somefun1 is a Lisp function.

(somefun1 firstarg secondarg)

This is a function to do something.
If firstarg is non-nil it will output that firstarg was true.
If firstarg is nil it will output that firstarg was false.
The secondarg argument isn't used for anyting at all


To try using the function, paste the following into the scratch buffer, and evaluate it:


This will cause the lisp debugger to pop up, saying that the function has two methods, but is called with none.

Add some arguments to the function call and evaluate it again:

(somefun1 nil "Required but unused")

The minibuffer will show “Firstarg was false!”. Now try putting different values as the first argument. All values, other than nil, will result in “Firstarg was true!” being written to the minibuffer.


Many variables used by language modes are “buffer local”. This is so that eg. c-mode can use different indentation settings on two separate source files, to accomodate the different indentation/formatting used on them.

This means that if you set the variable when starting up emacs, the buffer version of the variable may not get the setting.

To make it possible to customize this, and other things, many emacs format specific modes provides “hooks”.

A hook is a list of functions, and emacs will run all functions in that list when doing a particular operation. Typically there are hooks for loading and saving files.

In summary

In the same way as the two preceeding articles, this article describes the subset of lisp and emacs lisp, that I use on a regular basis. Lisp by itself could probably be a lifetime study. I have for instance skipped mentioning macros, which are another important construct in lisp. Macros allow programmers to create constructs that behaves similar to special forms. Macros can e.g. be used to create new control structures (the macro “when” is such a control structure. It is nicer syntactically than the special form “if”).

Suggested reading is the Emacs Lisp Reference Manual and An Introduction to Programming in Emacs Lisp. In addition to reading them on the net, you can read them in the builtin manual reader of emacs (`C-h i m elisp RET’ and `C-h i m emacs lisp TAB RET’).

Very basic emacs usage


Emacs is a very powerful and flexible text editor.

This is a very quick walk-through of essential emacs commands, based on my own experience of the commands I use most frequently.

I’m giving the key shortcuts, because those are the ones I use. But running in a GUI, you can use mouse navigation for such things as switching windows, and there are menu items for many of the commands.

I thought that this list would be short, but once I started writing I thinking about new stuff that I should include. Eventually I had to cut it down to what I really think are the basics.

I won’t spend any time on the flexibility of emacs, but will mention it in the section titled “Commands and key bindings”


Keypress sequences will be represented like these examples `C-x C-f’ (pronounced “control ex control eff”), `M-w’ (pronounced “meta double you”).


  • The sequence is delimited by a single back quote ` at the start, and a single quote ‘ at the end
  • Spaces in the command sequence are just separators. If pressing the space bar is part of the command sequence, it will be represented by SPC, e.g. `C-SPC’
  • `C-‘ means: hold the Ctrl key pressed while pressing and releasing the key connected to C with the hyphen
  • `M-‘ means: hold the “meta” key pressed while pressing and releasing the key connected to C with the hyphen. On PC keyboards the “meta” key is most commonly the “Alt” key
  • `RET’ in a command sequence means pressing the Return (or Enter) key
  • `TAB’ means press the tabulator key

Commands and key bindings

The actual commands in emacs are all lisp functions. The keyboard key combinations you press to run the command, are known as the “key binding” of the command.

If a command’s regular key binding has been masked for some reason, you can run the command with `M-x command RET’ where “command” is the command’s lisp function name.

Emacs allows replacing the key bindings for all commands with your own, and for a new emacs user it may be tempting to replace the default emacs key bindings with something more familiar. However, I recommend against replacing the default key bindings, because:

  1. different emacs modes often have a command set patterned on the base command set, and they are easier to remember for that reason
  2. if you come to a emacs installation that does not have your customizations you may not be able to use it

Exiting emacs

To quit emacs, do `C-x C-c’.

If running emacs with a GUI, you can also go to the File menu, and select quit.

Note that `C-x C-c’ is quite easy to hit by accident. It’s possible to make gnus query for whether it should quit or not, by customizing the lisp variable confirm-kill-emacs.

To modify the variable, do `C-h v confirm-kill-emacs RET’. The editing screen will split. Do `C-x o’ to move to the lower half of the edit area, move the underlined word “customize” and press RET. In the form that appears select the “Value Menu” button, and then choose “Ask with yes-or-no-p”. Then select “Set for current session”, then select “Save for future sessions”, then select “Exit”.

Getting help

The Help menu have lots of stuff. There’s an online tutorial that is worth following. All help commands are available through `C-h’ followed by a key.

I use the following help commands on a regular basis:

  • `C-h i’ to get the online manual
  • `C-h b’ to find the current key bindings
  • `C-h f’ to find the documentation for a lisp function (eg. a command),
  • `C-h v’ to find the documentation for a lisp variable

Note that for both `C-h f’ and `C-h v’ TAB and SPC can be used to autp expand the function or variable name.

Also note that many variable documentations have the text “This variable can be customized” in it. Move the cursor to the word “customized” and press RET. This will take you to a form where you can change the variable and persist the changes.

Loading and saving files

When starting emacs in a GUI environment, you will see:

  • A menu bar at the top
  • A toolbar below the menu bar
  • A big window with a blurb-looking text
  • A line with a different background color and some text and numbers
  • A white area at the bottom (known as “the mini buffer”). This is where you will be prompted for file names, and give search strings

To load a file into emacs, press the keys `C-x C-f’.

The minibuffer will present a prompt for the file name. It will suggest a path based on the current working directory. You can edit that path whereever you want to go, and the possibilities for the paths can be expanded by pressing SPC or TAB. SPC means “expand to the first separator character” (e.g. “/” or “.”). TAB means “expand as far as possible”.

To save the file type `C-x C-s’.

To save to a new file name, type `C-x C-w’.

All of these commands are available under the File menu, when running emacs in a GUI.

You can edit multiple files into emacs. To move between files, either use `C-x C-b’ to get a list of buffers, or `C-x b’ (no ctrl key for the b), and use TAB or SPC to expand the name of the loaded file you want to move to.

The `C-x C-b’ command will split the emacs window in two, vertically. Use `C-x o’ to jump to the lower half, and then arrow keys to navigate to the file you want to go to, and then `C-x 1′ to let it take the entire editing area.

You can also cycle through the loaded files with the commands `C-x C-right’ and `C-x C-left’ (“right” and “left” meaning the right and left arrow keys).

All of these commands are available in the Buffers menu

Editing multiple files

You can load as many files as you like (well… there’s a limit, but I’ve never reached it) into emacs at the same time.

To switch between files, use `C-x b’ (Note: no ctrl key for the `b’) and write the name of the file you want to go to (not the full path, just the local file name). You can use TAB and SPC to auto-expand the file name.

To see all files loaded in emacs give the command `C-x C-b’ (this time use ctrl for the b as well). The text editing window will split in two with the loaded files listed in the lower half of the screen.

To get to a loaded file, use `C-x o’ to switch the cursor over to the lower half, then cursor to the file you want to look at, and press RET.

Now the lower half will show the file you selected, but the top half still show again.txt. To make the new file use all of the editing area, press `C-x 1′ (“control ex one”).

There are many similar commands to split and close part of the editing area. I won’t get into them here. The manual has more.

Navigating in emacs

The normal arrow keys work as expected. So does page up and page down. The Home key will take you to the start of the line, and the End key will take you to the end of the line.

In addition you can navigate with Ctrl keys: `C-p’ is the same as arrow-up (p for “previous”), `C-n’ is the same as arrow-down (d for “down”), `C-b’ is the same as arrow-left (b for “back”), and `C-f’ is the same as arrow-right (f for “forward”).

Two of these commands have “meta” siblings: `M-f’ which forwards the cursor over the next word, and `M-b’ which takes the cursor over the previous word.

In addition there are `C-M-f’ (“control meta eff”) which takes the cursor over the next clause. Whatever the clause is depends on the type of file in the current buffer. In an XML buffer that would be an element. There’s also `C-M-b’ which moves a clause backwards.

The page-up and page-down keys also have ctrl/meta keys. `C-v’ is page down, and `M-v’ is page up.

Ctrl key equivalents for Home and End are `C-a’ and ‘C-e’.

Searching and replacing

To start searching in the current file, give the command `C-s’, then start typing the word you are searching for. The search is incremental so the more you type the more it will match. The cursor will move to the match.

When you have found a match, you can:

  1. Press `C-s’ to go to the next match
  2. Press `RET’ to stop the search at the current match
  3. Press `ESC’ to abort the search and return the cursor to where you were before starting the search

If you do `C-M-s’ instead of `C-s’ (ie. hold down both the ctrl and Alt keys, when pressing and releasing s), the incremental search will be a regular expression search, instead of just a plain search.

Another powerful search method is occur. Type `M-s o’ and the minibuffer will prompt you for a regular expression. After giving a regular expression and typing RET, the editing area will split in half, the lower half showing all matches. To go to a match do `C-x o’ to move to the lower half, use the up and down arrow keys to move to a match, and then press RET.

To search and replace, use the command `M-%’. You will be prompted in the minibuffer for search and replace texts. When being prompted, you can use the up and down arrow keys to select earlier search and replace texts.

The command `C-M-%’ is a regular expression search and replace, and adds complexity and power to the search and replace, but otherwise behaves the same as the plain text search and replace.

Cutting and pasting

To mark the area that is to be copied or pasted, press `C-SPC’ at the start of the area, and then move the cursor the other end. The area is now selected (in a GUI, it will show up as selected).

If you want to copy the marked region, use `M-w’, if you want to cut the marked region (“kill the region” in emacs terminology) use `C-w’.

Then move the cursor to where you want to paste it (“yank it”, in emacs terminology), and press `C-y’.

If you immediately (ie. without moving the cursor) press `M-y’ the yanked (ie. “pasted”) region will be replaced with the previously copied or cut region. By pressing `M-y’ again you will get the copy/paste preceeding it. You can continue pressing `M-y’ until you’re back to the last copied/cut region, and then continue past that again for a new cycle.

This is what emacs calls “cycling the kill ring”.

A very useful way of deleting lines is using `C-k’. This command will kill the rest of the line from the cursor point. If you continue to press `C-k’ the killed lines will be added to the most recent entry in the kill ring, rather than appear as new entries.

If you want to delete the next 50 lines you can do it with `C-u 5 0 C-k’.

`C-u’ is known as “the universal argument prefix”. It can be applied to many emacs commands, typically to give them a numerical argument.

Useful variables to set

There are some variables that are useful to set to values other than the default.

For all of them, do `C-h v variable-name RET’ then select the underlined word “customize” in the variable’s documentation, select the “Value Menu” in the form that appears, and set the appropriate value. Then select “Set for current session”, then select “Save for future sessions”, then select “Exit”.

The variables are:

  • `confirm-kill-emacs’ set to `yes-or-no-p’ (emacs will query yes/no on exit)
  • `split-width-threshold’ set to `nil’ (emacs will split windows horizontally)
  • `x-select-enable-clipboard’ set to `t’ (copy/paste will work from emacs to other programs)