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.


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

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).

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

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.