25

Using :map provides a list of all mappings in Vim. But, I am unable to search through the list. I am surprised to see it being opened in a different type of window unlike usual Vim help files. Is there a way to have it available in the usual form?

Jikku Jose
  • 18,306
  • 11
  • 41
  • 61

4 Answers4

22

Vim uses its internal pager to display the output of :map, which has pretty limited functionalities (see :h pager for more info).

If you want to access the output of :map in a normal vim buffer, you could use :redir :

:redir @a>    " redirect output to register a
:map
:redir END
:put a        " paste the output of :map in the current buffer

Note that you can redirect to a file, to a variable, etc... See :h redir for more details.

Marth
  • 23,920
  • 3
  • 60
  • 72
16

Don't be surprised. :map is not related at all with :help so there's no reason whatsoever to expect it to work like :help.

You can give :map an argument to narrow-down the listing:

:map ,

With the right values for wildmenu and/or wildmode, you can tab-complete :map:

:map ,<Tab>

You can also list the current mappings with <C-d>:

:map <C-d>

You can also use the mode-specific variants of :map to get a more manageable list:

:imap ,
:nmap ,
:xmap ,
and so on…

But keep in mind that :map only lists custom mappings (made by you or your plugins). If you want a list of default mappings, check out :help index.

--- EDIT ---

So, a recent upvote reminded me of this old answer and it turns out that a new command, :help :filter has been added in the mean time to help with that kind of use case.

You would use it like this to only list your insert mode mapping that use :help pumvisible():

:filter pumvisible imap

Not technically a search, mind you, but a pretty useful addition nonetheless.

romainl
  • 186,200
  • 21
  • 280
  • 313
7

Here's a robust function to create a searchable vertical split with the sorted output of :maps

function! s:ShowMaps()
  let old_reg = getreg("a")          " save the current content of register a
  let old_reg_type = getregtype("a") " save the type of the register as well
try
  redir @a                           " redirect output to register a
  " Get the list of all key mappings silently, satisfy "Press ENTER to continue"
  silent map | call feedkeys("\<CR>")    
  redir END                          " end output redirection
  vnew                               " new buffer in vertical window
  put a                              " put content of register
  " Sort on 4th character column which is the key(s)
  %!sort -k1.4,1.4
finally                              " Execute even if exception is raised
  call setreg("a", old_reg, old_reg_type) " restore register a
endtry
endfunction
com! ShowMaps call s:ShowMaps()      " Enable :ShowMaps to call the function

nnoremap \m :ShowMaps<CR>            " Map keys to call the function

The last line maps the two keys \m to call the function, change this as you wish.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • But the sort isn't working so well... they're all jumbled up; maybe because of OS X differences? I changed to `sort --key=1,14`. – JESii Jun 05 '17 at 12:52
  • Does one need the assignment to the `@a` at all? Why cannot one just `put` the value of `silent map | ...` into the new buffer? – gented Feb 23 '21 at 16:06
  • Thank you! I am getting the 'No write since last change (add ! to override)' warning. Is there a way to avoid this by opening as a read only buffer or something? – HowlingFantods Mar 18 '21 at 21:07
6

For neovim, telescope has mapping list and search capability out of the box: builtin.keymaps.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 2
    I imagine this answer could be improved by giving some more context for those who don't know what telescope is. I'd never used it but now I have a starting point. For others: I have telescope installed via packer (described in the link you shared) then ran :Telescope, typed keymaps, hit enter, and then was able to find the keymaps I was interested in by typing a bit of what I expected to find. Here's a sample for anyone else struggling, if it helps: https://asciinema.org/a/g9YF0I3tw5nyn4U0l2NIiqRTR I don't understand how to search, obviously, but docs can get me the rest of the way. – James Andariese Feb 27 '22 at 18:13