如何在Vim的同一文件中“区分”两个子程序? [英] How to ‘diff’ two subroutines in the same file in Vim?

查看:88
本文介绍了如何在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, "\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拒绝,您可能想设置 hidden 选项放弃更改的文件(请参见:h放弃)。

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

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

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