0

I'm adding a buffer-local mapping to close netrw windows:

" ~/.vim/ftplugin/netrw.vim
nnoremap <buffer> q <C-W>c

It works, but it hesitates because Netrw has created other mappings with a q prefix, namely qb, qf, qF, and qL, so it waits for the duration of 'timeoutlen' before executing my mapping.

I want my q mapping to be instantaneous, so I've unmapped the other q-sequences:

nunmap <buffer> qb
nunmap <buffer> qf
nunmap <buffer> qF
nunmap <buffer> qL

But rather than hard-code these and have to update them if netrw adds more q mappings, I'd like to programmatically fetch a list of the keys my netrw buffer has mapped to sequences beginning with q and use that to drive the unmappings.

I suppose I could parse the output of :nmap, but that seems like a kludge. Is this information more directly accessible?

UPDATE

The answer provided below worked perfectly. It turns out, however, there's a way I can avoid the need to fetch these keybindings in the first place:

:help map-nowait

I came across this when reading through the dirvish plugin. So rather then perform any nunmaps, I just changed my ftplugin file to:

nnoremap <nowait> <buffer> q <C-W>c

and it works right away!

2

EDIT: My mistake: Neither getcompletion() nor the old trick (see my lh#command#matchging_askvim() function) are able to return buffer-local mappings.

This means: I'm not aware of any way other than parsing execute('nmap q') result. This means you'll may need to decrypt @ and *. From execute() result, I'll simply extract the keybindings. Then, I'll use maparg() to have more precise information.

let keybindings = map(split(execute('nmap q'), '\n'), 'split(v:val, "\\W\\+")[1]')
let mappings =  map(copy(keybindings), 'maparg(v:val, "n", 0, 1)')

NB: in lh-vim-lib, I have other functions able to obtain mapping states and to restore them eventually. See lh#on#exit().restore_mapping_and_clear_now()

  • Nice. In my case I don't actually need the mappings, just the keys they were bound to, but I'm curious, in your let mappings = ... why are you making a copy of keybindings? Btw, I found your lh#encoding#previous_character and lh#encoding#current_character functions very useful recently. Thanks! – ivan Jul 13 '17 at 13:18
  • You're welcome. I'm glad you find my lib useful. It's one of its purposes :) Regarding the copy, it's because I wanted to leave the variable unmodified and simply contain the string version of the key bindings. A one-liner without any copy would have worked. Otherwise, map() will transform these strings into dictionaries. The mapping definitions can be used to restore these definitions once you've finished. It really depends on the scenario. Sometimes we need to restore stuff to its previous definition. – Luc Hermitte Jul 13 '17 at 13:25
  • Ah, I didn't realize map() was destructive like that. Ok cool – ivan Jul 13 '17 at 13:44

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.