有没有办法使用Vim自动重新设置大括号? [英] Is there a way to reformat braces automatically with Vim?

查看:379
本文介绍了有没有办法使用Vim自动重新设置大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新格式化一些如下所示的代码:

  if(cond){
foo;

$ / code>

 if(cond)
{
foo;





$ b

由于这是C代码,我一直在寻找 cindent / cinoptions = 一起使用,但它似乎不处理多行规则。

我一直在寻找 formatoptions 来与 gq

那么可以使用默认的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 formatoptionsto use with gq, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆