Using Key Chords In Emacs

Last week I wrote about David Andersson’s Key Chord Mode that I discovered in the latest Emacs Rocks video. I thought it was interesting and I enabled it in my Emacs by adding

(require 'key-chord)
(key-chord-mode 1)
(key-chord-define-global "dq" 'dq)

to my scratch buffer and evaluating it just to play around with it a little. I thought it was pretty nifty but I didn’t think I would actually use it so I didn’t add it to my init.el file.

Yesterday, as I was writing the Automatic Lookup From Emacs post, I wanted to set the function names in the first column of the table in a constant width font. In Org mode you do that by surrounding the name with equal signs like so

=lookup-google=

But the equal sign has a special meaning in an Org-mode table and it causes an error when you try to use it to mark an entry as constant width. So I set the first couple of names by hand like this

@<code>lookup-google@</code>

where the @ tells the Org exporter to treat the following tag as an actual tag and not translate it to &lt; and so on. The problem is that it’s a pain to type the tags because they’re shifted and the 【@】 key is way at the top of the keyboard.

I thought about writing a quick little function to do it for me but it hardly seemed worth the effort. Then I remembered key-chord.el, which was still enabled, so I added

(key-chord-define-global "<>" "@<>\C-b")

to the scratch buffer and evaluated it. It seems like a small thing but it really made doing the rest of the table a snap. I just typed 【Shift+<+>】 and

@<|>

where | represents the point, was inserted into the buffer. That’s handy enough that I am going to enable chord-mode in my init.el and add the <> chord for those, admittedly rare, times when I need to insert some HTML tags by hand.

Posted in General | Tagged | 4 Comments

VimGolf In Emacs Episode 20 Is Up

Tim Visher has another VimGolf in Emacs video up. This one has some interesting applications of the Emacs calc function. As I mentioned on my old blog, I use a Scheme REPL as my calculator so I’ve never bothered to learn much about calc, and use it only indirectly with Org-mode tables. Visher’s video makes me think that it might be worthwhile to learn some more about it.

Posted in General | Tagged | Leave a comment

Automatic Lookup From Emacs

Xah Lee has released an interesting package called lookup-word-on-internet.el, which he describes on his Web site here. It will take the word at point or a phrase in an active region and look it up in Google, Wikipedia, Dict.org, Answers.com, Wiktionary, or PHP.net.

Lee doesn’t really describe how to use the package on his site so you have to look at lookup-word-on-internet.el to get the details. Basically, there is a little service-specific routine you can call for each service and they1, in turn, call a worker function to do the real work. The service-specific routines are

Function Site Called
lookup-google Google
lookup-wikipedia Wikipedia
lookup-word-dict-org Dict.org
lookup-word-definition Answers.com
lookup-wiktionary Wiktionary.org
lookup-php-ref PHP.net

Of course, for these to be useful, you should bind them to convenient key sequences.

I have a couple of functions like these in my init.el file but this package gives you a fairly comprehensive set in one convenient package and also provides a framework that makes it easy to add your own service-specific functions for sites not in the list.

Footnotes:

1 Except for lookup-php-ref, which does all the work itself.

Posted in General | Tagged | 4 Comments

Emacs Init Time

Just a quickie today. If you have a large and complicated Emacs init file you might wonder how long it takes to process it. Perhaps you should compile it. Maybe it doesn’t matter. How are you to know? It turns out that—to borrow a phrase—Emacs has a command for that.

If you run 【Meta+xemacs-init-time, it will tell you in the echo area (and the *Messages* buffer) how long Emacs took to process your .emacs or init.el file. In my case, it was 1.5 seconds and since my Emacs has been running for 26 days, 23 hours, 47 minutes, 14 seconds (【Meta+xemacs-uptime), I’m not too worried about the 1.5 seconds.

My init.el is fairly long and loads several packages so the 1.5 seconds is reasonable even if I were starting Emacs anew each day. Does anyone have a time significantly larger (say an order of magnitude or more)? Leave a comment if you do.

Posted in General | Tagged | 23 Comments

Reverting In Emacs

One of the things about Emacs is that buffers tend to hang around for a long time. This is especially true if you’re using desktop-save-mode. Most of the time this isn’t a problem but every once in a while the file underneath a buffer changes. Most of the time Emacs will notice this and can run 【Meta+xrevert-buffer to sync things up again. Of course, you can also use revert-buffer to erase all changes to the buffer since the last save.

Sometimes, though, there is another process making frequent changes to the visited file and you’d like to keep the buffer current. For this, there is auto-revert-mode. With a positive argument, auto-revert-mode will track the visited file and automatically update the corresponding Emacs buffer. This mode affects only the current buffer but you can, if necessary, track all open buffers with global-auto-revert-mode. You’ll probably take a performance hit (although I haven’t verified this) if you have a lot of buffers open and turn on global-auto-revert-mode so be careful out there.

A related mode is auto-revert-tail-mode. This mode tracks appends to the file in the manner of tail -f. It needs only check if the size of the file has changed so it’s more efficient when you’re just worried about additions to the file.

Posted in General | Tagged | 2 Comments

Happy Anniversary To Mastering Emacs

Happy Anniversary to Mickey Petersen and Mastering Emacs. Mickey has been posting really great material for a year now and we should all hope he continues. As I’ve said before, Mickey always has something interesting to say and I invariably learn something from each of his posts.

As an anniversary gift from him to us, Mickey has created a reading guide that serves as an index of sorts to the articles on his blog. Hop on over there and take a look, pick an area that interests you, and read some of the articles in that area. You’re sure to learn something worthwhile.

Posted in Blogging | Tagged | Leave a comment

Emacs Key Chord Mode

Magnar Sveen has a new episode of Emacs Rocks up. This time he talks about key-chord.el, a package that lets you press two keys simultaneously (or a single key twice in quick succession) to run a command. For example, I could bind prettify-key-sequence to the key chord 【p+k】 and avoid having to run it with 【Meta+x】.

I probably wouldn’t do that because smex makes calling prettify-key-sequence fast enough for the relatively few times that I call it. There are other cases where it’s just what you need, though. I see its real utility in two areas.

The first, as explored in the Emacs Rocks video is for commands that you run all the time and want to be able to invoke as quickly as possible. Of course, you could just bind those commands to a function key or something like 【Ctrl+c κ】 where 【κ】 is some arbitrary key. The problem with that solution is that there are a lot of useful commands but all except a few of the convenient key sequences are already used, especially if you’re in something like org-mode.

This is an ideal situation in which to use key-chord.el. Suppose, for example, that I was writing fiction with a lot of dialog. It would be great to be able to press 【d+q】 to run the dq command that I’ve written about previously to insert open and close quotes. Actually, key-chord.el lets you do that directly by adding an expression such as

(key-chord-define-global "dq" "“”\C-b")

to your .emacs or init.el file.

In summary, I see key-chord.el being useful to

  • Bind a fast two-key sequence to a frequently used command that is bound to a less convenient key sequence.
  • Extend the number of convenient key sequences.
Posted in General | Tagged | 4 Comments

Three Trees

Scott Chacon, author of Pro Git, gave an interesting talk at Strange Loop entitled A Tale of Three Trees. The idea is to understand how Git works by understanding how it manipulates the three trees: head, index, and working directory.

The talk is a little over 45 minutes and worth a look. It will help you understand some of the fine points of Git. If you do watch it, don’t view it full screen as you won’t be able to see his slides that are displayed in a secondary window.

Posted in General | Tagged | Leave a comment

Unix V6 Lives!

If you’re a Unix nerd like me, you probably like reading through the Unix sources. These days, that’s remarkably easy to do. We have Linux, BSD, Open Solaris, and even some of the ancient Unix sources to peruse. It wasn’t always like that. I remember my excitement when the famous Lions Book was finally made available to the public. I ran right out and got myself a copy the same day I heard about it. For those who don’t know, John Lions’ A Commentary of the Sixth Edition UNIX Operating System was a line-by-line exegesis of V6 Unix. For years it was restricted to Unix source code licensees so it was a moment for celebration when it was finally unencumbered.

Today I learned—or maybe relearned—via OS Blog that V6 Unix has been ported to the X86 architecture and ANSI C. The original V6 sources were written in a pre-ANSI C and don’t compile with a modern C compiler. The nice thing about V6 is that it was a small OS (less than 9000 lines) and pretty easy to understand. The downside was that it was targeted at the PDP-11 so most of the low level machine details and the assembly language bits are opaque to younger1 readers.

Now Russ Cox, Frans Kaashoek, and Robert Morris have resurected it for the X86. The system is bootable or you can run it under QEMU. Even nicer, the authors have a commentary to go along with it. It’s not a line-by-line commentary like Lions’ book but it does explain the details of the X86 that you need to understand the code. There’s also a printed version of the code with line numbers that goes along with the commentary.

If you’re interested in learning some of the low level details of Unix-like systems but find slogging through the Linux or BSD code base too daunting, this could be just what you need. It’s a really great resource and it’s all free. If you want to read about the code but aren’t interested in compiling and running it you can just download the two PDFs. If you want the code itself, that’s available too. The code was ported for use in the MIT 6.828 Operating Systems Engineering course and the course notes are available too.

Footnotes:

1 Younger ≡ Not working on computer systems 30 years ago.

Posted in General | Tagged | Leave a comment

VimGolf In Emacs 19 And A New Trick

Tim Visher has a new version of VimGolf in Emacs up. There are a couple of interesting things in this episode. First, Visher is working on a VimGolf minor mode that loads the challenge into Emacs and provides special functionality useful in working on VimGolf challenges. It’s still very early in the development process, but it’s already functional and looking very nice. Just the thing if you’re interested in joining the fun.

The second thing of interest in the video is a very efficient way of deleting the current line. We all know how to do this with 【Ctrl+a Ctrl+k Ctrl+k】 and probably use it several times a day. Visher’s discovery is that you can do this with a single (modified) keystroke by typing 【Ctrl+Shift+Backspace】. That’s a lot more efficient but will take, at least for me, some muscle memory retraining because 【Ctrl+a Ctrl+k Ctrl+k】 is second nature.

Posted in General | Tagged | 6 Comments