Plugin solutions
As already mentioned, there are several plugins:
- BlockDiff.vim is an old, minimalistic plugin to view a diff between text blocks in a separate tab page.
- linediff.vim is a modern, way more powerful variant that can also sync back changes and even handles merge conflicts, shows the scope with signs, etc.
- spotdiff.vim has a different approach: instead of temporarily copying the areas to separate buffers (in a new tab page), it instead does the diff in the original windows and just fades out the unrelated parts.
- My own AdvancedDiffOptions plugin has a
:DiffIRange command that excludes certain lines from the diff, via a custom 'diffexpr' that filters the files via shell commands. The plugin can also filter by line and pattern.
homemade
On Unix, you can combine shell (e.g. Bash) features with common tools (sed) to just feed the extracted text contents to vim:
vimdiff <(sed -n '14,26p' file1.py) <(sed -n '30,60p' file2.py)
This uses process substitution (<(command)) to run command, and provide its output as a temporary file descriptor, which Vim can read just like any other file. sed with default output suppression (-n) is used to selectively choose lines (via the {range}print command); you could also use head, tail, or even grep, depending on the situation.
This works similar to my plugin; the downside here is that only the selected ranges are accessible inside Vim.