Vim Tips Wiki
Advertisement
Tip 1166 Printable Monobook Previous Next

created 2006 · complexity basic · author Robert Stovall · version 7.0


Vim has a very powerful built-in sort utility, or it can interface with an external one. In order to keep only unique lines in Vim, you would:

:{range}sort u

Yes, it's that simple.

You could create a range in advance, such as 'a,. (from mark 'a' to the current line) or you could create one on-the-fly using visual selection by pressing ':' in visual mode, after selecting the text you wish to sort, to get a range of '<,'> on the command line.

If you like using an external sort utility instead, you can do it just as easily. For example, Unix sort, removing duplicate lines:

:{range}!sort -u

Many other systems also have an external sort utility, but the options and capabilities will differ. It is probably better to use the built-in Vim sort unless you are looking for a specific feature of the external sort (or using an old Vim without the :sort command).

See also

References

Comments

I tried merging all the relevant comments into the tip, but was a little confused as to the original tip purpose. Does "keep unique lines" mean "remove duplicate lines" or "keep all lines, even if they are unique"? I went with the former meaning. Something I could not figure out what to do with is the following:

:!sort -k2

and also the numerical sort option.

I think we should probably just leave them out, because they have nothing to do with the tip title.

Also,

Just to point out that sort is a *nix utility. Windows users will need to get sort.exe from http://www.cygwin.com/ or http://unxutils.sourceforge.net/

Maybe we should mention these links somewhere...but are they really needed? The purpose of the tip is easily accomplished through Vim's internal sort.

--Fritzophrenic 22:42, 11 December 2008 (UTC)


Let's make this tip our "sort" tip and omit "unique" from the title. How about renaming to Sorting lines?

We could make redirects 'sort' and 'sorting' which point to this tip. Similarly, we could make redirects 'unique' and 'uniq' which point to 648 Uniq - Removing duplicate lines.

 TO DO 

  • Rename tip and make redirects as above.
  • Give some general :sort command info.
  • Give specific examples of "{range}" (and how you can select some lines, then sort them).
  • Give examples of unique sort (despite the duplicate in VimTip648).
  • Give examples of numeric sort and using regex sort.
  • Clean up my "see also" list. It's useful now for a comprehensive list of related tips, some of which need work. At least should add a note on point of tip.

Re Fritzophrenic's comments:

  • Traditionally, I would interpret the current title to mean "keep only unique lines".
  • Yes, let's omit :!sort -k2 (or perhaps better, mention it in VimTip374 or VimTip923 in "see also"). -k2 sorts on the second field (word by default).
  • Do not mention GNU win32 sort in this tip (but leave VimTip128 in "see also").

--JohnBeckett 08:32, 12 December 2008 (UTC)


Advertisement