Compare files with Visual File diff tool of VIM – vimdiff

Comparing files efficiently is crucial for developers, system administrators, and power users who work with configuration files, source code, or logs. VIM is not only a powerful text editor but also includes an inbuilt visual file diff tool that allows side-by-side comparisons. The VIM diff mode enables users to detect changes, highlight differences, and merge files seamlessly without the need for external diff tools. This guide will walk through how to compare files with the Visual File Diff tool of VIM ( vimdiff) , ensuring an optimized workflow for text and code analysis.

What is the Visual File Diff Tool in VIM?

🔹 VIM’s diff mode is a built-in feature that highlights the differences between two or more files visually.
🔹 It allows side-by-side comparisons, marking added, removed, and modified lines efficiently.
🔹 The tool is particularly useful for comparing source code, tracking changes in configuration files, and debugging discrepancies in logs.
🔹 Unlike external GUI-based diff tools, VIM’s visual file diff tool is lightweight, fast, and works directly within the terminal.

By enabling diff mode in VIM, users gain a powerful and customizable comparison tool for handling text-based files effectively.

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

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 🙂

Mastering VIM’s visual file diff tool allows developers to compare, edit, and merge files effortlessly within the terminal. Whether handling source code changes, debugging logs, or resolving configuration discrepancies, VIM’s diff mode provides a powerful and efficient solution. By leveraging split views, navigation shortcuts, and merging commands, users can streamline file comparison tasks and improve workflow efficiency.

Start using VIM’s file diff tool today to simplify text and code comparison like a pro!

Leave a Comment