有时,在使用
vimdiff
编辑三个文件时,我想将一个文件中的一大块复制到另外两个文件中。通常这会像这样完成:
:diffput 2
:diffput 3
但是,
:help diffput
说:
*:diffpu* *:diffput* *E793*
:[range]diffpu[t] [bufspec]
这让我很好奇
bufspec
是否允许您指定多个缓冲区。我尝试使用文档,然后只是猜测,但没有运气。
:help bufspec
:diffput 2,3
:diffput 2 3
是否可以在
diffput
命令中指定多个缓冲区?
接受的答案要求您指定哪些缓冲区将接收差异。 从你的问题措辞来看,听起来你想将更改推送到每个其他缓冲区(例如,如果你有 10 个 diff 缓冲区——重新编译 vim 后——你需要 diffput 到缓冲区 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
推送到所有缓冲区。
不,不是,但是没有什么可以阻止您编写自己的扩展命令:
command! -range=-1 -nargs=+ Diffput for bufspec in [<f-args>] | execute (<count> == -1 ? '' : '<line1>,<line2>') . 'diffput' bufspec | endfor
这是我通过 vimdiff 合并 3 个缓冲区时使用的内容。它将始终包含当前缓冲区(作为无操作)
:diffput 1 | diffput 2 | diffput 3 | diffu
我最多只使用 3 个文件,但如果您想支持各种缓冲区数量,您可以为上述命令添加别名(例如
:dp3
),然后类似地为多个缓冲区数量添加别名(dp4、dp5... )
正如@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")
扩展 Ingo 的解决方案:
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
您可以像普通命令一样执行,然后重复。例如
:norm 1dp2dp
要重复另一部分,请点击:
@: