vimdiffvimdiff

diffput to multiple buffers?


Sometimes while editing three files using vimdiff I want to copy a hunk from one file to both of the other two. Normally this would be accomplished like so:

:diffput 2
:diffput 3

However, :help diffput says this:

                        *:diffpu* *:diffput* *E793*
:[range]diffpu[t] [bufspec]

This makes me curious whether bufspec allows you to specify more than one buffer. I tried using the docs, and then just guessing, but no luck.

:help bufspec
:diffput 2,3
:diffput 2 3

Is it possible to specify more than one buffer in a diffput command?


Solution

  • The accepted answer requires you to specify which buffers will receive the diff. From your question wording, it sounds like you want to push a change to every other buffer (e.g. if you had 10 diff buffers -- after recompiling vim -- you'd need to diffput to buffers 1,2,3,4,5,6,7,8,9)

    I use the following to push to all buffers:

    function! GetDiffBuffers()
        return map(filter(range(1, winnr('$')), 'getwinvar(v:val, "&diff")'), 'winbufnr(v:val)')
    endfunction
    
    function! DiffPutAll()
        for bufspec in GetDiffBuffers()
            execute 'diffput' bufspec
        endfor
    endfunction
    
    command! -range=-1 -nargs=* DPA call DiffPutAll()
    

    and then just run :DPA to push to all buffers.