Skip to content

Instantly share code, notes, and snippets.

@mattratleph
Forked from roothybrid7/vimdiff_cheet.md
Last active October 16, 2024 17:26
Show Gist options
  • Save mattratleph/4026987 to your computer and use it in GitHub Desktop.
Save mattratleph/4026987 to your computer and use it in GitHub Desktop.
vimdiff cheat sheet

vimdiff cheat sheet

##git mergetool

In the middle file (future merged file), you can navigate between conflicts with ]c and [c.

Choose which version you want to keep with :diffget //2 or :diffget //3 (the //2 and //3 are unique identifiers for the target/master copy and the merge/branch copy file names).

:diffupdate (to remove leftover spacing issues)
:only (once you’re done reviewing all conflicts, this shows only the middle/merged file)
:wq (save and quit)
git add .
git commit -m “Merge resolved”

If you were trying to do a git pull when you ran into merge conflicts, type git rebase –continue.

##vimdiff commands

]c :        - next difference
[c :        - previous difference
do          - diff obtain
dp          - diff put
zo          - open folded text
zc          - close folded text
:diffupdate - re-scan the files for differences
@573
Copy link

573 commented Jun 26, 2015

C-W C-W lets you toggle between the diff columns

@uwo
Copy link

uwo commented Oct 11, 2015

thanks!

@pankajmodi
Copy link

To avoid whitespace comparison:

:set diffopt+=iwhite

@ejlp12
Copy link

ejlp12 commented Nov 22, 2015

press CTRL+W twice lets you toggle between the diff columns

@plank-sr
Copy link

C-W "direction" for moving between windows. If you're in the left column and want to be in the right: ctrl-w l.
If you're in the right column and want to be in the left: ctrl-w h

@bhch
Copy link

bhch commented Mar 4, 2016

@573 @ejlp12 Actually, the command is C-W W i.e. Ctrl-W W. No need to hit Ctrl twice.

@johncip
Copy link

johncip commented Jun 3, 2016

smart quotes in git commit -m “Merge resolved” :)

@knayan1
Copy link

knayan1 commented Sep 19, 2016

To wrap lines :set wrap
To remove colors: :syn off

@vivamus
Copy link

vivamus commented Sep 22, 2016

zR command unfold ALL lines -- I feel safer to merge when I see complete files

Also, if you merge frequently, you may prefer to use ] and [ instead of ]c and [c
Put following to your .vimrc

   map ] ]c
   map [ [c

To limit this action to diff mode, use following
(highlight colors are modified to fit my particular background, you may ignore hi commands and set cursorline)

if &diff
    set cursorline
    map ] ]c
    map [ [c
    hi DiffAdd    ctermfg=233 ctermbg=LightGreen guifg=#003300 guibg=#DDFFDD gui=none cterm=none
    hi DiffChange ctermbg=white  guibg=#ececec gui=none   cterm=none
    hi DiffText   ctermfg=233  ctermbg=yellow  guifg=#000033 guibg=#DDDDFF gui=none cterm=none
endif

@choikwa
Copy link

choikwa commented Dec 27, 2016

A really desirable command would be having each window focused and allowing ]c [c commands to only jump on diff to original caused by its changes. In short, left column being mine should only traverse changes due to my changes only. Right column should traverse incoming changes (theirs), and bottom row should include both.

That way, I can traverse only my changes and detect if there is conflict.

EDIT: nvm, just searching for <<<<<<<, =======, >>>>>>> for conflict markers also works.

@alekibango
Copy link

C-o previously opened text. especially good on jumping back to list of files when you start by vim directory, pick file and you need to get back to list of files.

@Ablesius
Copy link

If you were trying to do a git pull when you ran into merge conflicts, type git rebase –continue.

Note that this is only the case when you have set pull.rebase = true. If not, you're doing a normal merge, and there's no rebase running you can continue :)

@newgoliath
Copy link

zr open all folds
zm close all folds

@dsaw
Copy link

dsaw commented May 3, 2018

The default identifiers that can be selected using diffget
LO local master copy
RE remote master to be merged
BA common ancestor of remote and local changes.

@markhu
Copy link

markhu commented May 4, 2018

When I do vimdiff on two files, then later open the same two files with vim -o it restores vimdiff mode to my consternation. Still looking for a way out, otherwise I'd post a tip here.

@pieman72
Copy link

@markhu: This was happening to me, too. In my case, it was because my vimrc contained: autocmd BufWrite *[a-zA-Z0-9_.]* mkview, which forces a view to be created on every save (buffer write) and autocmd BufRead *[a-zA-Z0-9_.]* silent! loadview, which tries to load a view on every file open (buffer read). Since vimdiff uses folds and other formatting that gets included in the view, these artifacts appear when opening the file again in vim normally (e.g. vim -o). I resolved this by putting those commands behind a check for vimdiff mode:

if !&diff                                                                          
  autocmd BufWrite *[a-zA-Z0-9_.]* mkview                                          
  autocmd BufRead *[a-zA-Z0-9_.]* silent! loadview                                 
endif

@deniztoprak
Copy link

do (diff obtain): bring changes from the other file to the current file
dp (diff put): send changes from the current file to the other file

@MikaelElkiaer
Copy link

Could anyone tell me how to bring changes from multiple files?
:diffget will only bring from one file, and when doing :diffget multiple times it will overwrite the previous pick.
I often need this when consolidating changes where I need from both local and remote, e.g. for import statements or package references.

@harisund
Copy link

harisund commented Jan 2, 2020

One useful vimdiff related feature I found here

You can influence how many identical lines are kept around changes (default: 6 lines above and below) via the context value of the diffopt option. So, to completely fold all identical lines:

:set diffopt+=context:0

Another useful one from here

windo diffthis
diffoff

If you have files open in splits etc and want to activate vim's diff mode or deactivate

@igorcafe
Copy link

igorcafe commented Dec 1, 2021

I'm really struggling to find it, so...
Is there a vimdiffrc file or something?
I would like to configure the colorscheme and other stuffs

@MikaelElkiaer
Copy link

@igoracmelo AFAIK you just set it in your .vimrc. Here for instance is a color fix in my config:
https://github.com/MikaelElkiaer/dotfiles/blob/74b978d2ba94e93bb6e95e933f54d1d4e07edcbb/.vimrc#L87-L90

Or, alternatively, setting a theme specific for vimdiff:
https://github.com/MikaelElkiaer/dotfiles/blob/9f5544dc252f74ca8b1f46cec27df7d811a91ae8/.vimrc#L43-L45

@CervEdin
Copy link

CervEdin commented May 8, 2022

Another handy command :diffoff/:diffthis allows you to turn the diff of a window off/on. Usefull when comparing base with either remote/local

@kellymatiq
Copy link

Useful alias for .gitconfig

[alias]
  vimdiff = difftool -y -t vimdiff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment