How to sort in vim starting at some column / character

How to sort the following text starting at column 5:

123456789
000 123
013 122 
122 013
123 000 

      

I want to get the following:

123 000 
122 013
013 122 
000 123
123456789

      

+3


source to share


2 answers


The following vim command helped me:

:sort /\%5v/

      

Description

besides some simple options (for example, u, i,!, n) the sorting can get the regular expression / {pattern} /. In this case, there are two options:

A. Sort by Skip

by default - without the [r] flag set, in this case, for each line, the text matched with the {pattern} is skipped, so sorting is performed by what happens after the match .

Examples from the documentation:

A1. Example - sorting starting from virtual column 5 our case is sorting by text in virtual column 5 (thus ignoring the difference between tabs and spaces):

:sort /.*\%5v/

      



A2. Example - sorting by second comma separated field

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

:sort /[^,]*,/

      

B. Sort only matched - specified flag [r]

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

:sort /\a\a\a/ r

      

Link

Please check : help: sort for more details / options

+6


source


... The following also works, which basically sorts the 5th column



:sort /.*\%5v/

0


source







All Articles