Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim function to get all mappings

Tags:

vim

Is there a function in Vim to get the list of all current mappings as a dictionary? (I know about :map command.)

If no (it seems so), is there a decent workaround?

Here is the practical motivation for this question: i would like to be able to clear all key mappings except those that start with <Plug>.

like image 413
Alexey Avatar asked Feb 04 '26 15:02

Alexey


1 Answers

Nothing I could see either. You could use :redir to get the map output into a register and manipulate that. This snippet of vim script will do the job:

redir @a
silent map
redir END
let l = split(@a, '\n')

Now l contains a list of mappings, though parsing each entry might be a pain.

To filter these for those not containing <Plug> add this line:

call filter(l, 'v:val !~ "<Plug>"')

An example of a script to unmap non-<Plug> entries could be:

redir @a
silent map
redir end
let l = split(@a, '\n')
call filter(l, 'v:val !~ "<Plug>"')
for line in l
    let type = line[0]
    let thekey = split(line[1:len(line)], ' \+')[0]
    try
        exec type. 'unmap '. thekey
    catch /E31/
    endtry
endfor
like image 143
richq Avatar answered Feb 06 '26 04:02

richq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!