Skip to content
Advertisement

how can I diff two sections of the same file?

I have a source file with two similar yet subtly different sections. I’d like to merge the two sections into one subroutine with a parameter that handles the subtle differences, but I need to be sure I’m aware of them all so I don’t miss any.
What I usually do in such cases is copy each of the sections to a separate file and then use tkdiff or vimdiff to highlight the differences. Is there any way to skip the intermediate files and just diff two parts of the same file?

Advertisement

Answer

The linediff plugin for Vim works well for me. Visually select one section of your file and type :Linediff. Visually select the other section and type :Linediff. It will put vim in to vimdiff mode, showing only the two sections you highlighted previously. Type:LinediffReset to exit vimdiff mode.

More info:

https://unix.stackexchange.com/a/52759/32477

https://superuser.com/a/414958/199800

Advertisement