是否可以使用VIM格式化C ++代码?

问题描述:

我对VIM比较陌生。我有一些源代码,这是一团糟。乍一看,我至少希望对代码有一个清晰而有条理的了解,所以我想对它进行正确的格式化,我的意思是缩进取决于函数的深度等等。

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 rightly formatted, I mean indented depending on the depth of the functions and so.

我想知道是否可以使用VIM完成此操作,否则您可以推荐使用哪些其他命令行工具。

I wonder if it can be done with VIM, and otherwise which other commandline tools for that can you recommend.

谢谢

虽然vim是真正的瑞士刀,但我仍然偏爱使用外部工具来完成某些工作。与使用内置等效方法相比,这种方法有时更直观,更容易记住。

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.

在缩进的情况下,我通过astyle过滤了整个文件缓冲区。 astyle参数在几分钟之内就更容易掌握,尤其是如果您不是vim专家。同样,astyle在调整输出时也提供了更大的灵活性。

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.

首先安装astyle:
#apt-get install astyle

First install astyle:
# apt-get install astyle

然后在vim中:

:%!astyle (简单的情况-astyle的默认模式是C / C ++)



:%!astyle --mode = c --style = ansi -s2 (ansi C ++样式,每个缩进级别使用两个空格)



:1,40!astyle --mode = c --style = ansi (ansi C ++样式,仅过滤1-40行)

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)