Choylton B. Higginbottom
Choylton B. Higginbottom

Reputation: 2304

How to diff line ranges in Vim

I am trying to ascertain if two blocks of code in the same file are identical. Is there a way to diff two line ranges in Vi/Vim?

Upvotes: 6

Views: 1314

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 29260

I would create two empty buffers (:vnew, :new), paste the two line ranges in them and :diffthis them. You could, for instance, define:

:map q :vnew +put!a^M:new +put!b^M:diffthis^M^W<Down>:diffthis^M

If you want to compare two line ranges, select and yank them in registers a and b, respectively, and type q. This will split your window vertically, split again the new window horizontally, paste registers a and b in the two new windows, and diff them.

To simplify a bit you can:

:map q y:vnew +put!a^M:new +put!^M:diffthis^M^W<Down>:diffthis^M

and the second line range will be the one currently selected in visual mode. It saves a few keystrokes.

Upvotes: 4

Related Questions