I'm reworking a section of logic in some C code to avoid a memory leak that looks like this:
switch (result) {
case CASE_1:
return report(...);
case CASE_2:
return report(...);
/*
* More cases follow with the same pattern.
*/
}
I've used %s/return report/report/
to remove the return
statements, but now I need to add a line with break;
after that, so the final result resembles something like this:
switch (result) {
case CASE_1:
report(...);
break;
case CASE_2:
report(...);
break;
/*
* More cases follow with the same pattern.
*/
}
I've tried using the global
command to execute the append
command with no success:
# Error "E448: Trailing characters" (potential bug in Vim?)
:g/report("/a\
break;
# Appends nothing and does not wait for text to be input.
:g/report("/a
Trying the above in ex mode with Vim fails, and visual mode doesn't seem to work well with the insertion commands in general, regardless of the variant of vi. If I omit the global
command, it will work: I can enter text and type a single "." character on a line by itself to return to command mode (or visual mode, depending on which mode I was in). However, this cannot be repeated using the . key in visual mode because it's an ex command, not a motion command.
In an unusual twist, the first attempt works in ex mode with the port of "The Traditional Vi" and even ed
of all things, though nothing is auto-indented due to the usage of to the global
command. That's fine anyway since the structure is predictable enough to manually insert indentation characters.
I've also attempted the following:
%s/report(".*$/&\
break;/
This is the result of one line as displayed by Vim:
report("No match");^@
break;
Can anybody explain how to use the global
command with the append
command to append one or even more than one line of text in Vim, even if I have to leave visual mode to do it?
g/report("/execute "normal! obreak;\e"
?