Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When comparing files, and updating a source code repository, I like to use vimdiff. To copy changes made from one file to the other, I usually use a key sequence like this:-

Shift + V (select line)
k or j; { or }; Up or down arrow keys (select more lines)
y  (copy selected lines)
Ctrl + w, left/right arrow (move to other pane)
p (paste lines)

Vim, being the master of keyboard shortcuts, should surely have an easier way of performing this same task. Is there one? What do you use to manually update source-code changes?

share|improve this question
Thanks for putting my vague sense of "this isn't what it should be" into a meaningful question. The answer elicited is something I should have learned a long time ago. –  Caleb Oct 24 '12 at 12:21
Best answers for your question are here: stackoverflow.com/questions/5288875/… –  erik Oct 8 '13 at 17:53

1 Answer 1

up vote 57 down vote accepted

do (diff obtain) and dp (diff put) is what you need. Here is a small list of other helpful commands in this context.

]c               - advance to the next block with differences
[c               - reverse search for the previous block with differences
do (diff obtain) - bring changes from the other file to the current file
dp (diff put)    - send changes from the current file to the other file
zo               - unfold/unhide text
zc               - refold/rehide text
zr               - unfold both files completely

Note: Both do and dp work if you are on a block or just one line under a block.

:diffupdate will re-scan the files for changes

share|improve this answer
That's wicked! I didn't know any of those diff mode commands. Thanks! –  Alex Leach Oct 24 '12 at 11:16
Just started to use these, but do and dp don't work in Visual modes. –  Alex Leach Oct 24 '12 at 13:59
True. I found it quite confusing initially, as entire (contiguous) blocks are copied across. That, along with diff mode's auto-hiding of identical lines, I found myself thinking "What the hell just happened?", before triple-checking each move, in both panes. Say you've changed the indentation on one of the files; then I imagine everything will be moved across. For finer-grained control (i.e. Visual selection), I found that the full commands :[range]diffget and :[range]diffput are needed. –  Alex Leach Oct 24 '12 at 15:03
You can use visual mode together with diffget and diffput: ① Enter visual mode and mark some text/lines. ② Then type :diffput to push the selected lines to the other file or :diffget to get the selected lines from the other file. –  erik Oct 8 '13 at 17:46
It's important to note that the undo command will only work in the buffer that was changed, so if you use dp and change your mind, you need to switch to the other buffer to undo. –  Random832 Jun 11 at 18:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.