Table of Contents

diff

To perform a 'diff' is to view the differences between two files. Vim will automatically show you the different areas by highlighting the lines. You can easily copy those portions between the files if desired.

diff from command line

diff's are so common that vim comes with a command just for them. To start vim in diff mode with two files, use the vimdiff command:

vimdiff <file1> <file2>

diff after vim is started

If vim is already running, open the first file. Type the following (command mode):

:diffsplit file2

diff preferences

You can adjust your .vimrc file to customize your diff usage. A common viewing preference is to make diff's show up vertically, instead of horizontally.

" make vertical the default diff split (filler was already there)
set diffopt=filler,vertical

Winmerge is a popular Windows-only application, dedicated to managing diffs. You can simulate the behavior of the Winmerge keys with the following:

" disable cmd opt mappings for MacVim (interferes with winmerge mappings)
let macvim_skip_cmd_opt_movement = 1

" use diff navigation like winmerge, using alt with up, down, left, right
nnoremap <M-Down> ]c
nnoremap <M-Up> [c
function! CopyToLeft() 
	if !&diff
		return
	endif

	let currwin = winnr()
	if( currwin == 1 )
		:diffget
	else
		:diffput
	endif
endfunction
function! CopyToRight()
	if !&diff
		return
	endif

	let currwin = winnr()
	if( currwin == 1 )
		:diffput
	else
		:diffget
	endif
endfunction
nnoremap <M-Right> :call CopyToRight()<CR>
nnoremap <M-Left> :call CopyToLeft()<CR>

diffs on directories (multiple files)

Vim by default only handles diffs between files. If you need to analyze entire directories, try using the DirDiff plugin for vim.