vim

Vim Diff Two Files

Vim is an open-source, free, screen-based, highly configurable text editor program for UNIX. Although it has a graphical interface, it can run in a terminal as it is a small program that is why it is powerful and very fast. It runs on commands, so it is easily manageable without menus or a mouse with a keyboard. It is designed for creating and modifying all kinds of text efficiently. Vim is a very powerful code editor for UNIX, Linux, and Mac has it installed by default. Moreover, it can be easily installed on windows as well. It was introduced around 1991, making it the oldest text editor, and it is still around due to its extremely dynamic and versatile functionalities.

Vim text editor is known as a programmer’s editor, and developers still use it because it is highly configurable and enables efficient text editing. It is rock solid, so it is continuously being developed to become much better. It can easily be integrated with many tools, has powerful search and replace capability, and has an extensive plugin system, multiple undo trees, and persistent support for various file formats and programming languages.

Vim diff mode

The diff mode of vim is a custom function that allows us to easily compare the content of two or more files. It is the most common command used for comparing two files. If you want to compare two files containing similar content and decipher the difference, the ‘vim diff’ command is for you. ‘diff’ is short of ‘difference’. It is similar to the UNIX diff command; however, the Vim shows diff much better. Vim diff command is used to compare the two files that contain similar data to find out the common data in both files. Vim diff command simply finds out the same data present in the file and highlights the different data so that it is easily recognizable. It can compare single, multiple, or the contents of directories.

It is really helpful to find out the text that needs to be changed so that the content in both files matches exactly. This guide focuses on providing you the usage of the ‘diff’ command and making you learn how to use the vim diff command, how to activate or deactivate the vim diff mode, how to navigate in the vim diff mode, and finally, how to apply changes from the vim diff window.

How to Use the Vim Diff Command?

The ‘diff’ command aims to compare two files and output the list of differences between both files. In other words, it outputs the list of modifications that need to be done so that both files match exactly. Therefore, the ‘diff’ command is extremely useful for the developers as they can use it to get the differences between two source code files to develop patches.

The syntax of the vim diff command is as follow:

Start by creating two files that will contain similar content. Let us name the first file ‘diff_test_1’ and the second file ‘diff_test_2’. Open both the files, write some content in them with a slight difference, save and close the files. Now, execute the below command in vim editor to find differences between files.

Text Description automatically generated

The output of the above command will only display the lines that do not match. Furthermore, these lines will be displayed in the order of the files provided in the command, i.e., the line in the ‘diff_test_1.txt’ will be displayed first, and the lines in the ‘diff_test_2.txt’ will be displayed next.

It becomes very easy with vim diff to see the similarities and differences in two files side by side; you can easily push or pull the content from one pane to another and modify the files with ease. Thus, Vim diff is a great way to manage projects that require comparing the content of different files. Follow the below steps to activate or deactivate the vim diff mode:

  1. Open the first file ‘diff_test_1.text’ in vim.
  2. Type: split or: diffsplit or use command Ctrl+W v to get the two separate panes side by side.
  3. Switch to the second pane and open the second file, ‘diff_test_2.txt’.
  4. Use command ‘Ctrl+W h or l’ to move between two panes

How to Activate and Deactivate the Vim Diff Mode?

There are two ways to split the vim pane; horizontal and vertical.

Horizontal split:

To split the vim editor panes horizontally, use the following command:

Text Description automatically generated

Vertical split:

To split the vim editor panes vertically, use the following command:

Text Description automatically generated

Furthermore, there are two ways to deactivate the vim diff mode.

# :diffoff

To leave the vim diff window, type the following command

Shape Description automatically generated with medium confidence

This command will close the file, and you are out of the vim diff mode. The second way to get out of the vim diff mode is: windo diffo or :diffo!

# :windo diffo or :diffo!

The :windo diffo or simply :diffo! closes the vim diff mode easily. Use the following command to deactivate the vim diff mode

Shape Description automatically generated with medium confidence

These commands will work for closing both horizontal and vertical splits.

How to Navigate in the Vim Diff Mode?

Navigating between two files using vim editor is slightly different from usual. For example, if you scroll up or down in one window, the text in the adjacent window will also scroll up or down. This is known as scrollbind.

To enable the scrollbind functionality, use the following command:

Shape Description automatically generated with medium confidence

To edit the files in scrollbind mode, use the following command.

Shape Description automatically generated with medium confidence

To switch between two windows of the vim editor, use ctrl+w; however, keep in mind that you need to press Ctrl+W twice to navigate between two window panes.

To jump back to the previously executed change, use the following command:

Shape Description automatically generated with medium confidence

To jump to the next change, use the following command:

Shape Description automatically generated with medium confidence

To disable the scrollbind mode, use the following command:

Shape Description automatically generated with medium confidence

How to Apply Changes from the Vim Diff Command Window?

When you compare two files, you must want to modify and edit the files and save those changes. To apply the changes made in two compared files, use the following commands:

The above command will apply the changes made in one file to another. For instance, you have made changes in the left window file, and you want to copy those changes to the file in the right-side window, you will execute the command given above in the right side window, and all the changes in the left window pane will copy to the right window pane.

Shape Description automatically generated with medium confidence

To apply changes from the current window to the adjacent window, the above-given command will be used. For example, if you work in the left window and want to copy the changes in the right window, you will execute the above command.

Conclusion

In this guide, we have learned the use of the vim diff command and how it can help compare two files simultaneously, modify and edit them side by side, and apply the changes from one file to another file with ease.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.