Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

No, it doesn't, but nothing prevents you from writing your own extended command:

command! -range=-1 -nargs=+ Diffput for bufspec in [<f-args>] | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffput' bufspec | endfor

Here's what I use when I'm merging with 3 buffers via vimdiff. It'll always include the current buffer (as a no-op)

:diffput 1 | diffput 2 | diffput 3 | diffu

I only ever use 3 files max, but if you wanted to support a variety of buffer amounts, you could alias the above command (such as :dp3) and then similarly alias several more buffer amounts (dp4, dp5, ...)

As @glts said, no this is not possible.

The help from :exec 'helpg bufspec' | clast says this

The [bufspec] argument above can be a buffer number, a pattern for a buffer
name or a part of a buffer name.  Examples:

    :diffget        Use the other buffer which is in diff mode
    :diffget 3      Use buffer 3
    :diffget v2     Use the buffer which matches "v2" and is in
                diff mode (e.g., "file.c.v2")

To expand on Ingo's solution:

command! -range=-1 -nargs=+ DG for bufspec in [<f-args>] | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffget' bufspec | endfor
command! -range=-1 -nargs=* DGA for bufspec in map(filter(range(1, winnr('$')), 'getwinvar(v:val, "&diff")'), 'winbufnr(v:val)') | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffget' bufspec | endfor
command! -range=-1 -nargs=+ DP for bufspec in [<f-args>] | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffput' bufspec | endfor
command! -range=-1 -nargs=* DPA for bufspec in map(filter(range(1, winnr('$')), 'getwinvar(v:val, "&diff")'), 'winbufnr(v:val)') | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffput' bufspec | endfor
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top