How do I use vim as a diff tool?

  • 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).

    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

    I have this simple function in my bashrc ``vd () { diff [email protected] > /dev/null ; if [[ $? -eq 1 ]] ; then ; vimdiff -c 'windo set syntax=off' [email protected] ; 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.

  • 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

    You can also use the `:diffthis` command to initiate a diff when Vim is already running.

    And :diffoff to turn it off. I think a link to vim's documentation would be beneficial: http://vimdoc.sourceforge.net/htmldoc/diff.html

    To use neovim, set `git config --global mergetool.path nvim` (along with `diff.tool` as specified in the answer).

    For me even after `git config --global diff.tool vimdiff`, `git diff` still shows everything like if I change nothing.

    try `git difftool`

    There is also :diffupdate which will refresh the diff view after changes: ```` :diffupdate ````

  • 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
    

    To avoid switching between the buffers you can use `:windo diffthis` too

  • 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.

  • 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
    
  • 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

  • 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)

  • For NeoVim, you can setup your ~/.gitconfig with the following commands

    git config --global merge.tool nvim
    git config --global mergetool.keepBackup false
    git config --global mergetool.nvim.cmd $'nvim -d $LOCAL $REMOTE $MERGED -c \'$wincmd w\' -c \'wincmd J\''
    

    Your ~/.gitconfig should then look like:

    [merge]
        tool = nvim
    
    [mergetool]
        keepBackup = false
    
    [mergetool "nvim"]
        cmd = nvim -d $LOCAL $REMOTE $MERGED -c '$wincmd w' -c 'wincmd J'
    

    Refer to Nvim documentation: diff for more info on how to configure it to your liking.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM