如何“区分” Vim 中同一个文件中的两个子例程?

发布于 2024-09-17 05:39:31 字数 139 浏览 2 评论 0原文

是否可以diff甚至vimdiff同一文件中出现的两个非常相似的子例程?如果是这样,怎么办?

我可以考虑将两个子例程复制到两个单独的文件中,然后对它们进行比较,但是有没有办法在原始文件中执行此操作?

Is it possible to diff or even vimdiff two very similar subroutines occurring 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

机场等船 2024-09-24 05:39:31

插件 linediff.vim :对两个文本块执行交互式比较与 Vincent 指出的类似,具有一些附加功能:

  • 有一个关闭打开的缓冲区的命令
  • 似乎可以在没有 GUI 的情况下工作,
  • 在正在比较的原始文件上插入一些视觉指示。

要使用它,您可以在要比较的第一个块上执行视觉选择,输入命令 :Linediff,然后将其重复到第二个块。要退出,:LineDiffReset

我发现以下地图很有帮助:

noremap <leader>ldt :Linediff<CR>
noremap <leader>ldo :LinediffReset<CR>

Plugin linediff.vim : Perform an interactive diff on two blocks of text is similar to the one pointed ou by Vincent with some additional features:

  • has a command to close the opened buffer
  • seems to work without GUI
  • insert some visual indication on the original file(s) being diffed.

To use it you perform a visual selection on the first block to diff, enter command :Linediff, and repeat it to the second block. To quit, :LineDiffReset

I've found the followings maps helpful:

noremap <leader>ldt :Linediff<CR>
noremap <leader>ldo :LinediffReset<CR>
总攻大人 2024-09-24 05:39:31

您不能在原始文件中执行此操作,但您可以在不使用单独的文件而仅使用单独的缓冲区的情况下执行此操作。如果您在寄存器a 中复制了一个子例程(例如,在可视模式下键入 "ay)并在寄存器b 中复制了其他子例程,则此操作应该有效code>:

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放弃)。

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

To automate:

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>

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

我做我的改变 2024-09-24 05:39:31

您可以将这两个部分/子例程/部分写入两个文件,然后使用 vimdiff 来查看差异。

    :1, 39 write part1          //any line range or marks can be used
    :40, 79 write part2
    :!vimdiff part1 part2

如果您不习惯使用行号,可以将光标保持在该部分的开头,按 v 并选择直到该部分的末尾,然后按 : 。它将显示:'<,'>。然后在命令行中键入 write,然后键入文件名。按回车键。同样,第二个也这样做。然后你可以执行上面提到的 vimdiff 命令。

(写入命令将部件保存到新文件中。)
编写新文件可能不是一个好主意,但这对我有帮助。尤其是当我们必须进行多次比较时。

这是最简单的方法之一,无需使用插件或者您不关心内存。

You can write those two parts/subroutines/sections to two files and then use vimdiff to see the difference.

    :1, 39 write part1          //any line range or marks can be used
    :40, 79 write part2
    :!vimdiff part1 part2

If you aren't comfortable with using line numbers, you can keep the cursor at start of the section , press v and select till the end of the section and then press : . it will show :'<,'>. Then type write and then file name in the command line itself. Press enter. Similary, do for second one also. Then you can execute vimdiff command as stated above.

(Write command saves the part to a new file.)
Writing a new file may not be a good idea, but that helps me. Especially when we had to go through the comparison several times.

This is one of the simplest way without using plugin or if you aren't concerned about memory.

待"谢繁草 2024-09-24 05:39:31

我一直在使用这个命令:

vimdiff <(cat file.foo | sed -n 10,15p) <(cat file.foo | sed -n 20,25p)

其中输入到 sed 的数字是我想要在文件中比较的行号。 <(*) 表示评估并重定向为输入。

I've been using this command:

vimdiff <(cat file.foo | sed -n 10,15p) <(cat file.foo | sed -n 20,25p)

Where the numbers fed to sed are the line number I want to diff in the file. <(*) means evaluate and redirect as input.

难理解 2024-09-24 05:39:31

我真的很喜欢 ZyX 的答案,但需要进行两项修改才能使其无缝工作:

  1. 在实现时,>用垂直分割的差异显示替换活动缓冲区。然后,当时,关闭 diff,它不会重新加载原始缓冲区。为了解决这个问题,我将第三行的 enew 更改为 execute 'tab split | enew'

  2. 最大限度地减少副作用,我在 WipeOutDiffs() 结束之前添加了调用remove(a:diffed_buffers, 0, -1)

哈特哈,
- 斯图

I really like ZyX's answer, but needed to make two modifications for it to work seamlessly:

  1. As implemented, <F7> replaces the active buffer with the vertically split diff display. Then, while <F8> closes the diff, it does not reload the original buffer. To fix this, I changed enew on the third line to execute 'tab split | enew'.

  2. In order to minimize side-effects, I added call remove(a:diffed_buffers, 0, -1) just before the end of WipeOutDiffs().

HTH,
- Stu

遮了一弯 2024-09-24 05:39:31

你可以尝试 Block diff vim 插件,它会创建 2 个新缓冲区在新选项卡中显示差异。

you can try Block diff vim plugin, it will make 2 new buffer in a new tab to show the differences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文