Sometimes when I fire up vim, my Insert key doesn't work (need to use i) and my arrows don't work (they give me letters instead).

I'm guessing this has to do with my keys not being mapped correctly. How can I fix this for good?

  • It probably has to do with what kind of terminal you are in and whether the shell is correctly mapping the key-codes for that terminal. You will need to trace this to some specific scenario such as "It works in xterm but not urxvt" or "it works on my computer but not when I'm sshed into something else" or it "works from the console but not in x" or something like that. – Caleb Aug 20 '11 at 12:25
  • At first, I thought about that. But when I'm not in vim I can browser through my history with the up and down arrows (I'm using bash and xterm). So I figured if they react normally in regular shell, it should be a vim issue. – n0pe Aug 20 '11 at 12:43
  • That is not necessarily the case. The shell is more tolerant of alternate key patterns and might bind a number of different possibilities to a given action. Vim does much more with the keyboard and has to be much more specific with it's bindings. The wrong TERM settings might not be evident in the shell where a lot of things are generalized but would turn up when passed to a program like vim. You still need to look for what the difference is in your environment when it works and when it doesn't. – Caleb Aug 20 '11 at 12:54
  • Yeah that would be a good way of determining what the problem is. However, this problem is on my web hosting provider's servers and they only provide me with an xterm, so I can't really test much. I might open an assistance request with them unless I can find where xterm keybindings are located. – n0pe Aug 20 '11 at 13:01
  • You don't need to find xterm's keybindings. You said this sometimes works and sometimes doesn't. You need to figure out what the difference in context is for when it works and when it doesn't. Does it matter what computer you are connecting from? What on earth do you mean by "your hosting provider provides you with an xterm", that is a strange statement. Do you have physical access to this machine? Are they X-forwarding you something? Do you mean they provide you a shell account you ssh into? What OS and Desktop Environment are you in? What is the server you are being provided with? – Caleb Aug 20 '11 at 13:11

You need to declare the escape sequences sent by your usual terminals in your ~/.vimrc. In theory, escape sequences depend on the terminal. In practice, if an escape sequence corresponds to a certain key on a given terminal, other terminals either send it for the same key or don't send it at all, so you can just pile on the definitions and not worry about conflicts.

To find out what escape sequence a key sends, enter insert mode and press Ctrl+V followed by the key. This inserts the escape sequence literally.

Put directives like these in your ~/.vimrc (using the escape sequences that you've observed):

function Allmap(mapping)
  execute 'map' a:mapping
  execute 'map!' a:mapping
call Allmap('   <ESC>[A         <Up>')
call Allmap('   <ESC>[B         <Down>')
call Allmap('   <ESC>[C         <Right>')
call Allmap('   <ESC>[D         <Left>')
call Allmap('   <ESC>[F         <End>')
call Allmap('   <ESC>[H         <Home>')
call Allmap('   <ESC>[5~        <PageUp>')
call Allmap('   <ESC>[6~        <PageDown>')
call Allmap('   <ESC>[k4~       <C-Left>')
call Allmap('   <ESC>[5D        <C-Left>')
call Allmap('   <ESC>Od         <C-Left>')
call Allmap('   <ESC>[k6~       <C-Right>')
call Allmap('   <ESC>[5C        <C-Right>')
call Allmap('   <ESC>Oc         <C-Right>')
  • 2
    I stumbled on this while searching for vim keycodes, and I have to say, its the wrong way to do custom keys in VIM. If you "map" <esc>c to <Right>, then anytime you press "esc", VIM waits for the next character to see if its a "c", or something else. To get out of insert mode for example, you start having to press "ESC" twice in a row. Alternatively, use "SET" set <Right>=(press ctrl-v then escape)C Then VIM knows '<esc>C; is a keycode, and pressing escape on its own continues to work as normal. – Dave Jan 4 '17 at 3:50
  • 1
    @Dave I have something like this in my .vimrc and pressing ESC gets out of insert mode immediately. – Gilles Jan 4 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.