Keybinding Science: Emacs C-u vs Prompt

By Xah Lee. Date:

There's something interesting on keybinding science of emacs C-u system and prompt system. In summary, a combination of C-u with prompt is better than C-u itself. Following is explanation.

Emacs has a system to feed (mostly) numeric args to function when the function is called interactively. That is, you call universal-argumentCtrl+u】 first, then optionally type some numbers, before you call the command. (for elisp detail, see: Emacs Lisp: Get universal-argument)

This system, can be used as part of the key system. That is, any keybinding of a command, can be preceded by 【Ctrl+u n】 to do variant of the command.

emacs doesn't explore this much, only for commands that make sense of repeating n times. Commands that does not make sense to repeat, usually does not support this.

A systematic use, is that every command will have C-u variation in a consistent way. For example, 【Ctrl+u】 will always do reverse (as in {undo/redo, fill/unfill, encode/decode, …}), and 【Ctrl+u n】will do variations. We will need to survey at all commands to categorize their behavior characteristics they have in common, in order to design this system. (For example, what percentage command make sense for repeating n times, what percentage has a logical reverse, what are the command variant behaviors?)

A alternative similar in comparison to the C-u system, is actually do prompt instead. When a command that does prompt, the prompt can be made to accept unique keys such as digits, then this can be thought of as a key system. For example, after you call a command (say, by pressing F9), you can type 3 then Enter ↵. (For a example, see the command xah-shell-commands at Emacs: Interactive Abbrev)

Prompting is usually annoying, but this can be solved by not prompting unless 【Ctrl+u】 is pressed first.

When considered like this, then, this is the equivalent to the C-u system. Which is better?

The problem with just the C-u system, is that often you forgot what the different numbers do for the command. You need to describe-function or describe-key first to look up the doc. With prompt, it says right there. With prompt system, you don't have to lookup the doc first, as the prompt guides you. (and you don't have to spend time to read the prompt, when you memorized the key by muscle memory you just directly type keys)

The disadvantage with prompt system is that you'll need one extra key Enter ↵ at the end.

Overall, i discovered, the prompt system is better. The looking up the doc first with C-u system is too costy.

So, for example, for the command Emacs Lisp: Twitterfy, i need a variant behavior to untwitterfy.

this can be done with a new command, which would need a new keybinding. Taking another precious key spot is not so good. Or, it can be with a C-u system. For example, 【Ctrl+u】 and 【Ctrl+u 1】 will do untwitterfy, while all other numbers will do twitterfy.

But i've realized, best is to do prompt. So, when called with C-u first, it'll prompt using ido with 2 choices like this: {twitterfy, untwitterfy}. Then user can type 1 more key (t or u), then press Enter ↵. The prompt can be made with numbers, such as {“1 twitterfy”, “2 untwitterfy”}, or more options.

Another command of this example is: Emacs: Convert English/Chinese Punctuations

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs