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!