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;

<!-- /wp:html -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"> $ vim filename2.c </pre>
<!-- /wp:preformatted -->

<!-- wp:html -->

#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,

Related :   How to install Android Studio for 64 bit Ubuntu machines

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 🙂

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question