Chapter 2. Simple New Commands

In this chapter we'll develop several very small Lisp functions and commands, introducing a wealth of concepts that will serve us when we tackle larger tasks in the chapters to follow.

Traversing Windows

When I started using Emacs, I was dissatisfied with the keybinding C-x o, other-window. It moves the cursor from one Emacs window into the next. If I wanted to move the cursor to the previous window instead, I had to invoke other-window with -1 as an argument by typing C-u - 1 C-x o, which is cumbersome. Just as cumbersome was pressing C-x o repeatedly until I cycled through all the windows and came back around to what had been the "previous" one.

What I really wanted was one keybinding meaning "next window" and a different keybinding meaning "previous window." I knew I could do this by writing some new Emacs Lisp code and binding my new functions to new keybindings. First I had to choose those keybindings. "Next" and "previous" naturally suggested C-n and C-p, but those keys are bound to next-line and previous-line and I didn't want to change them. The next best option was to use some prefix key, followed by C-n and C-p. Emacs already uses C-x as a prefix for many two-keystroke commands (such as C-x o itself), so I chose C-x C-n for "next window" and C-x C-p for "previous window." ...

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.