2013-10-25 139 views
4

有时在使用vimdiff编辑三个文件时,我想从一个文件复制一个文件到另外两个文件。通常这会来完成,像这样:diffput到多个缓冲区?

:diffput 2 
:diffput 3 

然而,:help diffput这样说:

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

这让我好奇bufspec是否允许你指定一个以上的缓冲器。我尝试使用文档,然后只是猜测,但没有运气。

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

是否可以在diffput命令中指定多个缓冲区?

+1

没有。答案在':exec'helpg bufspec'|帮助clast'。 – glts

回答

1

接受的答案,需要你指定哪个缓冲区将收到差异。从你的问题措辞来看,你听起来像是想把改变推向其他任何缓冲区(例如,如果你有10个diff缓冲区 - 在重新编译vim之后 - 你需要分配到缓冲区1,2,3,4,5 ,6,7,8,9)

我用下面推送到所有的缓冲区:

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() 

,然后只需运行:DPA推向所有缓冲区。

0

正如@glts所说,没有这是不可能的。

:exec 'helpg bufspec' | clast帮助说这

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") 
3

不,不,但没有阻止你编写自己的扩展命令:

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

为了扩大对英戈的解决方案:

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 
0

这里是当我通过vimdiff同时3个缓冲区合并我使用。它会永远包括当前缓冲区(无操作)

:diffput 1 | diffput 2 | diffput 3 | diffu

我只用3个文件最多,但是如果你想支持多种缓冲量,你可以别名上面的命令(例如:dp3),然后类似地别名多个缓冲区量(dp4,dp5,...)