Table of Contents

Diff-mode

A short recipe for using the vim diff-mode to compare files for their differences.

Getting help

:h diff

Starting the diff-mode

To use the diff-mode of vim, start vim on the file to be compared:

user@host:~$ vimdiff diff_test_1.txt diff_test_2.txt

or

user@host:~$ vim -d diff_test_1.txt diff_test_2.txt

On an already running vim:

user@host:~$ vim diff_test_1.txt

Activate the diff-mode in the current window:

:diffthis

Split the window vertically and open the second file diff_test_2.txt in the new window:

:vnew diff_test_2.txt

Activate the diff-mode in the new window:

:diffthis

Leaving the diff-mode

Deactivate the diff-mode in the current window:

:diffoff

Forcing the diff-mode to update

Force the diff-mode to update:

:diffupdate

Move forward to the next change:

]C

Move backward to the previous change:

[C

Copying changes in the diff-mode

Copy the differences from another buffer to the current buffer:

:diffget

Copy the differences from the current buffer to another buffer:

:diffput

Use diff-mode to show the changes to a buffer since the last save

A file is opened in vim already and unsaved changes have been made.

Activate the diff-mode in the current window containing the edited file:

:diffthis

Split the window vertically:

:vnew

Load the last saved content of the file in the new window:

:0r#

Delete the last empty line:

:$d

Activate the diff-mode in the new window:

:diffthis

All of the above steps in one single command:

:diffthis | vnew | 0r# | $d | diffthis