Go back to the main page

Compare and merge directories with DirDiff vim plugin

This article is over 2 years old. Proceed with caution.

Regards ♨ – Minimul

 

Grab DirDiff and put in your ~/.vim/plugin directory

Edit .vimrc like so

"# DirDiff settings
"# Don't compare directories or filenames that match conditions like CVS,*.swp
let g:DirDiffExcludes = "system,CVS,*.class,*.exe,.*.swp" 
"# Ignore lines that Id:,Revision: etc.
let g:DirDiffIgnore = "Id:,Revision:,Date:"
"# Don't flag files as different based on whitespace
let g:DirDiffAddArgs = "-w"
"# au FilterWritePre * if &diff is to properly catch the transition to diff mode
"# When in diff mode map space bar to go down to next diff and center the page (z.). Shift-space to go up a diff
au FilterWritePre * if &diff | exe 'noremap <space> ]cz.' | exe 'noremap <S-space> [cz.' | endif
"# When in diff mode convenience map diffput and diffget
au FilterWritePre * if &diff | exe 'noremap <leader>g :diffget<CR>' | exe 'noremap <leader>p :diffput<CR>' | endif
"# When in diff mode convenience map undoing a diffput. Don't go without this one as it is a 3 step process.
au FilterWritePre * if &diff | exe 'nmap <leader>u :wincmd l<CR>:normal u<CR>:wincmd h<CR>' | endif
"# When in diff mode suppress annoying auto-folding (filler,context:1000) and don't check for white space differences
au FilterWritePre * if &diff | exe 'set diffopt=filler,context:1000,iwhite' | exe 'execute "normal \<c-w>\<c-w>"' | endif
"# Properly ignoring case and white space differences needs
"# to be done with the MyDiff function -- explanation can be found with 
"# a quick search
set diffexpr=MyDiff()
function MyDiff()
   let opt = ""
   if &diffopt =~ "icase"
     let opt = opt . "-i "
   endif
   if &diffopt =~ "iwhite"
     let opt = opt . "--ignore-all-space "
   endif
   silent execute "!diff -a --binary " . opt . v:fname_in . " " . v:fname_new .
    \  " > " . v:fname_out
endfunction
"# End DirDiff settings

Launch a directory diff like so:DirDiff ~/www/dev-branch/ ~/www/prod-branch/

From here you move around the split windows to do your tasks. If you want to completely sync a file you may use s when in the DirDiff view. You will then be presented with 6 options on how and in what direction you want to sync. See all the options at the plugin page.

Comment on this article?