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

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."
  • doobtains (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 foo hunk, press dp → pushes foo = 1 change to the right buffer
  • Cursor on the foo hunk, press do → replaces current line with the right buffer's version

Tips

  • Navigate between hunks with ]c (next) and [c (previous)
  • After manually editing lines, run :diffupdate to refresh stale diff highlighting
  • :diffoff! turns off diff mode in all windows at once
  • For 3-way merges, use :diffget 2 or :diffget 3 to select which buffer to pull from

Next

How do I encode and decode JSON data in Vimscript for configuration and plugin development?