Key Bindings in Bash and Vi

So I was typing away in an ssh window today when for the billionth time I had hit the END key expecting the cursor to jump to the end of the line, and it just printed a ~. As always, I just erased the character and then held down the arrow until I was at the end of the line. However, seeing as it was the billionth time I decided to figure out how to make it actually do what I want.

Not that I didn’t really know how to make it do what I want, I’ve just always been too lazy to actually look up the escape codes and such for the home and end keys, and then actually create the entries to fix it. So for the benefit of any other fellow lazy people, here’s what you need to do:

Create ~/.inputrc and put this in it:

“\e[1~”: beginning-of-line
“\e[4~”: end-of-line
“\eOH”: beginning-of-line
“\eOF”: end-of-line
“\e[H”: beginning-of-line
“\e[F”: end-of-line
That should cover most of the bases terminal-wise.

And to make sure that home/end work in Vi, this is what I added to .exrc:
map [CTRL-V] [HOME] ^
map [CTRL-V] [END] $

When I say [CTRL-V] I mean actually press the keys ctrl and v, not type that out, of course.

There are probably better ways to accomplish this, but this worked for me. Feel free to suggest a more elegant solution.

One thought on “Key Bindings in Bash and Vi

  1. Just a note, using the .exrc above will “break” vi if you are using it in some consoles (I noticed it in Konsole under X). So just be careful where it’s used…

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image