Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 83 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
zm               - fold 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
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
Hi, does anyone know if there is an equivalent of "do" (obtain difference) for JUST one line? Let's say you have a block of codes (more than one line) that are different, "do" with cursor either at any line of these codes for just the line below these code will obtain difference for the whole block of code --- But I just want to obtain difference for ONE line. Every time I have to visually select this line, followed by "diffget", a bit tedious ... – wiswit May 28 '15 at 9:37
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 '15 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.