Diff files using neoVim
1. The vimdiff Command (for files)
If you're starting Neovim fresh and want to compare two files, this is your go-to:
nvim -d file1.txt file2.txt
This will launch Neovim with both files open in a split window, and it'll highlight all the juicy differences.
2. The :diffthis Command (for open buffers)
Now, this is where it gets really fun and flexible, especially if you already have files or even unsaved text in buffers:
Scenario A: You have two buffers open in separate splits.
- Make sure you have two buffers open in separate windows (e.g., using
:splitor:vsplit). - Go to the first window and type:
:diffthis - Switch to the second window (e.g.,
Ctrl-w worCtrl-w h/l/j/k) and type::diffthis
Voila! Neovim will automatically show you the differences between those two buffers. It's like magic, but better, it's code!
Scenario B: You have two buffers, and you want to quickly set up the split.
Let's say you have buffer_A and buffer_B that you want to compare.
- Open one of them (or just be in it):
:e buffer_A - Then open the second one in a vertical split for a side-by-side view (my personal fave!):
:vsplit buffer_B - Now, in both windows, you can apply
:diffthisas in Scenario A, or even better, if they're the only two windows, use:
This command applies:windo diffthisdiffthisto all windows in your current tab. So efficient!
Getting Around in Diff Mode & Other Goodies:
Once you're in diff mode, you'll love these shortcuts:
[c: Jump to the previous change.]c: Jump to the next change.do(diffget, in one buffer): Pulls changes from the other buffer into the current one. Use this when you want to accept a change from the other side.dp(diffput, in one buffer): Pushes changes from the current buffer into the other one. Use this when you want to apply your changes to the other side.:diffupdate: If you've made more changes, use this to refresh the diff highlighting.:diffoff: To turn off diff mode for the current window.:windo diffoff: To turn off diff mode for all windows in the current tab.
Neovim's diffing capabilities are seriously powerful and so handy for comparing code, notes, or anything else you're working on.
