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.

When I hit the <s-f2> key to execute my nnoremap <s-f2> :set number! mapping Vim opens its "Insert" mode above (O) and types the 1;2Q string. In order to see the entire terminal key code – not eaten up half-way by the "Normal" mode – I hit <c-v><s-f2> in "Insert" mode and get ^[O1;2Q, where ^[ is the <esc> character.

Even after reading the "Mapping fast keycodes in terminal Vim" I don't understand why the ^[O1;2Q terminal key code is not mapped to the <s-f1> Vim code. Therefore I defined the following function in my ~/.vimrc file:

function! s:Mod_fix_shift_fkey()
  let a=0    
  let b='PQRS'    
  while a < 4    
    exec 'set <s-f' . (a + 1) . ">=\eO1;2" . b[a]    
    let a+=1    
  endwhile    
endfunction

By calling it I fix the shifted function keys from <s-f1> to <s-f4> and the mapping bound to <s-f2> suddenly works.

Can someone explain?

Also I had to fix the shifted function keys from <s-f5> to <s-f12> like:

  "...
  let a=5 
  let b='1517181920212324' 
  let c=0 
  while a < 16 
    exec 'set <s-f' . a . ">=\e[" . b[c : c + 1] . ';2~' 
    let a+=1 
    let c+=2
  endwhile
  "...

And from <c-s-f1> to <c-s-f4> and <c-s-f5> to <c-s-f12> the control-shifted function keys like:

" ...
exec 'map <esc>O1;6' . b[a] ' <c-s-f' . (a + 1) . '>'
" ...
exec 'map <esc>[' . b[c : c + 1] . ';6~ <c-s-f' . a . '>'
" ...
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use a special wildcard syntax with :set <Key> to let Vim automatically recognize xterm-style modified keys:

if &term =~ '^gnome'
execute "set <xUp>=\e[1;*A"
execute "set <xDown>=\e[1;*B"
execute "set <xRight>=\e[1;*C"
execute "set <xLeft>=\e[1;*D"
execute "set <xHome>=\e[1;*H"
execute "set <xEnd>=\e[1;*F"
execute "set <PageUp>=\e[5;*~"
execute "set <PageDown>=\e[6;*~"
execute "set <F1>=\eOP"
execute "set <F2>=\eOQ"
execute "set <F3>=\eOR"
execute "set <F4>=\eOS"
execute "set <xF1>=\eO1;*P"
execute "set <xF2>=\eO1;*Q"
execute "set <xF3>=\eO1;*R"
execute "set <xF4>=\eO1;*S"
execute "set <F5>=\e[15;*~"
execute "set <F6>=\e[17;*~"
execute "set <F7>=\e[18;*~"
execute "set <F8>=\e[19;*~"
execute "set <F9>=\e[20;*~"
execute "set <F10>=\e[21;*~"
execute "set <F11>=\e[23;*~"
execute "set <F12>=\e[24;*~"
endif

See :help xterm-function-keys and :help xterm-modifier-keys.

share|improve this answer
    
Still your answer doesn't explain why the ^[O1;2Q terminal key code isn't mapped to the <s-f1> Vim code in the first place without any further ado. –  Tim Friske Dec 14 '12 at 13:34
    
Such modified keys are only partially specified in terminfo (the database tty-based applications use to determine the sequences supported by the terminal specified by the TERM environment variable). For example, my terminfo entry for gnome has says that ^[O1;2Q is the sequence for the kf14 capability, which is the F14 key. It is common—though not exactly standard—to interpret F14 as Shift-F2; Vim does not appear to pick this up out of terminfo. The lack of standardization is why you need to use the “wildcard” syntax (or your looping technique) to let Vim know which sequences to expect. –  Chris Johnsen Dec 15 '12 at 0:10

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.