Question

Say I want to remove all comment blocks in my source code without deleting the whole lines they are on. It's possible to achieve this using the

:%s/\/\*.*\*\//

command. I was wondering, is there a specific delete command for this, or is replacing the matched pattern the best approach? The difference most likely wouldn't be much, I'm just curious.

Was it helpful?

Solution

Replacing with nothing really is the idiomatic 'delete this pattern' operation.

:%s/pattern//g

If you want to blank all lines that contain a pattern, like in your example, the obvious solution is to add wildcard matches around the pattern.

:%s/.*pattern.*//

An alternative is to use :global with a normal mode or Ex command. These two achieve the same thing:

:g/pattern/normal! S
:g/pattern/delete|put! _

By the way, while I don't recommend using abbreviated command names in scripts or in code that other people might see, I think it's fine to use them interactively. Thus I tend to abbreviate such commands as :g/pattern/norm! S and :g/pattern/d|pu!_.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top