Please join me at my new location

Tuesday, August 21, 2012

Universal Command

I used to love emacs. It was a great text editor and while it had a reputation for being somewhat difficult to use when first learning, the amount of stuff you could do with it was jaw dropping. With a piece of software as big and complex as emacs there needs to be some way for users to not only quickly do what they want to do, but also discover things that they didn’t know they could do. Emacs has such a feature. It’s called the minibuffer.

The minibuffer is a small area at the bottom of your editor window that allows you to quickly type in a command to execute. Emacs is essentially a framework built out of small functions that when combined together make an editor. There’s a function for moving forward one letter at a time. There’s a function for moving backwards one word at a time. There’s a function to insert the letter being pressed into the buffer[1]. There’s a function for basically everything you can do with it. Many, but not all of these functions are interactive, meaning they can be executed directly by the user. And these commands can be executed by typing their name into the minibuffer.

For example, to move forward a word you can type M-x forward-char <RET>. That is: press the meta key[2] and the ‘x’ key at the same time. This focuses you on the minibuffer. Type in ‘forward-char’, and hit the return key. This will move the cursor to the next cursor. Trivial example, I know. Obviously moving forward one letter has been bound to the right arrow key so you don’t need to do this, but you get the idea.

One thing I always missed when using other programs was the ability to just tell the program what I wanted to do with the keyboard instead of having to use the mouse. I wanted to be able to tell Pixelmator to duplicate a layer; something that Pixelmator doesn’t have a keyboard shortcut for. Well, OS X actually lets you do this. It’s not exactly the same thing as the minibufer in emacs, but it gets pretty close.

So what exactly is this feature? Well, under the Help menu in every program on OS X there’s a search box. If you type in the name of a menu command into that search box you’re able to arrow down to it and not only will OS X show you where the command is for future reference, but it will also allow you to invoke it by pressing the enter key. Just having the menu isn’t enough though. It needs to be accessible via the keyboard. Good news, it is. Pressing Command-Shift-? will open the Help menu with the Search box highlighted.

This is something I use on a daily basis to get my job done. It’s great for those times when you need to invoke some feature of an application that doesn’t have a keyboard shortcut assigned to it.

  1. In emacs parlance, a buffer is an editor window.  ↩

  2. More emacs parlance here. The meta key means either the escape key, or the left alt or left command key if you’re on a Mac. This is just left overs from the era in which emacs was created.  ↩