I am looking for a way in VIM to delete all lines that are duplicates and only leave unique lines that exist in the file. I would prefer a macro but a command or function would be great.
Say I have a file that has duplicate lines and some uniques:
1Apple
1Apple
2Peach
2Peach
2Peach
3Beer
4Banana
4Banana
4BananaI want to delete all lines so that all that is left:
3BeerThe one unique line that I REALLY want.
I use sort u all the time to get a unique list by deleting duplicates but I have times I just need the unique line in the file.
Any ideas how to handle this case in VIM?
14 Answers
You can do this with the substitute command and a backreference:
s/^\(.*\)\(\n\1\)\+//Note that this only works if the lines are sorted. This works by matching and removing the first group one or more times on the next line(s).
1You could use the "uniq" command to do this -
uniq -u filename-to-check.txt2
Select all the lines you want to filter.
Do
:'<,'>!uniq -u<CR>, the'<,'>range is added automatically for you.
If you want to filter the whole buffer, don't select anything and simply do :%!uniq -u<CR>.
You can read about filters in :h filter.
You can invoke the uniq tool as a filter directly from Vim:
:!%uniq -u