How to use vimdiff to compare the first 1000 line of two very large files (say a.txt and b.txt)

Viewed 906

As the title, 1000 lines became a little bit too much to use diff, and so vimdiff is the best choice. My first thought was that if vimdiff provide this as in-building function. If not, a walk around would be combine vimdiff and head, as a one-liner without creating two buffer files.

1 Answers

Assuming you're running bash, you could invoke it as

$ vimdiff <(head -n 1000 "file1.txt") <(head -n 1000 "file2.txt")

(or gvimdiff). This uses bash process substitution to take a temporary named pipe holding the outputs of those head commands and compares them in vimdiff.

Those pipes' names will vary based on your system. On my Linux system, they look like /proc/123456/fd/11 and /proc/123456/fd/12 (where 123456 is the process ID of the parent bash shell). Note that these won't share the extensions of your original files (and you may confuse which is which; remember the leftmost was the first and the rightmost was the last—which I say because you can put in a third as well). Consider a command like vimdiff -c 'windo set syn=bash' <(head …) <(head …) to e.g. set both of them to use the bash format for highlighting.

If you want better labels, you could shove an echo in there, like <(echo "file1.txt"; head …) and it'll appear on the first line. To assign custom names to the statusline so they show up on the ruler, you could launch it like this:

$ vimdiff -c "windo setlocal statusline=$file1" <(head -n 1000 "$file1") \
                  +"setlocal statusline=$file2" <(head -n 1000 "$file2")

… though note that spaces in filenames won't work in the statusline (they have to be escaped rather than quoted).

Related