I am sure this has been answered, but the query is a bit too complex for google.
In short, I am trying to delete many deprecated methods from some code. So I am skimming the file and when I see a method that I want to remove, I am deleting every line until I see a line that starts with a tab then the string "def".
I am thinking VIM should let me create a simple macro to do this, but the trick is escaping me.
Here is the kind of thing I want to remove.
def Method_to_Delete(self):
"""
@return Control ID :
@author
"""
results = {}
results['xx'] = "thingy"
results['yy'] = 'thingy2'
return results
def Method_to_Keep(self):
"""
This is a method that does something wonderful!!!
@return Control ID :
@author
"""
results = {}
results['xxx'] = "WhooHoo!"
results['yyy'] = 'yippee!!'
return results
I want to have the cursor on the line with "def Method_to_Delete" and do something that will delete up until but not including "def Method_to_Keep"
There has got to be a better way than multiple "dd" commands.
Deleting Multiple Lines in Vi and VimInstead of simply pressing dd, you can specify the number of lines you want to delete. For example, typing 3dd will delete the next three lines from the file. You can also use wildcard characters in the aforementioned command.
The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
Vim searches are actually text objects! That means you can simply:
d/\s*def
Then, simply repeat the command with .
as desired.
Use the :delete
ex-command with a range
:,/Keep/-1d
Explanation
:.,/Keep/-1delete
.,/Keep/-1
is the range in which the delete
command will act.
means the current line..
can be assumed and dropped/Keep/
search for the line that matches "Keep"/Keep/-1
is search for the line that matches then subtract one line:delete
can be shorted to :d
For more help see
:h :range
:h :d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With