vimtricks.wiki Concise Vim tricks, one at a time.

How do I pull a diff change from another window into the current buffer in Vim?

Answer

do

Explanation

The do command (diff obtain) is shorthand for :diffget. In Vim's diff mode, it replaces the hunk under the cursor in the current buffer with the corresponding content from the other diff window. This is the fastest way to accept a change from the opposite side when reviewing diffs or resolving merge conflicts.

How it works

  • do stands for diff obtain — it fetches the change from the other buffer
  • It acts on the hunk under the cursor, not the entire file
  • The complement is dp (diff put), which pushes the current buffer's version into the other buffer
  • In a two-buffer diff, Vim automatically picks the other window. In a three-buffer diff, you must specify: :diffget 2 (by buffer number) or :diffget LOCAL / :diffget REMOTE when using merge tools

Example

Open two files for comparison:

:vert diffsplit original.txt

Navigate to a changed hunk with ]c, then press:

do

The hunk in your current buffer is replaced with the version from original.txt.

Tips

  • ]c and [c jump to the next and previous diff hunks respectively
  • After accepting several changes, run :diffupdate to refresh the diff highlighting if it becomes stale
  • :diffoff! exits diff mode in all windows at once and restores normal display
  • When using Git's three-way merge (git mergetool), the buffers are typically labelled LOCAL (2), BASE (3), and REMOTE (4) — use :diffget 2 or :diffget 4 to pull from each side

Next

How do I use a Vimscript function to control how the gq operator formats text?