Archive for the ‘Korn shell’ Category

Using Vi commands to control your shell (ksh, Bash)

Saturday, September 13th, 2008

Recently I was forced to use the Korn Shell (or ksh) when I had to perform some minor tasks on an AIX server. So of course I just jumped in and started using the shell. A few seconds later I was thoroughly frustrated as I discovered I had no command history and couldn’t use backspace or command completion (as you can probably guess I’m a Bash user).

The Vi mindset

So begrudgingly I resorted to a few web pages to work out what was going on. To my pleasant surprise, all I had to do was switch my thinking from Bash to Vi. I’m a big Vi(m) fan and feel quite at home using Vi like commands so I was quite happy to discover this was all I had to do. Command history: just hit ESC and use the Vi “up” which is “k” to go back in history, or Vi “down” which is “j” to go forward in history – easy.

Keeping in this mindset I began investigating other commands. Jumping back in history, then a quick “cw” to change a word, “A” to append to the end of that command. I wont bore all the people that don’t use Vi(m) with a list of commands, because if you don’t, then all this probably sounds like a pretty painful way of using a shell. But if you do use Vi(m) regularly, you quickly learn that a few carefully selected keystrokes can save you many repetitive backspaces or left arrows etc.

Escape from home

The other great thing about using Vi(m), or a Vi like interface, is that your fingers never need to leave the home keys. The only exception to this is that pesky ESC key that has been banished to the far corner of the keyboard. But never fear, there are some fixes to this.

The answer is to remap your CapsLock key to be another Ctrl key. Not only does this open up a wealth of shortcuts that can be performed without taking you fingers away from the home keys, but it also allows you to type an ESC equivalent, which is Ctrl-[.

If you are running X11 you can do it in the (re)configuration. Running Gnome you can do it through keyboard preferences, I’m guessing there is something similar in KDE and other window managers. You can even do it in Windows with a simple registry hack.

More information for Linux, Mac and Windows at the below links:

http://www.manicai.net/comp/swap-caps-ctrl.html

http://johnhaller.com/jh/useful_stuff/disable_caps_lock/

Dessert time

Anyway, whilst I was pretty impressed with the interface that ksh had on offer (once I understood it), I wasn’t totally sold. Command line completion was there (ESC+\) but it was nowhere near the level of Bash’s usefulness.

But…..inspired by this discovery, it didn’t take long to work out that I could have my cake and eat it too! A simple command in Bash allows these Vi like commands to be used:

set -o vi

This way you can keep on using Bash as you would, but hit the ESC key (or Ctrl-[) and you have the power of Vi at your fingertips!