Tuesday, May 3, 2011

Correcting Wrong Marker Folding in VIM

I mistakenly did marker folding to my .vimrc:

{{{8 #CS
something..
}}}8  
{{{9 #Math
...
}}}9  
... many more!

I need to switch the format to "#SOMETHING {{{NUMBER" like:

#CS {{{8 
something..
}}}8  
#Math {{{9 
...
}}}9  
... many more!

What is wrong in the following code:

:%s$/({{{\d/) /(#[:alpha:]/)$\2 \1$g

[Solution]

%s$\({{{\d\) \(#[[:alnum:]]*\)$\2 \1$g
From stackoverflow
  • :%s/{{{\(\d\) \(.*\)/\2 {{{\1/g

    it works, but in your regex I don't understand why do you got a $ after s.

    Masi : The sign after the s does not matter, unless you need the sign. For example, I use normally # but now there was a #-sign in the match so I had to use some other. Choose anything that suits you.
    Masi : A word of advice: Using the .* can cause chaotic backtracking. In my case, I needed to be very precise to avoid backtracking.
    rampion : vim lets you use arbitrary delimiters for `:s`, so `:s$a$b$g` is the same as `:s/a/b/g`. Long story short, it lets you avoid escaping forward slashes when they occur in your pattern or replacement text.
    Raoul Supercopter : thanks for the tips :)
  • You forgot to escape the parentheses, and the POSIX character classes are only valid within a character class [[:alpha:]]:

    :%s$/\({{{\d/\) /\(#[[:alpha:]]/\)$\2 \1$g
    

    Note, however, that your example text doesn't contain any slashes - is this what your sample text is actually like?

    The above regex changes this

    /{{{8/ /#A/
    

    To this

    #A/ {{{8/
    
    Masi : Thank you. I solved the problem. Clearly, I was too uncareful with the intial regex. The errors are fixed in "my solution". Your regex is very similar to mine that I wrote to the post a few minutes ago.
    Masi : Thanks for elaborating the POSIX character classes.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.