Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM Removing duplicate lines [duplicate]

Tags:

vim

I have an issue regarding editing strings using VIM, I currently have the following strings:

The red fox.

The red fox.

The blue fish.

The blue fish.

Question: how do I remove the duplicates? and lets say that I have a minimum of 500 lines with duplicates

Expected result:

The red fox.

The blue fish.

like image 630
Francis Padron Avatar asked Apr 02 '15 16:04

Francis Padron


2 Answers

If you do not need to preserve the order of the lines you can do:

:sort u

i.e. sort all lines and remove duplicates.

like image 62
heijp06 Avatar answered Sep 19 '22 03:09

heijp06


If you don't want to sort lines:

:g/^\(.*\)$\n\1/d

Explanation:

:g/^\(.*\)$\n\1/d
 g/            /d   # Delete the lines matching the regexp
   ^\(.*\)$         # Match a whole line and put it in substitution register 1
           \n\1     # Match substitution register 1 preceded by new line
like image 32
svlasov Avatar answered Sep 21 '22 03:09

svlasov