vi and cursor keys

If you use the vi editor, and you sometimes find that your cursor keys don’t work, then you should read on. Anyone else should go and do something more fun. And we vi users should probably give up on it and use another editor. It is the 21st century, after all.

I often have this problem, and it’s generally when I’m ssh-ed into a remote Linux box, which is the main time I use vi. The problem is that the machine at the far end has the wrong idea of what type of terminal you have. In my case, it tends to think that I’m on an ‘xterm’ or an ‘xterm-color’, which I’m not, but that’s what the Terminal app on my Mac is configured to look like, and the value gets carried through to the other end.

The best thing seems to be to set the terminal type to ‘linux’ on the remote machine. There are several ways to do this:

  • You can set it at the local end for the ssh process:
    TERM=linux ssh me@remotemachine
    and the value should be carried across.
  • You can set it in the environment at the remote end, eg.:
    export TERM=linux
    vi filename
  • You can set it in vi, once you discover things aren’t working:
    :set term=linux
  • You can put set term=linux in your .vimrc file at the far end. This seems to me to be the most useful if you hit the problem regularly.

© Copyright Quentin Stafford-Fraser