Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Recently I began encountering a really annoying behavior in VIM:

No matter if I am in insert or normal mode, if I press one of the F keys, vim does not what it is normally intended to do:

F1 - inserts a "P"
F2 - inserts a "Q"
F3 - inserts a "R"
F4 - inserts a "S"
F5-10 - inverts case

When using GVim everything works as expected. Any suggestions? I am using version 7.3 on Ubuntu Maverick 10.10. Using terminator but changing to the default terminal does not help.

share|improve this question
    
Sounds like a terminal setting. You should probably ask this on askubuntu.com. –  Michael Todd Mar 16 '11 at 19:21
add comment

migrated from stackoverflow.com Mar 18 '11 at 2:18

This question came from our site for professional and enthusiast programmers.

1 Answer

This is because terminal translates X events into escape sequences like these:

<F1> -> ^[OP
<F2> -> ^[OQ
<F3> -> ^[OR
<F4> -> ^[OS
<F5> -> ^[[15~
<F6> -> ^[[17~

and so on (^[ is a escape character). In some terminals vim is able to get these sequences from terminfo database, but sometimes terminfo database does not match characters actually send or does not contains key_f* entries. In this case pressing <F1> will result in getting escape (escapes current mode unless it is normal mode), O (in normal mode: create a new line before cursor line and enter insert mode) and some character which is inserted on the new line (and for <F5>-... keys ~ is that command that inverts case). You may fix it by putting into vimrc something like that

" Condition should identify terminal in question so "
" that it won't change anything for terminals without this problem "
if !has("gui_running") && $TERM is "xterm"
    for [key, code] in [["<F1>", "\eOP"],
                        \["<F2>", "\eOQ"],
                        \["<F5>", "\e[15~"],
                        \]
        execute "set" key."=".code
    endfor
endif

If your codes are different from that ones that I used as example, use <C-v><F1> (in insert or command-line modes) to get what your terminal is sending (more info in :h i_CTRL-V).

share|improve this answer
    
omg yes this is the solution, you made my day –  Predator117 Mar 16 '11 at 20:51
add comment

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.