Align text on an equals sign in vim

后端 未结 9 2445
一生所求
一生所求 2020-12-12 22:48

I tend to align code on equal signs for better readability. From this:

$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main
         


        
9条回答
  •  时光说笑
    2020-12-12 22:54

    For a simple solution that does not involve installing a plugin, just filter through the Unix column command.

    Note there are two ways to do this depending on whether your column command supports -o.

    GNU column command (Linux etc)

    :% ! column -t -s= -o=
    

    That's it.

    BSD column command (Mac OS X etc)

    Step one, filter through column -t:

    :% ! column -t
    

    Step two, remove the padding around the delimiter:

    :%s/ = /=/
    

    Initial text is

    $ = jQuery.sub()
    Survey = App.Survey
    Sidebar = App.Sidebar
    Main = App.Main
    

    After step one it becomes

    $        =  jQuery.sub()
    Survey   =  App.Survey
    Sidebar  =  App.Sidebar
    Main     =  App.Main
    

    And after step two

    $       = jQuery.sub()
    Survey  = App.Survey
    Sidebar = App.Sidebar
    Main    = App.Main
    

    Or, if you want to do it in one step:

    :% ! column -t | sed 's/ = /=/'
    

    For more info, man column.

提交回复
热议问题