A Developer In The mountains having fun

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.

  1. Make sure you have two buffers open in separate windows (e.g., using :split or :vsplit).
  2. Go to the first window and type:
    :diffthis
    
  3. Switch to the second window (e.g., Ctrl-w w or Ctrl-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.

  1. Open one of them (or just be in it):
    :e buffer_A
    
  2. Then open the second one in a vertical split for a side-by-side view (my personal fave!):
    :vsplit buffer_B
    
  3. Now, in both windows, you can apply :diffthis as in Scenario A, or even better, if they're the only two windows, use:
    :windo diffthis
    
    This command applies diffthis to 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.

More places to find me
Mental Health
follow me on Mastodon