如何避免 vim 中命名空间内容缩进?
如何设置 vim 在 C++ 中不缩进命名空间内容?
namespace < identifier >
{
< statement_list > // Unwanted indentation
}
令人惊讶的是, 'cinoptions' 没有提供编辑命名空间内容的方法缩进。
How to set vim to not indent namespace content in C++?
namespace < identifier >
{
< statement_list > // Unwanted indentation
}
Surprisingly, 'cinoptions' doesn't provide a way to edit namespace content indentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不确定它是什么时候引入的,但我安装的 vim 版本 v7.3.353 有一个 cino 选项,可以显式处理 cpp 命名空间。我目前正在使用示例值:
cino=Ns
并按照
:help cinoptions-values
OP 发布的链接适用于 v7.3.162
Not sure when it was introduced but my installed version of vim, v7.3.353 has a cino option that handles cpp namespace explicitly. I am currently using the example value:
cino=N-s
and as per
:help cinoptions-values
The link the OP posted is for v7.3.162
cpp.vim 会解决你的问题,但如果你不想成熟的 Google 编码风格,然后只需看一下插件源代码,看看它如何处理名称空间。它非常简单:
本质上,您所做的就是将最后一个非空行与
/^\s*namespace/
进行匹配,如果匹配则返回 0(作为indentexpr< 的缩进位置) /代码>);否则返回 Vim 内置的 cindent 机制的值。
我本质上是从插件中窃取了代码,删除了与命名空间无关的所有内容,并将缩进函数重命名为
IndentNamespace()
。将其保存为 ~/.vim/indent/cpp.vim。cpp.vim will solve your problem, but if you don't want the full-blown Google coding style then just take a peek at the plugin source and see how it handles namespaces. It's super simple:
In essence all you do is match the last non-blank line against
/^\s*namespace/
, and if it matches return 0 (as the indent position forindentexpr
); otherwise return Vim's builtin cindent mechanism's value.I essentially stole the code from the plugin, stripped anything that isn't namespace-related and renamed the indent function to
IndentNamespace()
. Save this as ~/.vim/indent/cpp.vim.正如许多人提到的,Google C++ 风格非常好。
我建议安装
clang-format
,我发现它比推荐的插件更好,然后在上面安装一个 vim 插件。安装 clang-format
Ubuntu:
Mac:
Vim 插件:
rhysd/vim-clang-format
安装使用您最喜欢的插件管理器。
查看插件的链接以获取更多选项。默认样式是
google
,但也有llvm
等。例如,如果您使用命令的自定义版本,则可能需要的一个选项是
:
As many have mentioned Google C++ style is quite good.
I 'd recommend to install
clang-format
which I find it better than the recommended plugins, and then install a vim plugin on top.Install clang-format
Ubuntu:
Mac:
Vim plugin:
rhysd/vim-clang-format
Install using your favorite plugin manager.
Checkout the plugin's link for more options. The default style is
google
, but there is alsollvm
and others.One option you might need if you are using a custom version of the command is for
example:
我使用 cpp.vim ,它的灵感来自于 Google C++ 风格指南。除此之外,该脚本可以满足您的要求。
I use cpp.vim which is inspired by the Google C++ Style Guide. Among other things, that script does what you're asking.
我已将 vim 配置为不缩进命名空间。这些是我的 vimrc 中的相关行:
坦率地说,我不记得如何解释
cino
语句,但是:help cinoptions
应该有助于破译它。一个警告:我认为它被配置为在使用如下格式时不缩进:vs
因为我将大括号放在下一行专门用于命名空间,所以它可以满足我的要求,但如果您将该样式用于其他格式,它可能不起作用函数声明、for 等
I've configured vim to not indent for the namespace. These are the relevant lines in my vimrc:
Frankly, I don't remember how to interpet the
cino
statement, but:help cinoptions
should help in deciphering it. One caveat: I think it's configured to not indent when using a format like so:versus
Since I put the curly bracket on the next line exclusively for namespaces, it does what I want, but it might not work if you use that style for other function declarations, for, etc.