vim-dirdiff
vim-dirdiff copied to clipboard
Suggestion: enable mappings only while inside dirdiff mode
I map <A-j>
and <A-k>
to :cprevious
and :cnext
to navigate the "quickfix" list when opened.
I would also like to use these same keys to go to the previous and next file while in dirdiff mode (by setting variables g:DirDiffNextKeyMap
and g:DirDiffPrevKeyMap
). Unfortunately this does not work because the original ("quickfix") mappings are lost when I do this.
I see two workarounds for this:
- Modify the dirdiff plugin to add a new API function that we can query to check whether dirdiff mode is currently enabled or not and then externally set the mapping for
<A-j>
and<A-k>
to execute either:cprevious
/:cnext
or:DirDiffPrev
/:DirDiffNext
. - Modify the dirdiff plugin to save and restore the previous mappings when entering/exiting dirdiff mode.
Any thoughts on this?
Thanks!
Can you try using &diff to detect if you are in a diff mode and map something differently?
Good idea. I tried it but there are two small problem with this approach:
- The window at the bottom (the one listing all files) does not have "&diff" set (so I cannot use the shortcuts from there)
- &diff is set everytime there is a diff, even if we are not in dirdiff
But your idea gave me another one: it would be nice to have another buffer specific variable which is only set in those buffers opened by dirdiff. Something like this:
--- dirdiff.vim.ori 2022-05-07 22:10:41.828692192 +0200
+++ dirdiff.vim 2022-05-07 22:47:02.066283682 +0200
@@ -234,6 +234,7 @@
return
endif
silent exe "edit ".s:FilenameDiffWindow
+ let b:dirdiff = 1
echo "Defining [A] and [B] ... "
" We then do a substitution on the directory path
" We need to do substitution of the the LONGER string first, otherwise
@@ -530,17 +531,20 @@
if s:LastMode == 2
call <SID>Drop(previousFileA)
silent exec "edit ".s:FilenameA
+ let b:dirdiff = 1
diffthis
silent exec "bd ".bufnr(previousFileA)
call <SID>Drop(previousFileB)
silent exec "edit ".s:FilenameB
+ let b:dirdiff = 1
diffthis
silent exec "bd ".bufnr(previousFileB)
else
let previousFile = (s:LastMode == "A") ? previousFileA : previousFileB
call <SID>Drop(previousFile)
silent exec "edit ".s:FilenameB
+ let b:dirdiff = 1
silent exec "bd ".bufnr(previousFile)
diffthis
@@ -550,9 +554,11 @@
else
"Open the diff windows
silent exec "split ".s:FilenameB
+ let b:dirdiff = 1
" To ensure that A is on the left and B on the right, splitright must be off
silent exec "leftabove vert diffsplit ".s:FilenameA
+ let b:dirdiff = 1
endif
" Go back to the diff window
Then I can do something like this on my .vimrc:
nnoremap <expr> <A-k> b:dirdiff ? ':silent! DirDiffPrev<CR>' : ':cp!<CR>'
nnoremap <expr> <A-j> b:dirdiff ? ':silent! DirDiffNext<CR>' : ':cn!<CR>'
...and it works :)
Let me know if you like this approach and I will happily create a PR.