Vim: Sort lines by text at position

Viewed 816

I'd like to use the vim sort command to start sorting at a certain position. Without installing any plugins.

2011-09-17  00:37 |Free|ALL RIGHT NOW
2011-09-17  00:41 |Kim Wilde|CAMBODIA
2011-09-17  00:45 |Take That|NEVER FORGET
2011-09-17  00:53 |Visage|FADE TO GREY
2011-09-17  00:56 |SUTHERLAND BROTHERS & QUIVER|ARMS OF MARY

The command :sort u starts at position 1.

In the example the artist name starts at position 20, so I'd like to start sorting there.

This is the result I'd like to get

2011-09-17  00:37 |Free|ALL RIGHT NOW
2011-09-17  00:41 |Kim Wilde|CAMBODIA
2011-09-17  00:56 |SUTHERLAND BROTHERS & QUIVER|ARMS OF MARY
2011-09-17  00:45 |Take That|NEVER FORGET
2011-09-17  00:53 |Visage|FADE TO GREY

Is this something easily done without plugins or should not attempt that? Google spits out a lot of user defined scripts, but nothing working out of the box...

3 Answers

Out of the box, you can sort starting at a virtual column

:sort /\%20v/

from :help :sort

You can skip the first part of the line (until the "|" char) in your sort by the VIM command:

:sort /^[^|]*/

If you really want to sort from position 20, use the next syntax:

:sort /^.\{19}/

Mind the backslash before the opening bracket and the character to be repeated must be before the quantifier.

Related