Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  maxmackie 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. –  maxmackie 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

1 Answer 1

up vote 1 down vote accepted

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
endfunction
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>')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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