tags:

views:

102

answers:

2

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
+1  A: 

:%s/{{{\(\d\) \(.*\)/\2 {{{\1/g

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

Raoul Supercopter
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.
Masi
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.
rampion
thanks for the tips :)
Raoul Supercopter
+1  A: 

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/
rampion
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.
Masi