Vim: source code formatting

前端 未结 3 690
天涯浪人
天涯浪人 2020-12-10 16:10

Take a look at the enum:

enum TestEnum
{
    First = 1,
    Second = 2,
    Unknown = 3,
    TestTestTest = 100,
    Zero = 0,
    Foo = 123,
}
相关标签:
3条回答
  • 2020-12-10 16:25

    You can harvest from two plug-ins that can do this stuff:

    • Align.vim , or
    • Tabular.vim
    0 讨论(0)
  • 2020-12-10 16:38

    The Align.vim plugin is probably the way to go, but if you wish to have it handy on a standard installation, you could always filter through awk to get some generic functionality with not too much work.

    For TestEnum you would do something like

    '<,'>!awk '{printf "^I\%-20s\%-20s\%-20s\n", $1, $2, $3}'
    

    after visually selecting the braced contents (viB is awesome here.)

    For Foo you would do

    '<,'>!awk '{printf "^I\%-20s\%-20s\n", $1, $2}'
    

    You could probably make it variable width with an awk for-loop but at the cost of the easy and fast to type version here.

    If you have the unix utility col handy, you might simply try

    '<,'>!col -x
    

    But here your mileage will really vary, as this is not the intended use of the utility.

    0 讨论(0)
  • 2020-12-10 16:48

    Align or Tabular sound like the way to go, but I will also mention the Unix utility column, which is pretty nifty and more people should know about.
    Unix-specific, obviously. (On openSuSE 12.3, it's in the util-linux package; likely different on other distributions.)
    To invoke it within vim, visually select the lines you want to align, then
    :!column -t
    So with the visual range that vim fills in for you when you hit : with lines selected, you get:
    :'<,'>!column -t
    (By default it separates on whitespace, but you can change that with the -s <separator> option.)
    It aligns things such that each column is just long enough for its longest member.

    0 讨论(0)
提交回复
热议问题