Chapter 1. Customizing Emacs

This chapter introduces basic Emacs customizations, and teaches some Emacs Lisp along the way. One of the simplest and most common customizations is to move commands from one key to another. Perhaps you don't like Emacs's two-key sequence for saving files (C-x C-s) because you've been using some other editor where save is simply C-s. Or perhaps you sometimes accidentally type C-x C-c, which exits Emacs, when you mean to press only C-x, and you'd like accidental presses of C-x C-c to have a less drastic effect. Or perhaps, as in the example that follows, you need to work around an unusual expectation that Emacs has about your keyboard.

Backspace and Delete

Imagine you're typing the word "Lisp" and you accidentally type "List." To correct your typo, do you press the BACKSPACE key or the DELETE key?

The answer depends on your keyboard, but it's not merely a question of how the key is labeled. Sometimes the key is labeled "Backspace," sometimes it's labeled "Delete," sometimes "Erase," and sometimes it's not labeled with a word but has a left-pointing arrow or some other graphic. To Emacs, what matters isn't the label but the numeric character code that the key generates when pressed. Regardless of the label on the key, the "move left and erase the previous character" key may generate an ASCII "backspace" code (decimal 8, usually denoted BS) or an ASCII "delete" code (decimal 127, usually denoted DEL).

In its default configuration, Emacs believes only DEL is the code meaning "move left and erase the previous character." If you have a BACKSPACE/DELETE/ERASE key that generates a BS, it won't do what you expect when you press it.

What's worse is what your BACKSPACE/DELETE/ERASE key will do when you press it, if it's a BS-generating key. Emacs presumes that since BS isn't used for moving left and erasing the previous character, it's available to perform another function. As it happens, BS is also the code sent when you press C-h. If you're among those who don't need C-h to mean "move left and erase the previous character," then C-h is a pretty natural choice for a Help key, and in fact that's what Emacs uses it for by default. Unfortunately, this means that if you have a BS-generating BACKSPACE/DELETE/ERASE key, then pressing it won't backspace or delete or erase; it will invoke Emacs's online help.

More than one tentative first-time Emacs user has been put off by the surprise that greets them the first time they try to erase a typo. Suddenly a new Emacs window—the Help window—pops up, prompting the hapless user to choose some Help subcommand. The Help window is so verbose and unexpected that it merely exacerbates the user's astonishment. The natural panic reaction—hit C-g ("abort the current operation") a bunch of times—is accompanied by a cacophonous ringing of the terminal bell. It's no wonder that intelligent, well-meaning users who might otherwise have helped swell the ranks of fervent Emacs evangelists instead choose to continue struggling with safe, inoffensive vi.

It pains me to think of it, especially when the situation is so easily remedied.

When Emacs starts, it reads and executes the contents of the .emacs file in your home directory. Emacs Lisp is the language of this file, and as we will discover in the course of this book, there's almost nothing you can't customize in Emacs by writing some Emacs Lisp and putting it in .emacs. The first thing we'll look at is adding some code to .emacs to make BS and DEL both do "back up and erase a character," moving the Help command to some other key. First we'll need to take a look at Lisp, the language of the .emacs file.

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.