15

Particularly when editing legacy C++ code, I often find myself manually reformatting something like this:

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

to something like this:

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

Is there a builtin command to do this? If not, can someone suggest a plugin or provide some VimScript code for it? (J or gq can reverse the process very easily, so it need not go both ways.)

2
  • This almost seems like a question for superuser
    – Wug
    Oct 4, 2012 at 19:05
  • 1
    @Wug The Vim community is split between the sites. This is a question about a coding-specific use of Vim and thus is quite appropriate for this site, tho of course I could have asked it there too. Oct 4, 2012 at 19:17

3 Answers 3

13

You can use splitjoin.

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

Inside or on brackets, type gS to split. You get:

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

You can also use vim-argwrap

2
  • 4
    Just installed argwrap - it's awesome! :) Nov 18, 2015 at 1:04
  • 1
    Installed both. For comparison, even splitjoin has more contributions but argwrap is better because it works on all filetypes. splitjoin only works on a handful filetypes and it can't wrap in many situations. It relies on too much specific features. I map argwrap to gaw. It has some bugs that if resolved would be a lot better than splitjoin.
    – offchan
    Nov 29, 2016 at 18:03
4

Here's what I've put in my .vimrc. It is more flexible than @rbernabe's answer; it does the formatting based on the cinoptions global settings and simply breaks on commas (and so can be used on more than functions, if needed).

function FoldArgumentsOntoMultipleLines()
    substitute@,\s*@,\r@ge
    normal v``="
endfunction

nnoremap <F2> :call FoldArgumentsOntoMultipleLines()<CR>
inoremap <F2> <Esc>:call FoldArgumentsOntoMultipleLines()<CR>a

This maps F2 in normal and insert mode to doing a search and replace on the current line which converts all commas (with 0 or more spaces after each) into commas with a carriage return after each, then selects the whole group and indents it using the Vim builtin =.

A known shortcoming of this solution is for lines that include multiple template parameters (it breaks on their commas as well instead of just the commas of the normal parameters).

2

I would set a register to a preset macro. After some tests I got the following:

let @x="/\\w\\+ \\w\\+(\nf(_:s­\\(\\w\\+\\)\\@<=,/,\\r            /g\n"

With this line in your vimrc, you can format the methods by executing the macro x: @x with the cursor above of the line you want to format. It adds 12 spaces for indentation so given:

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

After executing the macro: @x You get

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

If you are in the line of the function definition you could just do a replacement:

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

Which is easy to put it in a mapping:

nmap <F4> :s/\(\w\+\)\@<=,/,\r            /g<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.