Open In App

How to expand/collapse a diff sections in Vimdiff in Linux

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Vimdiff is a powerful text editor Linux users love comparing and merging files. It’s based on another popular editor called Vim. With Vimdiff, you can easily see the differences between two files side by side.

Sometimes, when you’re working with big and complicated files, it can be hard to understand all the changes. That’s where Vimdiff’s expand and collapse features come in handy. They let you focus on specific changes or hide parts of the files to make things clearer. In this guide, we’ll show you how to use these features in Vimdiff, so you can easily navigate the differences.

Whether you’re already familiar with Vim or just starting, learning how to use these features will make you more productive and help you compare files more easily. So let’s get started and discover all the great things you can do with Vimdiff in Linux!

Prerequisites

  1. In this article, We are assuming that you already know how to use Vim. If you’re not familiar with Vim, don’t worry! You can check out other articles on Geeksforgeeks to learn about Vim.
  2. You should be familiar with the command line and basic commands of Vim.

Procedure

Step 1: To facilitate the step-by-step instructions provided in this tutorial, let’s generate two dummy files for illustrative purposes. You can utilize the Vim command to create both of these files effortlessly. For instance:

$ vim dum_file1.py
$ vim dum_file2.py

Enter the code provided below in both files and save them by pressing the “Esc” key followed by “:wq“. This command combination will ensure that your changes are saved, and the files are closed.

How to expand/collapse a diff sections in Vimdiff in Linux

Dummy File 1

How to expand/collapse a diff sections in Vimdiff in Linux

Dummy File 2

Step 2: In both examples, we are demonstrating how to calculate the factorial of a number in Python using two different approaches: recursion and a while loop.

Step 3: To open both files vertically in your default terminal or command prompt, you can use the command vimdiff <file1> <file2>. This command will launch vimdiff with file1 and file2 displayed side by side.

$ vimdiff dum_file1.py dum_file2.py
How to expand/collapse a diff sections in Vimdiff in Linux

Command: vimdiff dum_file1.py dum_file2.py

Step 4: Go to the last line to find the fold in the code. Folds are indicated by lines starting with “+–” and represent hidden or collapsed text.

How to expand/collapse a diff sections in Vimdiff in Linux

Fold

Step 5: To expand a folded section, use the Up and Down keys to move the cursor to the desired folded area, and then press the “zo” keys.

How to expand/collapse a diff sections in Vimdiff in Linux

Folded Section

Step 6: Press the “zc” keys to close a folded section.

Conclusion

In conclusion, Vimdiff is a powerful command-line tool in Linux that facilitates file comparison and merging using the Vim editor. It offers features such as expanding or collapsing diff sections, colourful text display, and efficient handling of large files. Tools like Vimdiff are essential for avoiding errors and simplifying comparing files manually, mainly when dealing with complex or extensive codebases. Users can streamline their file comparison and merging tasks by following the provided procedures, including creating dummy files, using Vimdiff commands for expanding and collapsing sections, and utilizing shortcuts. Overall, Vimdiff is a valuable tool for developers, allowing them to manage changes and resolve differences in files efficiently.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads