Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diff two lines in an open file in vim?

Tags:

vim

I occasionally see very long lines in my code that I need to check if they are the same. Is there a way in vim to select two lines and diff them to show any differences between the two?

For example, given the two lines in vim:

AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(That *is, Overloaded *with, Multiple *different, Parameter *lists); AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(That *is, Overloaded *with, Multiple *different, Parameter *1ists); 

I would like vim to tell me that the two lines are in fact different because each spells "lists" differently. Is this possible, and if so, how do I do it?

like image 437
Cory Klein Avatar asked Apr 01 '11 21:04

Cory Klein


People also ask

How do you get to the next difference in Vimdiff?

You can jump to the "next difference" ( ] c ), but this will jump to the next line with a difference.

What is Vimdiff?

Vimdiff is a Linux command that can edit two, three, or four versions of a file with Vim and show their differences.


1 Answers

A quick and dirty solution is to just select both lines and sort them while removing duplicates:

  • select lines
  • ":sort u"
  • if only one line remains, both were equal
  • if both remain, there most be some difference

An undo recovers everything again.

like image 130
jelsayeh Avatar answered Sep 17 '22 14:09

jelsayeh