bash comand line

For a few years now I've been using vi editing mode for bash and anything that uses readline. Here is how I've set things up.

In .bashrc I use the following to enable vi editing mode:

        set -o vi
This allows me to type as usual and use ESC to get into vi command mode. Since ESC is so far away I frequently use control-[... unless I feel I need the exercise.

Ok, so the whole ESC and even control-[ sometimes gets tired. For example if I want to search history I would have to push ESC to get into vi-command mode and then be able to push J or K to go through history. I found a way around that by using the 'meta convert' feature of many terminal programs. In xterm, for example, setting the eightBitInput to false will cause xterm to convert all Meta-something key combinations to ESC-something which means that I can push Meta-k to go up 1 in history. It just so happens that all the other terminals I've used have this working already, but anyway, here is the line in my .Xdefaults:

        xterm*eightBitInput: false
Note that once you push Meta-something you are in vi-command mode so to go up by two lines in history you would use Meta-k k, or use J & K to go up and down as you wish.

everything else

Many programs use readline, as bash does, to present a command line to the user. In particular I wanted to get lftp, and others, to use vi editing mode as well. For this I set the following in the .inputrc file:

        set editing-mode vi
        set keymap vi
	set convert-meta on
Now when I start up lftp, I can use vi editing just like in bash.

making tab completion more advanced

I noticed that the vi editing mode in bash has much fewer commands defined then the emacs counterpart. The commands are available but just not bound to any shortcuts. Here is a list of extra bindings that I've added to make TAB-completion, which makes bash so great, a bit more useful:

        # ^p check for partial match in history
        bind -m vi-insert "\C-p":dynamic-complete-history
    # ^n cycle through the list of partial matches
    bind -m vi-insert "\C-n":menu-complete</pre>

The comments give away the secrets of what the commands do. They don’t work exactly like the vi equivalents for completion, but also provide a bit more functionality.

clearing the screen

Last feature I missed was to be able to clear the screen with control-L, as defined in emacs editing mode. I've added the following to bind the same in vi editing mode:

        # ^l clear screen
        bind -m vi-insert "\C-l":clear-screen

further reading

Here are a few links that you might find useful: