Understanding vimdiff for identifying difference between files and integrating changes from one file to another

vimdiff is very good command which we can use to identify what the differences between two files. Lets say in some scneratios we need to check the difference between two versions of the same file existing in two difference version source codes, in this vimdiff comes very handy to identify what are the lines / code blocks have been changed between two versions.

Another best use of vimdiff will be to integrate changes from one file to another. Lets say, we have some additional function into version 1.5 of a filename.c and now we want to backport this to version 1.3 of same file. With vimdiff we will be able to see that v1.5 has this additional function and v1.3 doesn’t have.

Now, lets write two versions of same program as below,

 $ vim filename1.c 
#include <stdio.h>

int main(void) {
        printf("Hello World : First program\n");
        return 0;
 $ vim filename2.c 
#include <stdio.h>

int add (int a, int b) {
        return a+b;

int main(void) {
        printf("Hello World : second program\n");
        printf("addition = %d\n", add(10,4));
        return 0;

Now, lets take a diff between these two programs as,

 $ vimdiff filename1.c filename2.c 

In Above image, we can see the clear difference between the two codes, now lets try to understand the color codes,
– lines with background color same as terminal color indicates those lines doesn’t have any difference between two files.
– lines with blue background indicates newly added lines compared to another file.
– text with red foreground color indicates exact changed text between two files

Now, lets try to do integration of code from one file into another.

 $ vimdiff filename1.c filename2.c 

Here we will see the two files opened on the terminal. Now, if we want to copy complete function “add” from filename2.c to filename1.c ( like backporting from new version to old version ), follow below steps,

READ  Preparing ubuntu for gstreamer development

By default vimdiff will have cursor at the first character in first file, we need to go to next file as,
– press and hold ctrl key, and hit “w” key twice i.e. ctrl w w
and you will see the cursor moved to second file filename.c at relevant place.

Now use, arrow key to go to first column of add function line, and as we see our add function has 3 lines from open bracket to close bracket so we need to copy this 3 lines which can be done as,
– 3 yy i.e type the number of lines you want to copy followed by hitting key y twice

and it will show “3 lines yanked” at the bottom of screen.

Now we need to switch to first file, by same method as “ctrl yy” and go to line where we want to paste and hit key “p” and you will see all 3 lines copied from second file to first file and vimdiff may disappear the function “add” from both files because now there will not be a difference between these two files related to add function.

Now use, “:wq!” to save and quit both files and verify that every thing is goos 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *