Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diff line ranges in Vim

Tags:

vim

vi

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?

like image 995
Choylton B. Higginbottom Avatar asked Sep 26 '22 17:09

Choylton B. Higginbottom


1 Answers

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.

like image 146
Renaud Pacalet Avatar answered Sep 29 '22 07:09

Renaud Pacalet