25 December 2025

How to compare differences of files in Vim?

In Vim, you can compare (diff) the file you already have open with another file in a few common ways. Here are the most practical ones.


1️⃣ From inside Vim (recommended)

If you already have file A open and want to compare it with file B:

:vert diffsplit path/to/other_file
  • Opens the other file in a vertical split

  • Vim automatically enters diff mode

  • Differences are highlighted

Horizontal split (if you prefer):

:diffsplit path/to/other_file

2️⃣ If both files are already open in Vim

If you opened two files normally (no diff yet):

:diffthis

Run this command in each window you want to compare.

To turn diff mode off later:

:diffoff

3️⃣ Start Vim in diff mode (for future reference)

From the terminal:

vim -d file1 file2

or

vimdiff file1 file2

4️⃣ Useful diff navigation commands

While in diff mode:

CommandWhat it does
]cJump to next difference
[cJump to previous difference
:diffgetGet change from the other file
:diffputPut change to the other file
dodiff obtain (same as diffget)
dpdiff put

5️⃣ Exit diff mode cleanly

:diffoff!

or close one of the windows:

:q

💡 Tip (since you use Linux a lot)

Vertical diff is often easier on wide terminals:

:vert diffsplit other_file

If you want, I can also show:

  • how to compare 3 files

  • how to diff against the current buffer on disk

  • how to use Vim diff with Git

Just tell me 👍

How to compare 3 files, please.

 

 

In Vim, comparing 3 files is very doable. Here are the clean, correct ways, from simplest to more advanced.


✅ Method 1: Start Vim with 3 files in diff mode (best & simplest)

From the terminal:

vimdiff file1 file2 file3

or

vim -d file1 file2 file3

Result:

  • All 3 files open side-by-side

  • Vim automatically enters diff mode

  • Differences highlighted across all three


✅ Method 2: Add a 3rd file while already diffing 2 files

If you already have file1 open and diffed with file2:

:vert diffsplit file3

Vim will:

  • Open file3 in a new split

  • Automatically include it in the diff

💡 For horizontal split instead:

:diffsplit file3

✅ Method 3: Files already open → manually enable diff

If all three files are open in different windows but not diffed yet:

  1. Go to each window and run:

    :diffthis
    
  2. Repeat for all three windows

To exit later:

:diffoff

🧭 Navigating differences (very important with 3 files)

CommandDescription
]c / [cNext / previous diff
doTake change from another file
dpPut change into another file
:diffget LOCALGet change from current window
:diffget REMOTEGet change from another window

👉 With 3 files, Vim labels them as:

  • LOCAL

  • BASE

  • REMOTE

You can be explicit:

:diffget BASE
:diffget LOCAL
:diffget REMOTE

🧠 Common real-world example (Git-style 3-way diff)

vimdiff mine theirs base

Typical meaning:

  • BASE = original

  • LOCAL = your version

  • REMOTE = incoming version


🧹 Exit diff mode

:diffoff!

or close windows normally:

:q

⭐ Pro tips

  • Use vertical splits on wide screens:

    set diffopt+=vertical
    
  • Synchronize scrolling:

    :set scrollbind
    

If you want, I can also explain:

  • 3-way merge conflict resolution

  • diff against Git commits

  • color customization for diffs

Just say the word 👍

No comments:

Post a Comment