Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find and replace string of # (pound) characters in vim?

Tags:

vim

sed

I have a file where I have comments like ######### Utility Functions #######. I am trying to clean it up and make it something like # Utility Functions # which requires me to match a string of more than one pound signs and replace it with a single pound sign.

I have tried the following

:%s/#+/#/gc
:%s/"#"+/#/gc
:%s/\#+/#/gc
:%s/(#)+/#/gc

and a bunch more. As I understand # has a special significance to match multiple characters like * but in reverse direction. This would mean that I would have to escape it somehow to achieve the above. Any help appreciated.

like image 613
csurfer Avatar asked May 14 '18 12:05

csurfer


1 Answers

try that: :%s/\v#{2,}/#/g

#{2,} " 2, means at least two

There is a shortcut for "more than one" in most regex flavours: + so

:%s/\v#+/#/g

works too, if you use "very magic" (\v), see :h magic.

like image 118
Doktor OSwaldo Avatar answered Oct 20 '22 15:10

Doktor OSwaldo