smarber
smarber

Reputation: 5084

vim sort based on given column

Suppose I have this section in my yaml file

hdkkhj50599: 1000000  
504sdaaf54: 00000054  
pppppp: 00005464

I want to sort these lines based on the values after colons.

None of the suggested solutions in stackoverflow worked for me.

How do I do it?

Upvotes: 2

Views: 3746

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172648

Vim's internal :sort provides options for what you want: the n flag is for sorting decimal numbers (instead of alphabetially, though it wouldn't matter in your 0-padded example), a /.../ pattern is skipped at the beginning; we non-greedily match up to the first colon and whitespace:

:sort n/.\{-}: /

Upvotes: 8

With vim sorting command:

  • :sort /^.\{-}:/

You can sort them by using linux command:

  • :%!sort -t: -k2

For more see :help sort

Upvotes: 2

wwn
wwn

Reputation: 593

In case you're on linux you can try the following in vim (will call external sort command)

:%!sort -g

Obviously you can replace % with the definition of your lines-range e.g: for lines 5 to 10 you would do:

:5,10!sort -g

Upvotes: 0

Related Questions