Autocommands support the ++once
modifier. I am trying to achieve the same effect for a map.
Instead of coming up with a general solution I have been trying to solve my particular case via a side-effect – I thought it would be easier...
In this case it's a buffer-local map. The map, in window A, closes window B. To remove the map once it has executed, I have a BufLeave
autocmd in window B to remove the map from window A. However I cannot get the autocmd to find the map.
Here's how I set things up in window A:
nnoremap <buffer> <Esc> :<C-U>close_other_window()<CR>
let bufnr = bufnr()
execute "autocmd BufLeave <buffer=".bufnr_of_other_window."> nunmap <buffer=".bufnr."> <Esc>"
The problem is the nunmap
cannot find the map. There's no error but the map remains in place. If I use the following instead, it still cannot find the map (perhaps because <buffer>
is window B's buffer when the autocmd executes):
execute "autocmd BufLeave <buffer=".bufnr_of_other_window."> nunmap <buffer> <Esc>"
Any tips for this situation or the general case would be appeciated!
close_other_window()
so that it closes the window and delete the mapping: This way you wouldn't have to worry if the mapping is local to another buffer since you will run the command from the same buffer the mapping is defined for.close_other_window()
as it may have relevance.nnoremap <buffer> <Esc> :blah<Bar>nunmap <buffer> <Esc><CR>
but without success.<Esc>
with e.g.a
the mapping would work. The issue is that your string<Esc>
is probably interpreted as an escape character and not the literal string<Esc>
(check the output of:map <esc>
to be sure). To fix that you'll probably have to mess with escaping, maybe usingexecute
with simple quotes to run thennoremap
command would help but I'm not sure.