Vim includes a powerful sorting command
The general pattern is:
:[range]sort [options] [/pattern/]
Range
As with many Vim commands, the :sort command accepts a range. It is often convenient to select the lines in visual mode, which inserts the appropriate range. When no range is specified, sort all lines in the buffer.
Lets see an example of sorting with a range. The following buffer contains a simple csv file with headings.
We want to sort the rows in the table, but leave the headings in place, so we use the range
2,$
By default each line is sorted as a strings, so the lines in the file are now in alphabetical order.
Options
The :sort command accepts a number of options that specify how the sort should behave. Here are a few of the more common options:
Option | Description |
---|---|
! | Sort in reverse order |
i | Case-insensitive sorting |
u | Unique sort. Keep only the first occurrence of identical lines. |
These are used by adding the option after the command. For example, to repeat the previous
example with the !
option:
As expected, the lines are now sorted in the reverse order.
Pattern
Vim supports advanced sorting using patterns to identify which portion of the line should be used as the sort key.
If the pattern is empty (//), Vim sorts according to the last search pattern.
Two sorting strategies are available when a pattern is specified. By default, Vim skips any text that matches the pattern, then sorts each line according to the text that follows the match.
To demonstrate, this pattern matches consecutive letters, followed by a ,
. Following the default
behavior, this will sort each line according to the numbers in the second column.
The second sorting strategy is enabled by including the r
option. When the r
option is
present, each line is sorted according to the text that matches the pattern, which is roughly
the opposite of the default behavior).
In the following example matches two consecutive digits, then sorts the lines in reverse:
Any lines that do not match the pattern are returned in their current order, followed by
sorted matching lines, unless the !
option is used. When the !
option is used the matching
lines are returned first, then the non-matching lines appear in reverse order.
For example, the following matches only lines starting with a, b, c, or d:
and, for good measure, here is the result when the sort is reversed:
Note that the matching lines were sorted in reverse, followed by the non-matching lines but in reverse-order.
Built-in patterns
In a previous example we built a pattern that selected numbers in the lines and sorted based on the numbers. This is such a common sort that Vim includes a few options that automatically apply select a variety of number formats in sorted lines. The following (mutually-exclusive) options are available:
Option | Description |
---|---|
n | Decimal numbers. |
f | Floating point numbers |
x | Hexadecimal numbers, |
o | Octal numbers. |
b | Binary numbers. |
To demonstrate, the following repeats our earlier example with the n
option specified: