3

How can I show the mappings for all function keys <F1> to <F12> at once? I know that :map <F1> shows the mapping for <F1>, and I can repeat this 12 times for each key. But is there a faster way? I have tried :map <F but that doesn't work.

4

You could do this with a loop:

for i in range(1, 12) | execute 'map <F'.i.'>' | endfor

If you don't want any output for F-keys without mappings, you can use mapcheck(), as suggested by @BLayer:

for i in range(1, 12) | if !empty(mapcheck('<F'.i.'>')) | execute 'map <F'.i.'>' | endif | endfor

For more details, see:

  • :help :for
  • :help :execute
  • :help range()
  • :help mapcheck()
  • 2
    For cleaner output, omitting non-mapped "F" keys, the execute expression could be expanded to 'if mapcheck("<F'.i.'>") != "" | map <F'.i.'> | endif' – B Layer Jun 4 at 1:18
  • Thanks for the clean and simple solution. Initially, I had thought of using regex (and later, with :filter), but it seems that a loop is better suited for this situation. – husB Jun 4 at 8:13
6

The :filter allows to filter the output of certain vim commands. Currently, this is not yet supported by every Vim command, but according to :h map-listing, it should be possible. As the help states:

The :filter command can be used to select what mappings to list.  The
pattern is matched against the {lhs} and {rhs} in the raw form.

This means, you would need to put up a custom filter command with the raw key codes as input to the filter command. I was able to come up with this somewhat convoluted example for all <f> keys:

exe ":filter" join(map(range(1,12), { i,v -> eval('"\<f'.v.'>"')}), '\|') ":map"

This creates a list of each raw <f> key by using the "\<f1>" form (which will then be parsed into the correct raw key code by vim (therefore, the eval() is needed). The resulting list, is then joined together into a regular expression, with each item joined by the \| regex atom (OR).

As the help states, the filter criteria will be applied to both, the left and right side of a mapping, so if the criteria matches the right side, it will be output as well.

Note, in order to really get all mapping output, you might as well need the :map! command to get insert and commandline mappings.

Overall this is a lot more complicated than to use the solution provided by Rich

  • Is it possible to do something such as :filter /raw code of <f3>/ map without using exec? – dedowsdi Jun 3 at 11:09
  • 1
    I don't think so, since one needs the special "\<key>" notation – Christian Brabandt Jun 3 at 13:43
  • I guess you are right. If the raw code sequence starts with 0x80, one have to use the special "\<key>" notation. – dedowsdi Jun 3 at 13:56
  • Thanks, :filter was what I was looking for. I managed to grasp the main gist behind your example but it will take me some time before I fully understand it. Also, can the example be simplified to something along the lines of "\<f".'\d\{1,2\}'.">"? – husB Jun 4 at 4:40
  • @husB no, because as I said, the filter works on the raw key codes. – Christian Brabandt Jun 4 at 5:50

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.