VIM strange behaviour F1-10

3

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.

Predator117

Posted 2011-03-16T19:18:37.953

Reputation: 181

Sounds like a terminal setting. You should probably ask this on http://askubuntu.com/.

– Michael Todd – 2011-03-16T19:21:53.430

Answers

8

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

ZyX

Posted 2011-03-16T19:18:37.953

Reputation: 253

omg yes this is the solution, you made my day – None – 2011-03-16T20:51:42.477