Need help with understanding vimscript code snippet

78 views
Skip to first unread message

Bruno Sutic

unread,
Mar 29, 2014, 6:40:52 AM3/29/14
to vim...@googlegroups.com
Hi,
I'm posting this to vim_dev because it is related to plugin development (apologies if it should go to vim_use).

Recently, I discovered Tim Pope's vim-rsi plugin (https://github.com/tpope/vim-rsi).
I was mind-blown to discover you can actually use Meta (alt key) mappings in terminal vim. I tried to set this up before myself, but google searches weren't returning anything useful.

Now I'm trying to hack/tweak vim-rsi, but I'm utterly puzzled by the following snippet:
https://github.com/tpope/vim-rsi/blob/master/plugin/rsi.vim#L49-55

:helpgrep does not return anything on F31
Does anyone know how that works? What are those <Fxx>?

Thanks

Tony Mechelynck

unread,
Mar 29, 2014, 8:55:58 AM3/29/14
to vim...@googlegroups.com
Normally, <F1> to <F12> are the function keys on top of your keyboard.
The Vim help mentions even <F13> to <F19>, see ":help <F13>" etc., but I
haven't seen a keyboard which had them. I suppose that the author of the
snippet had a very special keyboard.

You can always use a different {lhs} in a mapping seen in an example
snippet; just use something which doesn't conflict which what you
already use. For instance, instead of <F31> to <F35> you can use <S-F8>
to <S-F12> (i.e. Shift-F8 to Shift-F12) provided that (a) they aren't
already used by something else, and (b) your terminal sends recognisable
byte sequences for them (see the last paragraph below).

You can even use multikey mappings, but, especially in that case, see
:help 'timeout'
:help 'ttimeout'
:help 'timeoutlen'
:help 'ttimeoutlen'

And, you can use Alt-key mappings in a terminal if the terminal sends a
different byte (or byte sequence) for that key than, for instance, the
same key without the Alt. To know what the keyboard (or the terminal)
sends for a given key, set Vim in Insert mode, then hit Ctrl-V followed
by that key. The result will often be gibberish, but (in Normal mode) g8
or ga over that gibberish will tell you which byte(s) it represents.


Best regards,
Tony.
--
"I'd love to go out with you, but my favorite commercial is on TV."

Bruno Sutic

unread,
Mar 30, 2014, 6:45:25 PM3/30/14
to vim...@googlegroups.com
Thanks for the answer Tony!
You're the only one that ever answers these hard questions :)

Btw. the snippet author is Tim Pope - he has a lot of great vim plugins and certainly knows what he's doing, so I'm curious to find out why he chose F31 to F35 when there seems to be a number of "lower" F-numbers.

LCD 47

unread,
Mar 31, 2014, 3:17:19 AM3/31/14
to vim...@googlegroups.com
On 29 March 2014, Tony Mechelynck <antoine.m...@gmail.com> wrote:
> On 29/03/14 11:40, Bruno Sutic wrote:
> >Hi, I'm posting this to vim_dev because it is related to plugin
> >development (apologies if it should go to vim_use).
> >
> >Recently, I discovered Tim Pope's vim-rsi plugin
> >(https://github.com/tpope/vim-rsi). I was mind-blown to discover you
> >can actually use Meta (alt key) mappings in terminal vim. I tried
> >to set this up before myself, but google searches weren't returning
> >anything useful.
> >
> >Now I'm trying to hack/tweak vim-rsi, but I'm
> >utterly puzzled by the following snippet:
> >https://github.com/tpope/vim-rsi/blob/master/plugin/rsi.vim#L49-55
> >
> >:helpgrep does not return anything on F31
> >Does anyone know how that works? What are those <Fxx>?
[...]
>
> Normally, <F1> to <F12> are the function keys on top of your
> keyboard. The Vim help mentions even <F13> to <F19>, see ":help <F13>"
> etc., but I haven't seen a keyboard which had them. I suppose that the
> author of the snippet had a very special keyboard.
[...]

In the old days it wasn't uncommon for keyboards to have 20 function
keys, labeled F0..F19. On these keyboards Shift-Fn produced distinctive
keycodes, but terminfo / termcap didn't have codes for Shift-Fn. So
people coded Shift-F0..Shift-F19 as F20..F39. Mystery solved. :)

/lcd

John Little

unread,
Mar 31, 2014, 6:04:09 AM3/31/14
to vim...@googlegroups.com
On Sunday, March 30, 2014 1:55:58 AM UTC+13, Tony Mechelynck wrote:
> The Vim help mentions even <F13> to <F19>, see ":help <F13>" etc., but I
> haven't seen a keyboard which had them. I suppose that the author of the
> snippet had a very special keyboard.

Or, maybe, the author was on a system using X and knew about xmodmap. If I

xmodmap -e 'keycode 135 = F30'

then gvim happily sees and can map F30 when I press the otherwise useless menu key. The limit on my system (Kubuntu 13.10) is F35, I've no idea where that comes from but I note tpope's mappings go up to F35. (When I was last on windows I used registry hacks to achieve similar results.)

One can find out the keycodes and keysyms using the xev utility. Note that one's "window manager" or "desktop environment" or whatever has to allow use of the key. My keyboard has silly little mail, home page and search buttons and I can use the mail and home page buttons with gvim but not the search button, as KDE wants to start a browser on Google if I press it.

Regards, John Little

Reply all
Reply to author
Forward
0 new messages