Reputation: 9
in Vim, how do I delete body of all functions with regex?
Example in PHP:
function myfunc($arg1, $arg2) {
$x = 1;
while ($x < 10) {
echo 'abcd';
$x++;
}
return $x;
}
Upvotes: 0
Views: 652
Reputation: 957
This can be a bit difficult to do with just a regex. However, it is easy with a macro.
First, find all beginning braces:
:map q /^function.*^V^[f{
In this macro, the "^V" is control-V and is used to escape the next control character entered which could be "Esc" (the escape character) or the newline character used to submit a search pattern. I prefer using escape which is displayed as "^[". But note, the "^" at the beginning of the search pattern is the literal "^" -- the one caret character... The "f{" is simply "find the next character given on the line following the cursor." In this case, find the "{".
Try this out by tapping the "q" key repeatedly in edit mode. It should move the cursor to the opening brace of each PHP function.
Now, delete the body of the function with "d%" which is "delete to matching brace/bracket/parenthesis." This can be added to a new macro:
:map Q qd%
Tapping "Q" in edit mode will now delete each function body in sequence.
To apply this macro to every "line" in the file, use:
:%norm Q
Use "u" in edit mode to back out any mistakes you or I made...
Upvotes: 0
Reputation: 792
You can use the following command for deleting the bodies of all functions:
:g/function/normal jdi}
Here is the explanation of the normal command
jdi}
g/searchterm/normal normal_command
to execute normal command on all the matches of a search term, the above method can be used.
Upvotes: 4
Reputation: 5112
Are you sure you want to do it with a regular expression? For a single function, I would start on the function
line, use $ to move to the end of the line, v to enter (character-wise) Visual mode, % to go to the matching brace, then c{}<Esc>
to replace it. Put the closing brace on a new line if you like. In short, $v%c{}<Esc>
.
If you want to do it to all functions in your file, try
:g/^\s*function\>/execute "normal! $v%c{\n}\<Esc>"
I assume that any line starting with "function" (and optional leading white space) starts a function, and that the opening brace is at the end of that line. If your code is not so uniformly formatted, then you have to work harder; there are limits to what you can do with regular expressions!
I used :execute
in order to avoid non-ASCII characters, and I added ! to the :normal
command just in case there are some mappings that I do not want triggered. The \n
is optional.
If you really want to do it with regular expressions alone, then I hope you never have a closing brace in the first column except when it is closing a function. Something like
:g/^function\>/+,/^}$/-d
should do it.
Maybe the entire function is indented (because it is a class method, for example). Assuming that the opening and closing lines are indented the same, then you could capture the leading whitespace, but it would be messy. You cannot simply use a back-reference, since the two search patterns are independent.
Upvotes: 0
Reputation: 754665
Here is one way
d/^}/e<Enter>
This will delete till the next }
(inclusive) which occurs at the start of the line
If you want to include the entire line that contains the closing }
you can adjust it as follows
d/^}/+0<Enter>
Upvotes: 0