How to sort methods alphabetically in VIM?

class MyClass def zzz # method body end def aaa # method body end end 

How can I get VIM to arrange them alphabetically so that the aaa definition precedes the zzz definition?

+4
source share
1 answer

Well, you can configure code folding, for example. indent, and close these functions / fold, then do it with d d , then p , but this is not an ideal solution with a large file.

This is not an easy task. You can configure some line connections, for example:

  • put some specific comment / id in front of every first def level, e.g.

    # DEFINITION

    def zzz

    ...

    end

    # END DEF

  • then attach these lines to one with some multi-line magic of regular expressions (and / or column editing) using some kind of placeholder that is not usually found in your code.

  • then sort it by standard unix sort (e.g., select the concatenated lines visually, then

    :'<,'>!sort

  • then split into an inserted template ...

Even less ideal, but it can be done ...

See this answer too .

+4
source

Source: https://habr.com/ru/post/1393760/


All Articles