是否可以使用 VIM 格式化 C++ 代码? [英] Is it possible to format C++ code with VIM?
问题描述
我对 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)
这篇关于是否可以使用 VIM 格式化 C++ 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!