Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Sort lines by text at position

Tags:

vim

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...

like image 420
576i Avatar asked Nov 26 '17 14:11

576i


2 Answers

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

:sort /\%20v/

from :help :sort

like image 145
Lieven Keersmaekers Avatar answered Oct 19 '22 01:10

Lieven Keersmaekers


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

:sort /^[^|]*/
like image 40
Pierre François Avatar answered Oct 19 '22 02:10

Pierre François