Similar to UNIX diff command we can use Vim to show diff in much better manner. It will show diff in colorful manner. In this chapter, we will discuss following items −
To show differences between files execute below command −
$ vimdiff <file> <file> OR $ vim –d <file> <file>
For instance below command shows differences −
In above image, text highlighted in magenta color is common. Remaining text is highlighted in red color which indicates that there are some differences after common text.
If you are already in vim and you want to perform diff action, then use one of the following command −
Use below command to perform horizontal split −
:diffsplit filename
Above images shows that we are comparing two files namely message-1.txt and message-2.txt.
To perform vertical split use following command −
:vert diffsplit <filename>
It opens following window −
Above images shows that we are comparing two files namely message-1.txt and message-2.txt.
Navigation in diff mode is little bit different. For instance, when you scroll text from one window then text from adjacent window also scrolled. This is called scrollbind. To enable this use −
:set scrollbind
To disable this use −
:set noscrollbind
If you edit file in this mode then use following command to update diff −
:diffupdate
To switch between diff windows execute following command −
Ctrl + w Ctrl + w
Please note that, we have to press Ctrl + w 2 times.
In diff window, execute following command to jump to previous change −
[c
In diff window, execute following command to jump to next change −
]c
To apply change from adjacent diff window to current diff windows execute −
:diffget
For instance, if you are in left window and you want to take change from right window to current window then you will execute above command.
To apply change from current diff window to adjacent diff window execute −
:diffput
For instance, if you are in left window and you want apply change from current window to right window then you will execute above command.