How to search in the Vim mapping listing?

前端 未结 3 1113
失恋的感觉
失恋的感觉 2021-02-07 12:22

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

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 12:58

    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("\")    
      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            " Map keys to call the function
    

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

提交回复
热议问题