My idea is that whenever &diff
is true for a buffer, I would like some mappings to be active when that buffer is the current buffer.
The following attempt works for when I open a session in diff mode via vimdiff
, and almost works when I do diffthis
from a non-diff
session; I say almost because running :diffthis
does not set the mappings because the BufEnter
event is not triggered, so I have to jump away from that buffer and back to it to see the mappings activated.
How can I have the autocmd
be executed even when :diffthis
is executed and, in general, everytime &diff
is/becomes true in the window where the cursor is?
function! Toggle_diffopts(which)
exe 'set diffopt'
\ . ['+','-'][!!(index(split(&diffopt,','), a:which) + 1)]
\ . '='
\ . a:which
endfunction
function! SetDiffMappings()
if &diff
nnoremap <buffer> <silent> <Leader>diw :call Toggle_diffopts('iwhite')<CR>
nnoremap <buffer> <silent> <Leader>dic :call Toggle_diffopts('icase')<CR>
let b:diff_mappings = v:true
else
if exists('b:diff_mappings') && b:diff_mappings
nunmap <buffer> <Leader>diw
nunmap <buffer> <Leader>dic
let b:diff_mappings = v:false
endif
endif
endfunction
augroup DiffMappings
autocmd!
autocmd BufEnter * :call SetDiffMappings()
augroup end
This does not answer my question.
autocmd OptionSet diff call SetDiffMappings()
work? It's supposed to trigger every time&diff
is modified, so I guess it fits the bill? It might do by itself, or you might need to combine it withBufEnter
or similar for when you already start a buffer in diff mode (or callvimdiff
in the command line) in case it doesn't trigger in that specific case... I hope that helps!OptionSet
event. However I'll check tomorrow, as now is time to sleep for me :POptionSet
is not triggered when moving between windows, so I can't rely just on that, but adding the command you suggest beside the one I have already seems to work. Feel free to add an answer and I'll accept it.