Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort in vim starting from some column/character

Tags:

vim

How to sort following text starting from column 5:

123456789
000 123
013 122 
122 013
123 000 

I want to get this:

123 000 
122 013
013 122 
000 123
123456789
like image 620
Robert Lujo Avatar asked Jan 07 '15 02:01

Robert Lujo


1 Answers

The following vim command helped me:

:sort /\%5v/

Explanation

besides some plain options (like u,i,!,n) sort can receive regular expression /{pattern}/. In this case there are two options:

A. Sort by skipping matched

default - with no [r] flag specified - in this case for each line the text matched with {pattern} is skipped, so sort is done on what comes after the match.

Examples from documentation:

A1. Example - sort starting from virtual column 5 our case - sort on the text at virtual column 5 (thus ignoring the difference between tabs and spaces):

:sort /.*\%5v/

A2. Example - sort on the second comma-separated field

The logic is: skip text until first comma is found:

:sort /[^,]*,/

B. Sort only matched - [r] flag specified

i.e. sorting is done on the matching {pattern} instead of skipping past it as described above ... to sort on only the first three letters of each line:

:sort /\a\a\a/ r

Reference

Please check :help :sort for more details/options

like image 112
Robert Lujo Avatar answered Sep 18 '22 00:09

Robert Lujo