5

I am using viemu in VS 2010. Is there a reliable command to delete an entire method in C# source code?

Here are things I have tried:

  • The movement commands } and ]] are not smart enough to pick out a method. Although at times they do happen to grab the correct amount of text.

  • From just within the method i can do diB which will delete the entire body of the method. That's not too bad, but then I'm left to delete the signature and outer brackets.

  • If I move the cursor to the visibility modifier (ie public, private, etc) then do d*, it will kill the entire method if the next method has the same visibility.

Any vi/viemu experts have a way to do this?

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • 3
    Click the minus sign to the left of the method to collapse the method, highlight the entire collapsed method line, and press the Delete key. – Robert Harvey Nov 24 '10 at 18:27
  • That's not bad, you should make this an answer. You can also collapse the method with `CTRL+M,M`, then just do `dd` from there. – Matt Greer Nov 24 '10 at 18:32

3 Answers3

2

In Vim, this is how I do it. I'm not sure if viemu is compatible enough, but:

  1. Move to the start of the method declaration (the accessibility modifier, etc).
  2. Hit v to enter visual mode.
  3. Position the cursor over the curly brace that starts the method body.
  4. Hit % to move the cursor to the matching brace.
  5. Hit d to remove everything that's selected.

It's not super-duper automatic or anything, but it works and is relatively easy to do once muscle memory kicks in.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • move on first line of method then `^vf{%d` – shellholic Nov 24 '10 at 19:13
  • Nice, that's much more compact and repeatable. One could write a custom command to do that. – cdhowie Nov 24 '10 at 19:27
  • Your answer works fine in viemu, it doesn't look like shellholic's does though. I do like CTRL+M,M then dd, but that is Visual Studio specific and one reason for using viemu is to help me branch away from VS. – Matt Greer Nov 24 '10 at 22:53
  • Actually I think I like go to the method signature, dd, d%. That does the trick every time and is pretty fast/simple. – Matt Greer Nov 24 '10 at 22:54
  • @Matt: Assuming that you put your open curly braces on a separate line, yeah, that will work fine. – cdhowie Nov 25 '10 at 01:04
2

I like to use zadd to fold it and delete that fold (which will get the declaration too).

Another way to do it if you're inside the level of the method/if statement that you want to delete is da{dd which I find a little easier to type than some of the other suggestions. It says to delete around the { bracket pair that the cursor is currently in, then dd to delete the remaining function declaration.

If you're nested inside control structures in a method, you can put a number before it to delete that many levels up.

Ted Naleid
  • 26,511
  • 10
  • 70
  • 81
  • (this works in vim, not sure about VS, don't have that, but these seem like pretty basic commands that it'd likely have) – Ted Naleid Dec 01 '10 at 22:00
  • This almost works in viemu, it leaves you one line off from the declaration, so basically it becomes `da{kdd`, assuming you have opening braces on their own line. – Matt Greer Dec 01 '10 at 22:11
1

dap (delete a paragraph) will work for simple methods which do not contain empty lines. For methods which do contain empty lines you will need to use diBdap (delete inner block followed by delete a paragraph). For easier use you can create a mapping in your rc file. ex: :nmap <C-d> diBdap

chris
  • 2,740
  • 3
  • 25
  • 23
  • You might want to clarify what das is, otherwise this isn't helpful. – Matt Ellen Nov 29 '10 at 22:13
  • Can you elaborate on das? I know `d` is the delete command, what type of movement is `as`? In viemu 2010 and gvim 7.3 for Windows `das` deletes a contiguous block of text, so if the method has empty lines in it, it will stop at the empty lines. If the method has no empty lines in it, `das` will delete it correctly. – Matt Greer Nov 29 '10 at 22:32