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:
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:
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).
Sort in reverse
Sort, removing duplicate lines
Sort using the external Unix sort utility, respecting month-name order
("respecting month-name order" means January < February < ... < December)
(this way, 100 doesn't precede 20 in the sort)
Sort subsections independently, in this example sort numbers between "start" and "end" markers
:g/start/+1,/end/-1 sort n
- 374 Use filter commands to process text
- 588 How to sort using visual blocks
- 758 Search and sort by selection
- 800 Sorting lines in a file based on the number of words in each line
- 923 Sort lines by a specified word number
- 667 Working with CSV files sort by CSV field
- 128 Use Unix command-line tools in Windows links to download GNU sort for Windows
- 648 Uniq - Removing duplicate lines techniques to remove duplicate lines
- Probably need some general :sort command info.
- 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.
- If we're going to mention an external sort tool, we may as well include the following with a brief explanation. Vim could do this, but only with a complex regex. Or perhaps better, mention it in VimTip374 or VimTip923 in "see also". -k2 sorts on the second field (word by default).