How to disable the arrow keys in vi or vim

» Posted by on Jan 13, 2009 in Blog | 4 comments

If you are learning to use vi or vim, one of the recommendations is not to use the arrow keys (cursor keys) but instead use the hjkl keys.  There are two main reasons for this:

  • Speed.  After you learn to use the hjkl keys, they are much faster since your hands can stay on the home row.
  • Universality. In some older systems, vim may not be installed and the original vi doesn’t use the arrow keys.

But if you are just starting to learn to use vi, how can you force yourself to use the hjkl keys?  The easiest way is to disable the arrow keys, which is simple if you know how but hard if you are just starting out. To disable the arrow keys, the best way is to use the key remapping functions and save these in your .vimrc file.  If like me, you use Windows, OSX and Linux you can copy these lines to all your .vimrc files.  I mention this because I’ve seen other recommendations using system key remapping.

And here are the magic lines

 noremap  <Up> ""
 noremap! <Up> <Esc>
 noremap  <Down> ""
 noremap! <Down> <Esc>
 noremap  <Left> ""
 noremap! <Left> <Esc>
 noremap  <Right> ""
 noremap! <Right> <Esc>