If the code we have looks like
for(...){
}
after reformatting I’d like it to look like
for(...)
{
}
as well for all functions, methods, classes etc.
I found something similar in other article in stackoverflow but it was a regular expression and needed to type every time in the vim console. And I am looking for something to put in the vimrc file (if possible) and to work every time I open it.
Well this is the one I’ve found:
:%s/^(\s*).*\zs{\s*$/\r\1{/
in http://stackoverflow.com/questions/4463211/is-there-a-way-to-reformat-braces-automatically-with-vim but the thing is it adds a new line even if the bracket is on the right place… and still don’t know how to map it to key combination.
(edited with a more accurate pattern)
This should do the trick:
But it it doesn’t really sound “safe” to me.
Instead, you could do:
which will ask for a confirmation for each match.
Or record a macro and play it back using
:global.edit
Your pattern,
:%s/^(\s*).*\zs{\s*$/\r\1{/, is wrong because:the capture parentheses are not properly escaped,
(\s*)instead of\(\s*\).*would match any number of any character, including0which is why the substitution also works on lines with a single{.