How do I push or pull a single diff hunk between buffers with a single keystroke in diff mode?
Answer
dp
Explanation
In Vim's diff mode, dp (diff put) and do (diff obtain) are single-keystroke shorthands for :diffput and :diffget. They let you transfer the hunk under the cursor between diff buffers without leaving normal mode or typing an Ex command.
How it works
dp— pushes the hunk under the cursor from the current buffer to the other diff buffer. Think: "put it over there."do— obtains (gets) the hunk from the other buffer into the current buffer. Think: "get it from the Other side."- Both operate on the hunk containing the cursor; no range selection needed
- If there are more than two diff buffers, use
:diffget {bufnr}or:diffput {bufnr}to target a specific buffer
Example
With two files open in a vertical diff (:vertical diffsplit otherfile):
Left (current) Right (other)
--- ---
+ foo = 1 ↔ foo = 1
bar = 2 bar = 2
- Cursor on the
foohunk, pressdp→ pushesfoo = 1change to the right buffer - Cursor on the
foohunk, pressdo→ replaces current line with the right buffer's version
Tips
- Navigate between hunks with
]c(next) and[c(previous) - After manually editing lines, run
:diffupdateto refresh stale diff highlighting :diffoff!turns off diff mode in all windows at once- For 3-way merges, use
:diffget 2or:diffget 3to select which buffer to pull from