有没有办法使用Vim自动重新设置大括号? [英] Is there a way to reformat braces automatically with Vim?
问题描述
if(cond){
foo;
$ / code>
到
if(cond)
{
foo;
$ b由于这是C代码,我一直在寻找
cindent
/cinoptions
与=
一起使用,但它似乎不处理多行规则。
我一直在寻找
formatoptions
来与gq $ c一起使用
那么可以使用默认的Vim选项,还是应该使用特定的插件或函数?
解决方案
:%s / ^ \(\s * \)。* \ zs {\s * $ / \r\1 {/
细分:
^ \(\ s * \)
=捕获行开头的空格
$ b $。*
=其他所有元素
\ zs
=在此之后开始替换
$ b $= =打开大括号 p>
\s * $
=行结束之前的空白符
\r\1 {
=换行符,捕获的空格,大括号I would like to reformat some code which looks like this :
if (cond) { foo; }
to
if (cond) { foo; }
Since this is C code, I have been looking at
cindent
/cinoptions
to use with=
but it seems it does not deal with multiline rules.I have been looking at
formatoptions
to use withgq
, and it does not seem to be possible either.So is it possible using default Vim options or should I use a specific plugin or function ?
解决方案:%s/^\(\s*\).*\zs{\s*$/\r\1{/
Breakdown:
^\(\s*\)
= capture the whitespace at the beginning of the line
.*
= everything else
\zs
= start replacement after this
{
= open curly brace
\s*$
= trailing whitespace before line end
\r\1{
= newline, captured whitespace, brace这篇关于有没有办法使用Vim自动重新设置大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!