1

I use Vim with marker folding for my work and the presence of all the fold markers annoys my colleagues, so I want a quick and easy way to cleanly remove them all from my files. Closing markers are easy, as I make sure they are always on their own line, preceded by varying amount of whitespace, so

:%s/^\s*# }}}\n//g

works fine.

Opening markers are a bit more of a problem as they can take one of two forms, either at the end of a comment (eg # IMPORTS {{{) or at the end of a block definition (eg def fn(): # {{{). I can deal with these two individually with

:%s/ {{{//g
:%s/  # {{{/g

but when I try to combine the two, I cannot get it to work. I have tried

:%s/( |  # ){{{)\\g
:%s/(  # {{{| {{{)\\g

both with and without an initial '^.*' before the parentheses but it didn't work (yes, I know that would have deleted the entire line, but I was hoping it would at least point me in the right direction). I've even tried escaping the various # and { characters out of sheer desperation, not expecting that to work since they don't need escaping in the individual substitutions, but regardless of what I try, all I get is a 'pattern not found' error. I've tried Googling, with no luck - all the answers I've found seem to say I'm doing it right, but I'm obviously misunderstanding them

It's not a major hassle having to use three instructions, especially as I plan to put this into a function, but I just want to know, what is the correct method of searching for and deleting either of the two patterns in one go?

1
  • I took the liberty of making your title more specific. Feel free to edit and improve further. Also: "the presence of all the fold markers annoys my colleagues, so I want a quick and easy way to cleanly remove them all from my files" :s/files/workplace/ :-)
    – Friedrich
    Commented Nov 13 at 13:37

1 Answer 1

1

You're not properly escaping your special characters.

You should either escape parentheses and the pipe or use very magic mode but then you'd have to escape the curly braces. See :help /magic.

  1. Escape parens and pipe
    :%s/\(\|#\) {{{//
    
  2. Use very magic mode
    :%s/\v(|#)\s*\{\{\{//
    
  3. Or just forget about the OR operator in favor of the \? (zero or one) quantifier. I threw in some \s* to also match any number of white space.
    :%s/\s*\(#\s*\)\?{{{//
    

In general, :help pattern is recommended reading.

One more thing, if it's not already on, consider :set incsearch. This will highlight matches while you type your pattern. If the highlight goes away, you know exactly where you mistyped.

2
  • Thank you ! As you can probably guess, regex are... not my strongest area of expertise :)
    – David Shaw
    Commented Nov 13 at 13:06
  • 1
    I can only repeat: :help pattern
    – Friedrich
    Commented Nov 13 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.