Sorting lines in Vim
Amongst Vim’s many commands, there’s the humble yet powerful
:sort. As you’d expect, it takes a selection or a range and sorts it. By default, that sort is numerical (``through
9), then alphabetical (
All that to say that running a generic
:sort on the characters to the left in the following example would result in them being reordered to how you see them on the right.
1Original Sorted2 a => 23 B => B4 c => a5 2 => b6 b => c
That’s ever so close to what I’d like. I’d rather it ignore the case of the letters when sorting alphabetically however; something that can easily be done by passing
i into the command.
:sort i results in the following.
1Original Sorted2 a => 23 B => a4 c => B5 2 => b6 b => c
99% of the time,
:sort i is precisely what I’m after. As it’s functionality I use multiple times a day, I have it remapped.
<leader>s works well for me.
1xnoremap <leader>s :sort i<cr>
Now for the lesser used, yet still occasionally useful sort options.
:sort! i (no space between the command and the
!) will sort the selection in the reverse order:
1Original Sorted2 a => c3 B => b4 c => B5 2 => a6 b => 2
:sort u will remove any duplicate lines:
1Original Sorted2 a => 23 B => B4 b => a5 c => b6 2 => c7 b =>
:sort n will sort lines based on the first decimal number in the line (use
f for a float). In it’s most basic use case (the example below), it will look at the entire value of the number, rather than just the first digit in the number. Otherwise,
55 would be sorted before
1Original Sorted2 22 => 13 1 => 54 42 => 85 5 => 226 8 => 427 80 => 80
It gets better though, as running
:sort n on lines that contain numbers will result in them being ordered based on the first number in the line, no matter where in the line that number is.
1Original Sorted2The 5 quick brown foxes => jumped over the 2 lazy dogs3jumped over the 2 lazy dogs => at 4 o'clock in the afternoon4at 4 o'clock in the afternoon => The 5 quick brown foxes
That concludes coverage of the most useful
:sort commands, but not all of them.
:help sort has explanations of the rest, should you be curious.