Sorting


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.

Initial buffer content
name,age
tom,19
david,22
bill,17
joe,21
COMMANDTop1:1
:2,$sort

We want to sort the rows in the table, but leave the headings in place, so we use the range 2,$

Default Sort
name,age
bill,17
david,22
joe,21
tom,19
NORMAL33%2:1
 

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:

Sort with ! option
name,age
tom,19
joe,21
david,22
bill,17
NORMAL33%2:1
 

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.

Info

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.

Default Pattern Sort
name,age
bill,17
tom,19
joe,21
david,22
NORMAL33%2:1
 

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:

Pattern Sort with R option
name,age
david,22
joe,21
tom,19
bill,17
NORMAL33%2:1
 

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:

Pattern Sort with non-matching lines
name,age
joe,21
tom,19
bill,17
david,22
NORMAL33%2:1
 

and, for good measure, here is the result when the sort is reversed:

Previous Sort, reversed
name,age
david,22
bill,17
tom,19
joe,21
NORMAL33%2:1
 

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:

Sort with n option
name,age
bill,17
tom,19
joe,21
david,22
NORMAL33%2:1