(Vim)diff同一文件中的两个子程序 [英] (Vim)diff two subroutines in same file

查看:164
本文介绍了(Vim)diff同一文件中的两个子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对同一文件中出现的两个几乎相似的子例程进行比较甚至vimdiff处理?如果可以,怎么办?

Is it possible to diff or even vimdiff two almost similar subroutines which occur in the same file? If so, how?

我可以考虑将两个子例程复制到两个单独的文件中,然后对它们进行差异化处理,但是有没有办法在原始文件中做到这一点?

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, "\n")) | diffthis | vnew | call setline(1, split(@b, "\n")) | 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, "\n"))
    diffthis
    vnew
    setlocal buftype=nowrite
    call add(a:diffed_buffers, bufnr('%'))
    call setline(1, split(a:b, "\n"))
    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拒绝放弃更改的文件(请参阅:h abandon),则可能需要设置hidden选项.

Note that you may want to set hidden option if Vim refuses to abandon changed file (see :h abandon).

这篇关于(Vim)diff同一文件中的两个子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆