Tuesday, January 13, 2009

UNIX: Nip That Elusive Backspace Key in the Bud! (3 ways to overcome an old, but annoying problem)

The problem that I'm about to address is nothing new at all, in fact it's been around for yonks and is specifically related to running an old shell (such as ksh, sh or csh). It's amazing how easy it is to take the bash shell for granted, until you don't have it anymore.

Yesterday I had the pleasure of being granted telnet access to our development server at my new job, an AIX boxen. Before then I'd never physically worked on AIX before; but as you have probably experienced, one Unix is pretty much the same as the next, so no problems there.

I logged in and was greeted by an annoyance I hadn't encountered in years; pressing the backspace key only resulted in a bunch of ^?^?^? characters appending to the command I'd been trying to erase. Thinking back, this is one of the first things I learned in Unix; but that said, it doesn't mean it's common knowledge. This was made more evident when a colleague of mine who had been standing next to me at the time asked "where's backspace on this damn thing".

So this is how you overcome this problem:

  • Find the elusive backspace key

    it's usually ^h (ctrl + h), ^del (ctrl + delete) or something to that effect

  • Tell the shell which key you want it to be (My preferred solution)

    stty erase {press the backspace key} and enter

    You have now reconfigured the shell to use the backspace key to perform a backspace; as you'd expect it should work.

    tip:  Place the above command in your .profile file to avoid having to enter it each time you log in. Or have the systems administrator place it in /etc/profile to enable the option globally.

  • Use VI key-bindings

    tip: If you're a VI fan (like me), another useful one to remember is

    set -o vi

    This will enable VI key-bindings in your shell and can also be appended to your .profile so that it's enabled automatically. I personally would not work in a shell without this feature enabled, it's amazing how much faster you can get things done using it.

    Using VI key-bindings, esc + x or X will perform a backspace nicely. You can even try esc + v to edit the command in VI itself.

    tipesc + k will allow you to scroll backwards through the commands you've issued; or esc + / or ? {search string} can be used to search for a previously issued command. This is especially useful as modifying previously issued commands beats the pants off having to retype from scratch if you're in a hurry (obviously).

If you have to work in the console often, check out the man page for stty, there's loads of options available that just might make things a bit more plesent for you.

No comments:

Post a Comment