Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Vim 7.3.1-762, mintty 1.1.2 (xterm), cygwin, Windows XP, the insert-mode map <F4> executes the intended key strokes at one time but inserts itself at another time. This undeterministic behavior especially occurs if I hold the <F4> key depressed.

How do I make the behavior predictable accross multiple Vim-, terminal- and operating system versions?

This is the code I defined in my ~/.vimrc file, that seems relevant to me:

set compatible
set timeout
set ttimeout
set timeoutlen=1000
set ttimeoutlen=100
imap <F4> <C-\><C-O>:set relativenumber! relativenumber?<CR>

If I assign the mapping to a function key above <F4>, e.g. <F5>, <S-F7> etc. I don't experience the behavior mentioned above. Also if I disable the timeout option with set notimeout it does seem to do the trick.

share|improve this question

I made the <F4> key apparently work in all modes by using map <expr> expression maps as follows:

MapOptToggle <F4> relativenumber
MapOptToggle! <S-F4> number

command! -bang -nargs=+ MapOptToggle call <SID>MapOptToggle(<bang>0, <f-args>)
function! s:MapOptToggle(bang, key, opt)
  function! s:ToggleOpt(opt)
    if mode() =~# '\vno?' || !&showmode
      exec 'set ' . a:opt . '! ' . a:opt . '?'
      exec 'set ' . a:opt . '!'
    return ''
  let a='noremap'
  let b='<unique> <silent> <expr> ' . a:key . ' <SID>ToggleOpt(' . string(a:opt) . ')'
  exec a . ' ' . b
  exec a . '! ' .  b
  exec 'set ' . (a:bang ? 'no' : '') . a:opt

But still my answer doesn't explain the unpredictable behavior of the imap insert-mode map.

share|improve this answer

That comment

especially occurs if I hold the <F4> key depressed.

hints that the problem is due to key-repeat (increasing repetition speed as you hold the key down) interfering with the timeout used to determine if an escape character is the beginning of a a function key, or the vi special character escape. When vi (or vim) gets too many characters too fast, it sees the beginning of a function key as an escape, and (for instance) aborts the current insertion.

Some programs (such as xterm) will send vt100-style PF1-PF4 for F1 through F4. The resulting escapeOS is a vi command that puts you into insert-mode, allowing it to insert parts of the escape sequences that it reads.

Further reading:

share|improve this answer

Your Answer


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.