The joy of an integrated environment

Most of my GNU Emacs learning time has continued to be spent writing elisp. I am very slowly starting to retrain myself on a couple of basic keystrokes: M-g M-g (goto-line) and M-% (query-replace) are two biggies, because my use of ido-ubquitous mode actually means that my default use of M-x to get to both of those is disrupted—the minibuffer no longer autocompletes in the same way, so if I’m going to have to relearn how to get to them, I should really re-learn the short versions.

Anyway, I did want to take a moment to sing the praises of the GNU Emacs Help facility, because it has been invaluable. Specifically C-h f (describe-function) and C-h v (describe-variable). While the help they give is necessarily brief, it’s often enough for someone like me, who just needs a gentle prod about something now and again. I even used C-h f to look up the documentation for if a few minutes ago, because I couldn’t remember if you had to do progn to do a multi-statement else block.

So that’s it for today. Learn the rich set of commands that let you get right to what you need in the documentation—it will be invaluable for exploring emacs.

One ring to rule them all

I suppose I might have once read that GNU Emacs had a mark ring—that is, a capped collection of marked locations in the current buffer. But I’ve never really thought about it.

I can see a lot of possibilities in the idea, though—it’s implicitly a list of nominally important spots in the buffer, so of course it has the possibility of being useful.

There are two commands that have a lot of potential. Both are variations on C-SPC (set-mark-command).

The first is simply C-SPC C-SPC—that is, set-mark-command applied twice. The first time sets mark and activates it, the second deactivates it so any further motion won’t start establishing a region.

Having set the mark, you can next use C-u C-SPC, which is simply applying the universal argument to set-mark-command. This will start cycling back through the mark ring, with each invocation moving further back.

This may just be something I have to use repeatedly even when I don’t need to, to start finding situations where it will truly be helpful.

Building blocks

The two tasks I do most in Emacs (which are two of the three tasks I do most at the computer) are read email and write programs.

I probably spend far more of my time in those tasks editing than I ever do writing—that is, I may write a first draft of a paragraph in an email, or a function in a program, in five minutes, and then spend fifteen getting it right.

A large part of that process will involve moving stuff around, sometimes in large chunks—because the flow of a program or a document isn’t just about one sentence coming after the other, but about how the ideas embodied in whatever you’re writing flow through the entire construct. If you don’t edit your emails and programs a lot, if you don’t spend more time in the refinement than the first draft then, well…I don’t know if I want to read your emails or source code.

Anyway, it’s important to me to be able to cut and paste stuff efficiently, and, again, this is one of those things where my knowledge isn’t as comprehensive as I would like. I’ve known about C-x h (mark-whole-buffer) for roughly forever, but that’s about it besides the normal set-mark-and-move behavior. I’ve got a bunch of new things to work with now.

M-@ (mark-word) is one I hope to get down pretty quickly. To have the cursor stay where it is, but have the region extend itself quickly should make a lot of email-y sorts of tasks much simpler.

Similarly, M-h (mark-paragraph) will be a nice addition to the toolbox when writing emails, or when writing things like blog posts—I often decide to reorder stuff on a paragraph-by-paragraph basis to help the flow of my ideas.

I don’t know how much use I’ll get out of C-M-@ (mark-sexp) in the short term. I will try to remember it when I’m working with lisp, but honestly, I don’t do so much of that, at least not yet—I have ambitions. So perhaps it will come in handy.

C-x C-p (mark-page) probably isn’t going to see a lot of use from me—I don’t use the whole ^L-delimited pages thing at all, really.

C-M-h (mark-defun) has some potential. As long as its implementation is up to snuff with regards to CPerl or Haskell modes—that is, in those modes, it has a sensible idea of what constitutes a function definition—I could see myself finding a lot of use for these things.

Do it again (again)

Q: How did I not know about this? A: You never read the manual.

(A close friend of mine from college with whom I’ve worked professionally now and again, once suggested that the secret to our individual successes was that we were the ones willing to read the manuals. Obviously in this instance I failed.)

Oh, good lord, the number of times I’ve done M-x UP RETURN RETURN to re-do query-replace on a new buffer, when I could have just done C-x ESC ESC (repeat-complex-command).

Yeah, I realize that it’s really only one fewer keystroke, but I can do that set of keys more efficiently. And I suspect that if I’m paying attention, I will find that I have far more opportunities to use it than just speeding up search and replace operations.

Of course, M-x list-command-history is a great complement to it, to see what you have done recently. It will be interesting to play with—I was honestly surprised at the contents when I tried it just now.

In which I humble myself terribly…

This is how poor a user of Emacs I am: I still habitually use C-@ (set-mark-command) to set the mark. I mean, on what planet is that easier or more ergonomic than C-SPC (set-mark-command)?

I actually started retraining myself to use C-SPC a while ago, and it’s mostly taken—but I still occasionally catch myself going for that awkward pinky-thumb-middle-finger chord that fires C-@.

More interesting for me to learn how to use effectively is C-x C-x (exchange-point-and-mark). I know I spend a lot of unnecessary time scrolling around the screen rather than targetting where I want to go. Internalizing C-x C-x is, I think, the first step in moving a little faster.

But there are other tools, as well.

Even faster ways to get through the minibuffer

I am a long-time user of short-lived Emacs sessions who is now trying to take advantage of having a browser that runs for days or weeks at a time—I’ve configured my desktop shell to auto-start emacs --daemon when I log in, and I rarely restart it (though it’s probably still do it more often than a really hard-core Emacs user would).

As a consequence, my minibuffer history starts to fill up with good stuff that I want to re-use in order to increase my efficiency.

I have little doubt that at some point in the past I’ve typed C-r (isearch-backward) to do an i-search-backward on the minibuffer, only to be rebuffed (see what I did there!). And then I probably started hitting the UP (previous-history-line) key to get back to where I wanted to be, assuming I didn’t just give up entirely.

To know that M-r (previous-matching-history-element) and M-s (next-matching-history-element) start forward and backward searches through the minibuffer is pretty nice. And if they’re not isearch, at least they’re regexes.

Staying on home row…

I’ve long known about using the UP (previous-history-element) and DOWN (next-history-element) keys to move through the minibuffer history. It’s always been a little jarring that C-p (previous-line) and C-n (next-line), which I’m very habituated to use (perhaps even more than the actual arrow keys, since I touch type, and can use them without moving my hands) don’t work in the minibuffer—Emacs just beeps unhappily at me.

I was pleased to find out that, in addition to the dedicated arrow keys, M-p (previous-history-element) and M-n (next-history-element) scroll through the minibuffer history. The should be much more easily accessible to me—and I do scroll through the minibuffer a lot.

Do it again

C-x z (repeat) is a little bit of a mystery to me.

That is, I don’t know when you would use it.

I mean, if you need to kill five lines, giving a numerical prefix to the kill line command, like M-5 C-k, makes good sense to me—you have an idea of how much you need to delete, do it all in one fell swoop.

Or if you’ve got a simple set of commands you need repeated, whip up a small keyboard macro, and then repeat it with C-x e (kmacro-end-and-call-macro), and then keep hitting e until you’re done.

But I’ve been trying to figure out when I’d need to use C-x z and I can’t come up with a situation where it’s not going to be as many keystrokes as whatever I was already going to do.

Still, hope springs eternal. Now that I’m aware of it, perhaps I’ll start noticing uses for it.

Further remedial keystrokes

As I’ve mentioned before, the first systems on which I used Emacs had keyboard support that was spotty-to-nonexistent for anything but the most common keystrokes.

Consequently, I learned to use ESC for a lot of stuff where you’d use Meta. For most of those things I’ve retrained myself, but until I was actually reading the GNU Emacs Manual, I didn’t know that you didn’t have to hit ESC to do a numerical argument.

So where I’ve often done ESC 5, say, to do a numeric argument to a function, I’ve now learned that I can simply do M-5, which is more like a single keystroke.

I do expect it’ll take a bit to train myself out of my existing habit, but I’m hopeful.

Closing down the competition

Well yesterday I talked about C-o (open-line) that opens up additional lines beneath the line you’re currently on, and how little I used it.

A related key that I use even less, but could see more opportunity for, is C-x C-o (delete-blank-lines), which collapses repeated lines of whitespace into a single line.

The first thing I find myself wondering, though, is “Why C-x?” It seems to me that most other commands like this—variations on a shorter set of keystrokes—use the C-u prefix to say, “do the opposite-ish”.

That aside, I’m going to try and keep this keystroke in mind, because it seems like something I will find a use for, if only I can remember it.