adeluccar
adeluccar

Reputation: 152

How to sort based on number of specific characters in vim?

I have a file where certain lines are tagged with multiple pipes for relative importance. How would you sort these lines in vim based on importance and also alphabetically?

Input

cli
bar ||
bar
foo |||
haz ||

Output

foo |||
bar ||
haz ||
bar
cli

Note that bar and haz are sorted by number of pipes but also alphabetically. Thanks!

Upvotes: 2

Views: 824

Answers (2)

Caek
Caek

Reputation: 592

If you have access to the external linux sort command, you can run this inside vim:

:%!sort -k2r -k1,1

This sorts the second column in reverse order and if there are duplicates in the second column, then sort by the first column in ascending order.

Upvotes: 1

Kent
Kent

Reputation: 195169

with vim built-in :sort, you can do:

:sort! r /|*$/ 

If you want to sort by importance and alphabet, you can:

:sort|sort! r/|*$/

Note your output doesn't follow the rule you described.

Upvotes: 6

Related Questions