如何在 Vim 的同一个文件中“区分"两个子程序? [英] How to ‘diff’ two subroutines in the same file in Vim?
问题描述
是否可以diff
甚至vimdiff
两个非常相似的子程序出现在同一个文件中?如果是,怎么办?
Is it possible to diff
or even vimdiff
two very similar subroutines occurring in the same file? If so, how?
我可以考虑将两个子程序复制到两个单独的文件中,然后 diff
它们,但是有没有办法在原始文件中做到这一点?
I can think of copying the two subroutines in two separate files and then diff
them, but is there a way to do it within the original file?
推荐答案
您不能在原始文件中执行此操作,但可以在不使用单独文件的情况下执行此操作,只需使用单独的缓冲区即可.如果您复制寄存器 a
中的一个子例程(例如,在可视模式下键入 "ay
)和寄存器 b
中的其他子例程,这应该有效>:
You cannot do this within the original file, but you can do this without using separate files, only separate buffers. This should work if you copied one subroutine in register a
(for example, with "ay
typed in Visual mode) and other subroutine in register b
:
enew | call setline(1, split(@a, "
")) | diffthis | vnew | call setline(1, split(@b, "
")) | diffthis
自动化:
let g:diffed_buffers = []
function DiffText(a, b, diffed_buffers)
enew
setlocal buftype=nowrite
call add(a:diffed_buffers, bufnr('%'))
call setline(1, split(a:a, "
"))
diffthis
vnew
setlocal buftype=nowrite
call add(a:diffed_buffers, bufnr('%'))
call setline(1, split(a:b, "
"))
diffthis
endfunction
function WipeOutDiffs(diffed_buffers)
for buffer in a:diffed_buffers
execute 'bwipeout! ' . buffer
endfor
endfunction
nnoremap <special> <F7> :call DiffText(@a, @b, g:diffed_buffers)<CR>
nnoremap <special> <F8> :call WipeOutDiffs(g:diffed_buffers) | let g:diffed_buffers=[]<CR>
请注意,如果 Vim 拒绝放弃更改的文件,您可能需要设置 hidden
选项(请参阅 :h 放弃
).
Note that you may want to set hidden
option if Vim refuses to abandon changed file (see :h abandon
).
这篇关于如何在 Vim 的同一个文件中“区分"两个子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!