18

I know you would normally enter into vimdiff mode from the terminal with vimdiff file1 file2. However, I frequently find myself already in vim with multiple different splits, and I'd like to run vimdiff on two specific splits, and then turn vimdiff off.

How would I go about this?

N.B. there might be other splits in my vim session that I don't want to be comparing. So I might have 4 vertical splits and I want to run vimdiff on split 1 with split 3, look at some differences, and then turn the diff off.

2 Answers 2

20

The command is called :diffthis. It must be executed in every window you want to add to the comparison.

The inverse is :diffoff which exits diff mode in the current window. To exit diff mode in all applicable windows add a bang: :diffoff!.

4
  • 2
    The cod mapping from tpope's unimpaired can be handy.
    – mMontu
    Mar 12, 2020 at 13:07
  • @mMontu It maps exactly to diffthis / diffoff. So it's useful if you're already using vim-unimpaired. Otherwise, it's not a problem to create such mapping yourself.
    – Matt
    Mar 12, 2020 at 14:39
  • @Matt glorious - thank you, this works a treat.
    – Aeroblop
    Mar 12, 2020 at 15:22
  • 4
    To quickly accomplish this in your splits, you can use :windo as in ":windo diffthis".
    – jpheldson
    Mar 12, 2020 at 20:28
2

Noting that two :diffthis commands ought to be issued with the existing two-split view, here's a quick Autohotkey snippet that does the trick - it simply emulates key-input to the Vim window by issuing :diffthis twice.

            send :diffthis
            send {enter}
            send ^w
            send w
            send :diffthis
            send {enter}

Looking forward to a native Vimscript/Lua-based solution.


Correction: :windo diffthis does the trick!

Kudos to Christian Brabandt.

Note, :windo diffoff achieves the same function as :diffoff!.

1
  • 3
    easier: :windo :diffthis if only two splits are opened Aug 4, 2022 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.