Question

I want to substitute continous same words with one word in vim ,that is to say to keep just one word , substitute kitty kitty with kitty ,substitute world world with world,and so on.

Why my regular expression in vim is wrong?

:%s/\<\(\w+\)\>\s\1/\1/g  
Was it helpful?

Solution

The + needs to be escaped in vim regular expressions.

:%s/\<\(\w\+\)\>\s\1/\1/g

You can reduce the amount of escaping by enabling magic mode with the \v switch.

:%s/\v<(\w+)>\s\1/\1/g

Magic alters the syntax of vim regular expressions quite a bit. Be sure to read the documentation. Especially in longer, meta-character-ladden regexes it can improve length and readability, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top