How to Compare Content of Two Files in Linux With vimdiff Tool

For comparing the contents of two (or more) files in Linux I’m using one tool named vimdiff. To get this useful tool you need to install vim-enhanced package, but if you already have vim installed on your system you already have this package.

  • To install it on CentOS run

Or just

After successful installation, you can use this tool.

To compare two files with vimdiff, you can run simple vimdiff and put files which you want to compare. In this case, we will compare two files but you can use a tool to compare up to 4 different files.

vimdiff file1.txt file2.txt

Here are views of two files compared using this method. For testing, I’ve just modified one line from file2.txt.

As you can see, differ text is (Linuxgain diff here) and it’s marked red. If you want to modify some of these two files, you can jump between these two screens with CTRL+w+w (CONTROL button and press w twice).

When you want to exit from this window(s) you can do that with simple vim commands: q! for the first window and again:q! for a second….or if you want to exit from both windows at the same time you can use:qa!

More about vim commands you can find in man pages of vim (just run: man vim)

Leave a Reply

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