Say I am in a directory with the filetree below, where gold.txt
is a file I would like to compare each of the other .txt files against, one at a time.
.
├── gold.txt
└── to-compare
├── compare1.txt
├── compare2.txt
├── compare3.txt
├── ...
...
Currently my idea of the workflow would be:
- I open (n)vim in diff mode with
nvim -d to-compare/compare1.txt gold.txt
, and open up a netrw split with:Lexplore
, so I can easily swap out other files to compare to gold.txt. - I see the difference between compare1 and gold as in the image below. Nice.
- I switch to compare2.txt in the middle window by selecting it in netrw and pressing enter
- Now, to see the difference between compare2 and gold, I need to do
diffoff!
and thendiffthis
on both of the windows. - To compare compare3 with gold, I have to do this again.
I'd really like to just be able to quickly tap through the list of files in to-compare/
, quickly seeing how & where each one differs with gold.txt
, without having to type diffoff and diffthis a bunch of times.
That is, I'd like to put my cursor in the leftmost split in the image below, and whenever I select the next file I'd like to compare (or by selecting it and typing enter, or p for preview or whatever), I'd like see the diff with the other file that's open.
I'm sure there are many options for how to do this. I'm not wed to netrw or anything else about this workflow. Is there an easy way to do what I want to do?
:h mapping
)? You can trigger multiple commands with a keystroke or two.:noremap <leader>d :diffoff!<cr> :diffthis<cr> <c-w>w :diffthis<cr> <c-w>W
and then type<leader>d
to diff the current window with the next window. I felt there would be an easier way, but that's not so hard, is it.nnoremap
if it's a key map to be pressed in Normal mode. 2) You can separate commands with\|
or<bar>
instead of<cr>
. The bar (aka pipe) character|
is Vim's command separator. In mappings you must escape it, though. 3) To use Normal mode commands in a mapping mixed with Ex commands use:normal[!]
. Alternatively, almost all Normal mode ctrl-w commands have Ex equivalents. See:h :wincmd
.:nnoremap <leader>d <Cmd>diffoff! \| diffthis \| wincmd w \| diffthis \| wincmd W<cr>
to diff the current window with whatever's in the next window, as a reusable command I can save.