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

How do I accept or reject individual changes in Vim's diff mode?

Answer

dp / do

Explanation

When reviewing differences between files in Vim's built-in diff mode, dp and do let you selectively apply individual hunks without leaving the editor. dp (diff put) pushes the current hunk from your window into the other; do (diff obtain) pulls the hunk from the other window into yours.

How it works

  • Open a diff view with :diffsplit filename or launch with vim -d file1 file2
  • Navigate between hunks using ]c (next change) and [c (previous change)
  • With the cursor on a changed hunk:
    • dp — copies the hunk from the current window into the other window
    • do — copies the hunk from the other window into the current window
  • The diff display refreshes automatically after each operation

Example

# Left window (your version)       # Right window (other version)
function greet() {                 function greet() {
    return 'hello';                    return 'hi';    ← hunk
}                                  }

With cursor on the hunk in the left window:

  • dp replaces 'hi' in the right window with 'hello'
  • do replaces 'hello' in the left window with 'hi'

Tips

  • Use ]c / [c to jump between all diff hunks without the mouse
  • :diffupdate refreshes the diff highlights if they become stale after edits
  • :diffoff exits diff mode and restores a normal window layout
  • In a three-way merge, you can use :diffget 2 or :diffget 3 to pull from a specific buffer by number

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?