How do I merge changes between two diff windows using diffget and diffput?
Answer
do (diffget) / dp (diffput)
Explanation
How it works
When you open two files in diff mode (using vim -d file1 file2 or :windo diffthis), Vim highlights the differences between them. You can merge individual changes using two shortcut commands:
do(short for:diffget) pulls the change from the other window into the current window. Think "diff obtain."dp(short for:diffput) pushes the change from the current window into the other window. Think "diff put."
Both commands operate on the diff hunk (block of changes) where your cursor is positioned. You can navigate between hunks with ]c (next change) and [c (previous change).
When more than two windows are in diff mode, you need to specify which buffer to get from or put to by using the full commands: :diffget bufnr or :diffput bufnr.
Example
Open two versions of a file:
vim -d config_old.yml config_new.yml
Vim shows them side by side with changes highlighted. Navigate to a diff hunk with ]c.
- If you are in
config_old.ymland want to accept the new version's change: pressdoto pull from the right. - If you are in
config_new.ymland want to revert to the old version: pressdoto pull from the left. - Press
dpto push the current window's version to the other side.
After merging, run :diffupdate to refresh the highlighting if it gets out of sync. When you are done, :windo diffoff turns off diff mode in all windows.
This workflow is essential for resolving Git merge conflicts directly in Vim, where you can compare two versions and selectively merge hunks.