views:

298

answers:

5

I am using the vim editor and I wanted to empty out all methods in the class, for instance

class A {

     public:
        int getNumber() const {
             return number;
        }

        void setNumber(int num) {
             number = num;
        }


};

I wanted to have a regex where I could substitute so that the result would be this

class A {

     public:
        int getNumber() const;

        void setNumber(int num};


};

I know I can use %s for global subtitution but getting everything under the method body is what I am looking for.

A: 

I think this is something that is best done using recorded macros and not regexes. Sorry I don't have time to create one for you case. To get started see:

:help q
Peter Severin
I will check this out thanks
kal
+2  A: 

Since braces may of course nest within your functions, it's unfortunately impossible to create a regular expression which could match the corresponding ending brace.

Fortunately, VIM does have the move-to-corresponding (which will move forward to the next brace/bracket/etc. and jump to the corresponding opening/closing one), using the %-key (shift-5 on most keyboard layouts I believe?), so basically, standing on the space before the opening brace, c%;<ESC> would replace the entire body with a semi-colon.

Finding the opening brace of a function is an entirely different matter, and I'll see if I can come up with something for you, but to get you going, execute it once and then just keep hitting . (period) to execute it again for every function.

roe
+1  A: 

I don't know that regular expressions can match over multiple lines. Peter may be right to recommend a macro. Something like this on the first method may do the trick:

[ESC]qa$d%i;0[ESC]q

Then on the first line of the other methods, you can do this:

[ESC]@a

Here is how it works:

  • [ESC] - the escape key puts you in the correct mode
  • qa - save the next commands as a macro named "a"
  • $ - jump to the end of the line
  • d% - delete to the matching bracket
  • i; - insert a semicolon
  • [ESC] - the escape key puts you in the correct mode
  • q - stop recording the macro


  • @a - calls the macro again (on the line for the next method)

I hope this helps.

gpojd
d%i; should be abbreviated c%; starting at the space before the brace, otherwise you'll get a space at the end of each line (not terribly wrong but perhaps a bit inelegant), or you can use d%a;
roe
A: 

Doing this with a regex for all possible inputs is impossible, as roe said. You would need a full parser for your language. But the regex below solves a subset of the problem that may be enough, as long as your text is VERY regular and adheres to some constraints:

  • Opening brace is the last character on the same line as the method header
  • Closing brace is indented the same amount as the opening line, is on its own line, and is the last character on the line
  • Line begins with int or void (you can expand this list to include all relevant types), ignoring leading whitespace

So this works on your sample input:

:%s/\v((^\s+)(int|void).{-})\s+\{$\_.{-}^\2\}$/\1;/
  • \v: "very magic" mode (avoids needing to backslash-escape everything)
  • ((^\s+): (begin capture of \1); capture the level of indent for this line
  • (int|void): first word on the line (add to this list as needed)
  • .{-}): non-greedy match on this line (end capture of \1)
  • \s+{$: as many spaces as possible, then a {, then end of line
  • \_.{-}: non-greedy match everything, including newlines
  • ^\2\}$: from the start of a line, match the number of spaces we captured above, then an ending brace at the end of the line

If you know how many spaces your method header lines are indented, you can plug this into the regex in place of (^\s+) to make it more fool-proof.

I guarantee you can easily think of possible inputs to make this regex fail.

Brian Carper
A: 

I would play with ctags to build the list of functions declared and defined in your header file. The list would contain the line of the inline definition, and not the regex to find the line.

Then in reverse order go to the line of definition of a function, search for its opening bracket, and apply a d%. It should work fine as long as there isn't any unbalanced brackets in the comments of the function.

BTW, in order to not loose the definition, I a have a :MOVEIMPL command that moves an inline function definition to the related .cpp.

Luc Hermitte