Jaunder

#common-keys

Posts by ~mdorman
M
Michael Alan Dorman@mdorman

The joy of an integrated environment

elisp programming is easy when references are at your fingertips

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.

M
Michael Alan Dorman@mdorman

One ring to rule them all

Using the Mark Ring

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

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.

M
Michael Alan Dorman@mdorman

Opening up your document

I'm actually a bit of a fan of whitespace in code. I know a lot of people who…are of a different opinion, to say the least. Perhaps it's because I don't have some of the more sophisticated code-motion keys down in Emacs, but I like to have the whitespace to break up semantic units and help me understand how lines of code are interrelated.

So it's surprising that I haven't ever really cottoned to C-o (open-line).

Actually, that's not 100% true—in the readline library that underlies the history system in the bash shell, I use C-o a lot; it is useful for when you have a repeated series of commands you want to cycle through again, you can find the first command in the series and then step through with it.

But in Emacs, I've never used it much.

However, what I do find myself doing a lot is C-q (quoted-insert) C-j (newline-and-indent) in the mini-buffer, during search-and-replace sessions. And for that, C-o may be, at times, an easier option. So I'll try and adopt it, and see how it goes.

M
Michael Alan Dorman@mdorman

The existential pain of naming Backspace and Delete

When I first started using uEmacs, one of the most confusing things was the fact that the adaptation had not been entirely completed, or at least on my platform (an Atari ST), the keyboard mappings were not entirely idiomatic—they hadn't kept up with the differences between VT-100-style keyboards and, well, everything else.

I think this explains why I got very habituated to the alphabetic keys for deleting stuff, but don't have the non-alphabetic keys as deeply ingrained.

So, while C-d (delete-char) for deleting the next character is definitely embedded in my fingers, and I use C-k (kill-line) like an old friend, and even M-d (kill-word), I still have to think hard before using M-DEL (backward-kill-word)—to the extent that I am far more likely to do M-b to move back and then do M-d to delete the words I just backed up over.

So I'm trying hard to get used to using M-DEL (which is Alt-Delete on most PC-flavored keyboards), because it is actually a fairly common operation, and I want to be more fluid with it.

M
Michael Alan Dorman@mdorman

I thee undo

For years I've been using C-x u (undo) for undo. As much as I use it, though, I really need to get C-/ (undo) under my fingers.

I never remember revert-buffer, since I rarely get myself to the point where I want to just nuke everything from orbit—so perhaps it's not surprising I rarely think about it.

In fact, I think the interface of GNU Emacs' undo facility is one of the few places where it falls dramatically short of its potential. I really don't know what sort of facilities that other editors have, but given the way that Emacs stores the history, I'm surprised that the baked-in functionality provides no way to access it more efficiently—I mean, there's not even an explicit redo command, you have to just undo your undoings, ad infinitum. I've often accidentally started redoing things when I hit an injudicious key, etc.

Poking around EmacsWiki, I find a reference to UndoTree, which looks like just what I might like. Perhaps I shall package it up and see how well it works. If so, I'll report back.

M
Michael Alan Dorman@mdorman

Just getting around

The this blog had its genesis when I sat down to read the GNU Emacs Manual while we were travelling over the holidays—I figured I could skim it, maybe pick up one or two new things, but, really, it would mostly be just speed-reading.

What it actually proved was that I had never tried to read the manual recently, perhaps ever. I would stumble across basic stuff I feel like I should have known all along, and then an hour or two later would have to work very hard to remember what it was that I had stumbled across.

I have come to realize I retain things better if I write them down—I still don't understand how I got all the way through college, since I never took notes worth a damn. So this is my public notebook.

While I know M-f (forward-word) and M-b (backward-word) at such a low level that I thought I didn't know them until I used them without thinking about it, I didn't know about M-RIGHT/C-RIGHT (right-word) and M-LEFT/C-LEFT (left-word), which do basically the same thing, but are a little bit more ergonomic since you're not doing both keys with the same hand. Something to retrain, I suspect.

I probably also need to try to train myself to use my right Meta key more; unfortunately, on my laptop keyboard, it falls somewhat awkwardly under my thumb, so it may take some time to make that transition.

Finally (for today), there's M-g M-g/M-g g (goto-line) which I use at least a couple of times a day…by typing M-x goto-line. I think this will be a little faster.