Even though you say you want a single command that does it automatically, I would suggest not doing that unless you really have a ton of them. Often attempting to come up with one command that does it all in one go is problematic compared to a simpler approach. You might have to try several commands before you come up with one that really works, whereas with a simpler approach you will probably get it right on your first or second attempt and you can think of what to do much quicker and without asking for help. Also, I often find that while I believe I know exactly how this pattern I want to delete appears every time in the file, there are often slight variances of it I had forgotten about or was not aware of. These variances might get picked up by the single command approach and deleted. You may not have wanted to delete them or it might delete more or less than what you wanted for a particular variance.
It is safer to let yourself see each match and decide to or not to delete it. Here is the approach I would take.
Type the following: /\/\/ SPECIAL
At this point it should be clear that you did or did not enter the search correctly before you ever delete anything, which is one of the advantages of this approach.
Once you verify you searched for the right thing, instead of pressing 4dd
, enter linewise visual mode with a captial V
. Instead of finding how many lines to go down, which will consume some of your mental visual processing ability for the day, do it the lazy way. Just press j
a few times until it looks right. That way is much easier to visually process. This also has the advantage of preventing you from mistakenly thinking you needed to delete 4 lines when you actually needed to delete 5 or some other amount, as you can clearly see highlighted what you are about to delete.
Once you have highlighted the lines you want to delete, just press d
to delete them. You only need to press it once, since you are in linewise visual mode. At this point, you can see that you either did or did not actually want to delete it that way without altering the entire file. It is not uncommon to notice that while you did delete what you intended, it would be better to also delete the line below it, as it is just white space. This is one of the advantages of this approach, you can refine exactly what you want to delete in one place before moving on to the rest of the file. The key here is to make sure that you undo the change that wasn't quite correct, and then delete all of it with a single command so that .
will work well for you going forward.
Once you have deleted what you want, press n
to jump to the next search result. Look and see if you really want to delete that next one. To delete it, just press .
to repeat your last deletion. Keep doing this until they are all deleted. So basically, you are pressing n.
over and over again.
This way with each result you can visually decide if that match is really the same and if you really want to delete it each time, and then deleting it is as simple as pressing .
. If you encounter a match where you want to delete it differently, ignore it for now, move onto the next result. Otherwise, you will mess up your .
. Finish deleting all the places that don't require any changes in how you delete it. Then keep pressing n
and try some variance of this approach to delete the remaining matches.
SPECIAL
lines was less than 3, what do you want to do? – Kentyesterday