在Vim中轻松地将函数参数重新格式化为多行 特别是在编辑遗留C++代码时,我经常发现自己手工重新格式化了这样的东西: SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree); SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree);

在Vim中轻松地将函数参数重新格式化为多行 特别是在编辑遗留C++代码时,我经常发现自己手工重新格式化了这样的东西: SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree); SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree);,vim,formatting,arguments,Vim,Formatting,Arguments,对这样的事情: SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree); SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree); 是否

对这样的事情:

SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree);
SomeObject doSomething(firstType argumentOne,
                       secondType argumentTwo,
                       thirdType argumentThree);

是否有内置命令来执行此操作?如果没有,有人能推荐一个插件或提供一些VimScript代码吗?(
J
gq
可以很容易地反转过程,因此它不需要双向进行。)

我会将寄存器设置为预设宏。经过一些测试,我得到了以下结果:

let @x="/\\w\\+ \\w\\+(\nf(_:s­\\(\\w\\+\\)\\@<=,/,\\r            /g\n"
执行宏后:
@x
您将获得

SomeObject doSomething(firstType argumentOne,
             secondType argumentTwo,
             thirdType argumentThree);
如果您在函数定义行中,则只需进行替换:

:s\(\w\+\)\@,<=,/,\r            /g

:s\(\w\+\)\@,以下是我在
.vimrc
中输入的内容。它比@rbernabe的答案更灵活;它根据
cinoptions
global设置进行格式化,只需在逗号上断开(因此,如果需要,可以在多个函数上使用)

函数FoldArgumentsOntoMultipleLines()
替换@,\s*@\r@ge
正常v``=”
端功能
nnoremap:调用foldargumentsonotomultiplelines()
inoremap:调用foldargumentsonotomultiplelines()a
这将正常和插入模式下的F2映射为在当前行上执行搜索和替换,将所有逗号(每个后面有0个或更多空格)转换为逗号,每个逗号后面有回车符,然后选择整个组并使用Vim内置
=
缩进

此解决方案的一个已知缺点是,对于包含多个模板参数的行(它也会打断它们的逗号,而不仅仅是普通参数的逗号)。

您可以使用

在括号内或括号上,键入要拆分的
gS
。您将得到:

SomeObject doSomething(firstType argumentOne,
    secondType argumentTwo,
    thirdType argumentThree);

您也可以使用

这几乎像是一个问题superuser@WugVim社区分为两个站点。这是一个关于Vim的特定编码使用的问题,因此非常适合这个站点,当然我也可以在那里问。刚刚安装了argwrap-太棒了!:)两个都安装了。为了比较,即使是splitjoin贡献更多,但argwrap更好,因为它适用于所有文件类型。splitjoin只适用于少数文件类型,在许多情况下无法包装。它依赖太多的特定功能。我将argwrap映射到
gaw
。它有一些错误,如果得到解决,将比splitjoin好得多。
SomeObject doSomething(firstType argumentOne, secondType argumentTwo, thirdType argumentThree);
SomeObject doSomething(firstType argumentOne,
    secondType argumentTwo,
    thirdType argumentThree);