如何“区分” Vim 中同一个文件中的两个子例程?
是否可以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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
插件 linediff.vim :对两个文本块执行交互式比较与 Vincent 指出的类似,具有一些附加功能:
要使用它,您可以在要比较的第一个块上执行视觉选择,输入命令
:Linediff
,然后将其重复到第二个块。要退出,:LineDiffReset
我发现以下地图很有帮助:
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:
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:
您不能在原始文件中执行此操作,但您可以在不使用单独的文件而仅使用单独的缓冲区的情况下执行此操作。如果您在寄存器
a
中复制了一个子例程(例如,在可视模式下键入"ay
)并在寄存器b
中复制了其他子例程,则此操作应该有效code>:自动化:
请注意,如果 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 registerb
:To automate:
Note that you may want to set
hidden
option if Vim refuses to abandon changed file (see:h abandon
).您可以将这两个部分/子例程/部分写入两个文件,然后使用 vimdiff 来查看差异。
如果您不习惯使用行号,可以将光标保持在该部分的开头,按 v 并选择直到该部分的末尾,然后按 : 。它将显示:'<,'>。然后在命令行中键入 write,然后键入文件名。按回车键。同样,第二个也这样做。然后你可以执行上面提到的 vimdiff 命令。
(写入命令将部件保存到新文件中。)
编写新文件可能不是一个好主意,但这对我有帮助。尤其是当我们必须进行多次比较时。
这是最简单的方法之一,无需使用插件或者您不关心内存。
You can write those two parts/subroutines/sections to two files and then use vimdiff to see the difference.
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.
我一直在使用这个命令:
其中输入到 sed 的数字是我想要在文件中比较的行号。
<(*)
表示评估并重定向为输入。I've been using this command:
Where the numbers fed to
sed
are the line number I want to diff in the file.<(*)
means evaluate and redirect as input.我真的很喜欢 ZyX 的答案,但需要进行两项修改才能使其无缝工作:
在实现时,>用垂直分割的差异显示替换活动缓冲区。然后,当时,关闭 diff,它不会重新加载原始缓冲区。为了解决这个问题,我将第三行的
enew
更改为execute 'tab split | enew'
。调用remove(a:diffed_buffers, 0, -1)
。哈特哈,
- 斯图
I really like ZyX's answer, but needed to make two modifications for it to work seamlessly:
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 toexecute 'tab split | enew'
.In order to minimize side-effects, I added
call remove(a:diffed_buffers, 0, -1)
just before the end of WipeOutDiffs().HTH,
- Stu
你可以尝试 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.