Take diff of two vertical opened windows in Vim

VimDiffVimdiff

Vim Problem Overview


I've have two files opened. They are opened in vertical mode, next to next. Can I instantly diff these two files without leaving or closing Vim ?

Vim Solutions


Solution 1 - Vim

To begin diffing on all visible windows:

:windo diffthis

which executes :diffthis on each window.

To end diff mode:

:diffoff!

(The ! makes diffoff apply to all windows of the current tab - it'd be nice if diffthis had the same feature, but it doesn't.)

Solution 2 - Vim

in each of the windows you want to diff type:

:diffthis

If you want to diff all of the open windows, you can do:

:windo diffthis

(windo will apply the command to all open windows)

Solution 3 - Vim

Following up on the earlier answers,

  • :windo difft (short for diffthis) will start diff mode in all the open windows.
  • :windo diffo (short for diffoff) will stop diff mode in all the open windows.

I have the following mappings in my vimrc to make it easier:

command! Difft windo diffthis
command! Diffo windo diffoff

Solution 4 - Vim

Following on the earlier answers I adapted the mapping provided by @cxw. The following mapping automatically closes NERDTree and diffs the open windows. It does not matter if NERDTree is closed, it works the same way. I do this quite often so it saved me quite some time.

command! Difft NERDTreeClose | windo diffthis

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFatih ArslanView Question on Stackoverflow
Solution 1 - VimNefrubyrView Answer on Stackoverflow
Solution 2 - VimNathan FellmanView Answer on Stackoverflow
Solution 3 - VimcxwView Answer on Stackoverflow
Solution 4 - VimJordi FreixaView Answer on Stackoverflow