1

I am looking for a way how to transfer this (and similar) block of text:

dollar_value: str = field(init=False)
dollar_symbol: str = field(init=False)
dollar_unit: str = field(init=False)
euro_value: str = field(init=False)
euro_symbol: str = field(init=False)
euro_unit: str = field(init=False)
bitcoin_value: str = field(init=False)
bitcoin_symbol: str = field(init=False)
bitcoint_unit: str = field(init=False)
ether_value: str = field(init=False)
ether_symbol: str = field(init=False)
ether_unit: str = field(init=False)
litecoin_value: str = field(init=False)
litecoin_symbol: str = field(init=False)
litecoin_unit: str = field(init=False)

into this

dollar_value: str = field(init=False)
euro_value: str = field(init=False)
bitcoin_value: str = field(init=False)
ether_value: str = field(init=False)
litecoin_value: str = field(init=False)
dollar_symbol: str = field(init=False)
euro_symbol: str = field(init=False)
bitcoin_symbol: str = field(init=False)
ether_symbol: str = field(init=False)
litecoin_symbol: str = field(init=False)
dollar_unit: str = field(init=False)
euro_unit: str = field(init=False)
bitcoint_unit: str = field(init=False)
ether_unit: str = field(init=False)
litecoin_unit: str = field(init=False)

I though maybe in this particular case a retrograde sorting macro would be helpful (like normal sorting, but starting with the last letter of the word or line, progressing towards the first letter.

Is there a way to do retrograde sort in vim? Or what would be a possible approch to this?

3
  • 2
    Does :sort /.\{-}_/ work? *nix sort will let you specify delimiters and fields, but so will vim's if you're careful
    – D. Ben Knoble
    Commented May 1, 2020 at 19:04
  • Thank you, that actually works pretty good! I did not know these tricks. What does it exactly do? Is it like a regular expression which says start sorting after the minimal amount of random characters before "_"? Commented May 2, 2020 at 4:41
  • 1
    It’s similar to filbranden’s answer, but yes! I was being a little too exact with my pattern.
    – D. Ben Knoble
    Commented May 2, 2020 at 12:08

1 Answer 1

3

Vim's :sort command can take an optional regex to indicate where to start sorting.

From :help :sort:

When /{pattern}/ is specified and there is no [r] flag the text matched with {pattern} is skipped, so that you sort on what comes after the match.

So this should be enough to sort lines, considering the text coming after the underscore:

:sort /_/

The sort is stable, so it will sort all "symbols", followed by all "units" and then all "values", preserving their relative order, which is the expected result you wanted.

It seems you wanted "values" first, but that should be a simple operation of cutting them from the end of the list and pasting them back at the beginning, getting to exactly the result you wanted.

1
  • 1
    Nice follow-up :)
    – D. Ben Knoble
    Commented May 1, 2020 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.