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:
| Command | What it does |
|---|---|
]c | Jump to next difference |
[c | Jump to previous difference |
:diffget | Get change from the other file |
:diffput | Put change to the other file |
do | diff obtain (same as diffget) |
dp | diff 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
file3in a new splitAutomatically 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:
Go to each window and run:
:diffthisRepeat for all three windows
To exit later:
:diffoff
🧭 Navigating differences (very important with 3 files)
| Command | Description |
|---|---|
]c / [c | Next / previous diff |
do | Take change from another file |
dp | Put change into another file |
:diffget LOCAL | Get change from current window |
:diffget REMOTE | Get change from another window |
👉 With 3 files, Vim labels them as:
LOCALBASEREMOTE
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+=verticalSynchronize 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