Is it possible to format C++ code with VIM?

后端 未结 9 1642
野的像风
野的像风 2020-12-07 14:58

I am rather new to VIM. I got some source code and this is a mess. At a first sight I would like at least to get a clear and organised view of the code, so I like to get it

9条回答
  •  青春惊慌失措
    2020-12-07 15:40

    While vim is a true Swiss-knife I still prefer external tools for some jobs. This approach is some times much more intuitive and easy to remember than using the built-in equivalent.

    In the case of indenting, I filter the whole file buffer through astyle. The astyle parameters are much easier to grasp in a couple of minutes, especially if you are not a vim guru. Also astyle provides much more flexibility in fine-tuning the output.

    First install astyle:
    # apt-get install astyle

    Then inside vim:
    :%!astyle (simple case - astyle default mode is C/C++)
    or
    :%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)
    or
    :1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40)

提交回复
热议问题