Improving page (section) navigation14 Sep 2015, by Artur Malabarba.
If you’ve taken the time to browse some Elisp source files, you’ve no doubt run
into that odd little
^L, a.k.a. the form feed character. Emacs uses these
white space characters as page delimiters. This makes for a very convenient way
to split a file into sections, and quickly navigate between them. I won’t go too
deep into them, as Eric James has already written a great crash course on pages
that you should go check out.
What I wanted to write about is the way that I do page navigation in Emacs. Firstly, I find the default keys to be nothing short of abhorrent. Take a prefix key with the Control modifier, attach to it a non-modified key, and then make that key be something not-so-easy to hit, like ], and you have the recipe for painful fingers.
These keys would normally be bound to
I’ve never ever ever used (in fact, the former is disabled by default).
Then there’s a minor peeve. In some corner cases Emacs might leave the cursor at the bottom of the screen after moving. Here we make sure that never happens.
And then there’s the best part. It turns out you don’t need the form-feed
character to delimit pages. That’s important because some languages aren’t that
nice about them, and some dev teams might prefer you don’t stick those
^L all over the place. In Clojure, for instance,
cljfmt confuses it for a
blank line and freaks out a little.
Fortunately, Elisp style already recommends using
;;; to indicate comment
sections, and the form feed character is most commonly placed right above these
sections. So why not use that instead?
The regexp above is a bit special. We’re setting the page delimiter to be a
;;; at the start of a line, plus any number of empty lines or comment lines
that follow it (that
# part is to exclude
Consequently, when we hit C-x C-n or C-x C-p, the point is left right at the
start of the first code-line after the
;;;. That’s usually where I want to be,
and it works even on buffers without
^L, Clojure and Elisp. No doubt you can
extended that to your programming language of choice by replacing the semicolons
with the appropriate comment character.
Even better, why not write up a general solution based on the