How to sort methods into alphabetical order in VIM?

眉间皱痕 提交于 2020-01-03 18:54:13

问题


class MyClass

  def zzz
    # method body
  end

  def aaa
    # method body
  end

end

How would I get VIM to organise these into alphabetical order such that the definition of aaa precedes the definition of zzz?


回答1:


Well you can set up code folding e.g. by indentation, and close those functions/fold, then do it manuall with dd then p but that is not an ideal solution with a large file.

It's not an easy task. You can set up some line-joining, e.g.:

  1. put some specific comment/identifier before every first level def, like

    # DEFINITION

    def zzz

    ...

    end

    # END DEF

  2. then join those lines into one with some multiline regex magic (and/or column editing), using some placeholder which generally does not occur in your code.

  3. then sort it via standard unix sort (e.g. visually select your joined lines, then

    :'<,'>!sort

  4. then split on the inserted pattern...

Still less than ideal, but it can be done...

See this answer too.



来源:https://stackoverflow.com/questions/9064425/how-to-sort-methods-into-alphabetical-order-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!