I prefer using vi as my text editor, because it is clean and simple. I don't have to go searching for commands from drop-down boxes and other GUI crap. But arrow keys and backspace are sometimes problematic, depending on the OS you're using. In my case, I frequently ssh from Linux to SunOS, and the SunOS vi doesn't like my keyboard mappings.

Here is my (partial) solution. Open ~/.exrc and add the following lines. In case it is not obvious, do not type the letters inside brackets, but press those keys instead (i.e., [ctrl-v] means you hold the Ctrl key while pressing v).

:map! [ctrl-v][backspace] [ctrl-v][esc]xa
:map! [ctrl-v][up-arrow] [ctrl-v][esc]ka
:map! [ctrl-v][down-arrow] [ctrl-v][esc]ja
:map! [ctrl-v][right-arrow] [ctrl-v][esc]la
:map! [ctrl-v][left-arrow] [ctrl-v][esc]ha

When you type these in (assuming you're using vi or vim, these lines will look like this.

:map! ^? ^[xa
:map! ^[OA ^[ka
:map! ^[OB ^[ja
:map! ^[OC ^[ha
:map! ^[OD ^[la

So what's going on here? The [ctrl-v] is some sort of meta-character that indicates a key mapping. So the ^? is the default mapping for the backspace key. That's why you would see ^? if you hit backspace while typing. The map command is telling vi to issue a series of keystrokes when the backspace is pressed. Specifically, we are telling it to interpret the backspace as the sequence [esc] (exit from edit mode) x (delete the current character) a (re-enter edit mode with the append command). Similarly, the ^[OA line tells vi to interpret the up-arrow key press as the sequence [esc] k (go left one character) a (append and edit). Try to play around with more mappings, such as mapping the home key to ^ and end to $.

Note that the backspace mapping isn't ideal. It works well if you're at the end of the line. For my purposes, this is fine, as most of the time I hit backspace is when I make a small mistake typing new material. But if you're in the middle of the line, this mapping will not match the traditional behavior of backspace. Specifically, if you have "abcd" and you hit backspace when the cursor is over the "c" you will delete the "b" (as intended), but the cursor will then be placed after the "c" rather than before it. The source of this problem is the semantics of the x command. If you use x to delete a character at the end of the line, the cursor will be placed over the new last character (which preceded the deleted character), so you want to use a to append new text. But if you x a character in the middle of the line, the cursor is placed over the character after the one you deleted and you want i to insert. I haven't figured out how to match both semantics. In the meantime, 99% of my backspace usage is at the end of the line, so I prefer the mapping to append mode.

Last Updated: October 04, 2011

  viewed with Mozilla FirefoxValid XHTML 1.0!