98

I use vim as my primary editor.

I would also like to use vim to diff files and modify the files while doing the diff to fix easy changes (rather than most diff techniques which are a cycle of diff/fix/diff/fix).

Additionally are there easy ways to integrate vim into diff tools used by source control tools/sites (I am specifically thinking of git but I am sure other people would be interested in integration with other source control tools/sites).

  • 1
    you should try vim-fugitive it adds the commands :Gdiff and :Gvdiff both commands lets you see the side by side diff of the current buffer if your current buffer is managed by git. It also heps you to resolve conflicts in a three window layout when you have merge conflicts on some files – rbernabe Mar 23 '17 at 4:31
  • I have this simple function in my bashrc vd () { diff $@ > /dev/null ; if [[ $? -eq 1 ]] ; then ; vimdiff -c 'windo set syntax=off' $@ ; fi ; } and I invoke it with vd file1 file2. It uses diff to determine whether the files differ and only opens vimdiff if that is so. Otherwise, I stay in the shell. I also disable syntax highlighting in Vim because I find it distracting when diffing. Only works with two files. – Rolf Oct 6 '17 at 6:10
119

vim has this functionality built in (with the correct command line flag).

vim -d <file1> <file2>

This opens each file in a view and highlights the differences.
Any code that is identical is folded away so you do not need to look at identical code or scroll through huge chunks of identical code.

But there is also a wrapper application vimdiff that correctly invokes vim with the correct flags.

vimdiff source1.cpp source2.cpp

If you are using git you can set up an external diff tool. So it is easy to set up vimdiff to be the diff tool for git.

git config --global diff.tool vimdiff

When using vimdiff you can edit either side and diff highlighting keeps pace to show you the differences.

Note: When editing from a git diff. If you try and edit the repository stored version of the file your changes will be discarded when you exit (git does not trust you with the original so you are diffing against a tmp copy) but you can edit the local copy to your hearts content and save it over you current version.

Some basic commands that are useful in vimdiff

dp             diffput: puts changes under the cursor into the other file
                        making them identical (thus removing the diff).
do             diffget: (o => obtain). The change under the cursor is replaced
                        by the content of the other file making them identical.


]c             Jump to the next diff
[c             Jump to the previous diff

Other vim settings I use to work with highliting with vimdiff

if &diff
    highlight! link DiffText MatchParen
endif

This turns off highlighting on the bits of code that are changed. So the line that is changed is highlighted so I can spot the changes, but the actual text that has changed stands out on the line (as it is not highlighted).

  • Also handles up to 4 buffers, making it great for comparing configuration files – Izkata Feb 8 '15 at 23:27
  • 34
    You can also use the :diffthis command to initiate a diff when Vim is already running. – Rich Feb 10 '15 at 11:15
  • 11
    And :diffoff to turn it off. I think a link to vim's documentation would be beneficial: vimdoc.sourceforge.net/htmldoc/diff.html – Cody Poll Feb 12 '15 at 4:49
  • 1
    For me even after git config --global diff.tool vimdiff, git diff still shows everything like if I change nothing. – Hi-Angel Sep 1 '16 at 15:17
  • 2
    try git difftool – Martin York Sep 1 '16 at 16:25
12

If you are editing an open file and want to compare it to another file without closing the current one:

Open the new file in split screen:

For vertical split:

:vs otherFile

or horizontal split:

:split otherFile

Switch cursors to different split screen:

ctrl+w ctrl+w

Invoke "diff mode" in file:

:diffthis

Switch to other file and invoke "diff mode":

:diffthis

To turn off "diff mode":

:diffoff
  • 4
    To avoid switching between the buffers you can use :windo diffthis too – statox Oct 2 '18 at 14:50
3

You can place below mentioned setting in .gitconfig file found in %homepath% (or %userprofile%) directory of the currently logged in user:

[diff]
    tool = vimdiff

This will enable git bash tool to start using vimdiff as the external diff tool as well.

2

Following is my git config:

https://github.com/tracyone/dotfiles/blob/master/.gitconfig

[core]
editor = vim
[diff]
tool = vimdiff
[merge]
tool = vimdiff
conflictstyle = diff3
[difftool]
prompt = 0
1

I can see only three situations to use vim as a difftool. They are briefly described below:

  • For git difftool, put the following in your ~/.gitconfig:

    [core]
    editor = vim
    [diff]
    tool = vimdiff
    [merge]
    tool = vimdiff
    conflictstyle = diff3
    
  • To open vim as a diff-tool for two file, you can do the following:

    vimdiff file1.ext file2.ext      # for vim
    nvim -d file1.ext file2.ext      # for neovim
    
  • To get diff view for the buffers that are currently active, i.e. all the buffers that have a window assigned to them in the currently active tabpage, you can do the following:

    :windo diffthis                " to get diff view
    :windo diffoff                 " to remove diff view
    

For more information, see :h diff

0

here's what I do:

  • open a window with first file (or text contents if you're pasting data)
  • open next file/window using :vnew (to have both windows side by side) or :new (to have the windows top and bottom). if you have a specific file to open in the second window, you can include the path like this: :vnew /path/to/secondfile.txt
  • use F8 to toggle a custom function which turns diff mode on and off

here's the custom function that's in my ~/.vimrc:

nmap <silent> <F8> :call ToggleDiff()<CR>
imap <silent> <F8> <C-O>:call ToggleDiff()<CR>
function ToggleDiff ()
    if (&diff)
        set nodiff noscrollbind
    else
        " enable diff options in both windows; balance the sizes, too
        wincmd =
        set diff scrollbind nowrap number
        wincmd w
        set diff scrollbind nowrap number
        wincmd w
    endif
endfunction
  • you can use the command :diffthis and diffoff! so you don't have to set all diff options yourself (e.g. cursorbind is also set by diffmode) – Christian Brabandt Apr 17 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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