163

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?

2
  • 2
    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, 2012 at 12:21
  • 1
    Best answers for your question are here: stackoverflow.com/questions/5288875/…
    – erik
    Oct 8, 2013 at 17:53

1 Answer 1

263

do (diff obtain) and dp (diff put) is what you need. Here is a small list of other helpful normal mode 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 in normal mode but not in visual mode. When selecting lines of text in visual mode, you must use the normal commands

  • :'<,'>diffget and
  • :'<,'>diffput.

See also :h copy-diffs.

:diffupdate will re-scan the files for changes.

11
  • 4
    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, 2012 at 15:03
  • 9
    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, 2013 at 17:46
  • 1
    Thanks for the comment about putting the cursor below - I was trying from above and do didn't work
    – hgolov
    May 1, 2015 at 6:54
  • 8
    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, 2015 at 9:37
  • 18
    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, 2015 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .