First of all, you can use :v as a shortcut to :g! . I think the etymology comes from the -v option for the standard grep command.
Yes, you can use any ex command, including :s . By default :s acts on one line; Combining it with :g (or :g! or :v ), you can select the lines on which it acts. If I understand correctly, then
:v/<string I want to keep>/s/.*//
- this is exactly what you want. You will probably want to follow :noh .
Addendum: I am a little surprised at how popular this short answer is. The funny thing is that docs ( :help :g and then scroll down a bit) mention :%s/pat/PAT/g as an alternative ("two characters are shorter!") Before :g/pat/s//PAT/g . I think this means that :g/pat/s/ was more familiar than :%s/pat/ .
Shameless plugin: more for :g and :s , you might also be interested in my answer to why:% s / ^ $ // g does not equal: g / ^ $ / d in vim?
benjifisher
source share