Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Command-Line Key Bindings

20 Nov 2005

Productivity shoots up the moment you understand that key bindings are the way to get stuff done. Even more so when you find most programs share the same ones.

1 Readline

Like it or not, in the UNIX world, there seems to be a supremacy of Emacs-like bindings. They are those bindings which move the cursor to the line start with Ctrl A , which clear the line contents before the cursor with Ctrl U , which redraw the screen with Ctrl L , [...] It's that set of key bindings we all seem to know a good chunk of, without really knowing where from. These key bindings which seem to creep in everywhere, from password prompts, to Mutt input fields, to shells, even to the Vim command-line.

While many programs such as Mutt only attempt to mimic those bindings, it turns out some other ones share a common library called Readline. It means that all the software that uses Readline will share the same behaviour which means that you'll feel right at home from the start. This will become so addictive that you'll soon choose software depending on whether or not it comes with Readline support.

2 Readline Configuration

In fact, you'll soon find that Readline is not specific to Emacs-style key bindings. It's not specific to any style of key bindings, for that matter. You can define that style in the ~/.inputrc configuration file (or /etc/inputrc). It can be as easy as setting the editing mode to vi:

set editing-mode vi

... but you can also bind any of the editing commands to keys of your choosing.

3 Readline Wrappers

What about those programs which don't support Readline and don't even try mimicking common key bindings? What about those programs which don't even try to be helpful to begin with? I'm always thinking of the infamous SQL*Plus, which is probably the very reason I started looking into where useful Readline key bindings come from and how to make them available everywhere.

There's a handful of programs which do this. They wrap up the command you give them as argument and let you use it with cleverer bindings. I've had a rather pleasant experience with rlwrap – Readline wrap. Take for instance /bin/sh which doesn't have any convenient bindings. Run it like so:

rlwrap sh

... and you'll find you can all of a sudden use Readline bindings, just like magic. Similarly, ledit – for line editor – will let you do the same. But it will not take your ~/.inputrc into account and has a configuration file of its own – ~/.leditrc. What's more, it discards the prompt, making it all a bit confusing. I've not had so much experience with rlfe which works just the same as the other two alternatives. Unlike ledit, it will take your ~/.inputrc into account and will display the prompt properly.

These programs work for shells and are not by any means intended for curses-like programs. You can give it a try: at best, inputs will work like before, without any particular key bindings. At worst, they will seriously mess up your user interface.

4 Wrapping with Editors

My first experience wrapping commands for line editing was in fact Emacs. Start Emacs, hit Alt X before typing shell and launch any command where you'd like to enjoy Emacs key bindings. This goes not only further because you can use the whole set of clever key bindings Emacs has to offer. But you can also work directly with program output. But still no way to use curses or other graphical terminal programs.

The Conque Vim plug-in offers similar facilities. It may be that installing e.g. the vim-conque package isn't enough and that you'll need to run:

vim-addons install conqueterm

... to use it, which is achieved by running in Vim a command along the lines of:

:ConqueTerm sh

If Conque is the one that pulls it off best to run graphical terminal programs, I still wouldn't do so as it's still a rather dodgy business.

5 The Python cmd Module

If you need to write your own command-line interface which uses Readline, the cmd Python module is a good choice and will make Readline work out of the box for you. I wrote a wrapper to it, the cmdline module, which will make your life even easier.

6 References