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 filenameor launch withvim -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 windowdo— 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:
dpreplaces'hi'in the right window with'hello'doreplaces'hello'in the left window with'hi'
Tips
- Use
]c/[cto jump between all diff hunks without the mouse :diffupdaterefreshes the diff highlights if they become stale after edits:diffoffexits diff mode and restores a normal window layout- In a three-way merge, you can use
:diffget 2or:diffget 3to pull from a specific buffer by number