Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diffput to multiple buffers?

Tags:

vim

diff

vimdiff

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?

like image 726
Cory Klein Avatar asked Oct 25 '13 16:10

Cory Klein


People also ask

What is Vimdiff?

What is vimdiff? Vimdiff is a Linux command that can edit two, three, or four versions of a file with Vim and show their differences.


4 Answers

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
like image 162
Ingo Karkat Avatar answered Oct 29 '22 07:10

Ingo Karkat


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.

like image 35
SheetJS Avatar answered Oct 29 '22 08:10

SheetJS


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, ...)

like image 42
MultiRRomero Avatar answered Oct 29 '22 06:10

MultiRRomero


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")
like image 1
Cory Klein Avatar answered Oct 29 '22 06:10

Cory Klein