This expression, run interactively within Vim:

:%s/ \v(\w+('\w+)*)\_s\1/ \1/gi

It removes needless repetition of words.

In this way it turns this:

the the it it them them it's it's then then and this too

into this:

the it them it's then and this too

as intended.

However the same expression used within vimscript, like this,

silent! exe ':%s/ \v(\w+('\w+)*)\_s\1/ \1/gi'

fails to replace any repeated words.

I have tried:

  • escaping the ' with a preceeding '
  • escaping the ' with a preceeding \
  • enclosing the expression in double quotes ":%s .... " and not escaping the '

but none of these worked.

I also tried:

silent! exe ':%s/ \v(\w+('\''\w+)*)\_s\1/ \1/gi'

This did not work either.

  • 2
    Why do you use :help :execute to begin with? :help :s is perfectly valid in vimscript on its own.
    – romainl
    Mar 10 at 15:07
  • exactly, there is no reason to make your life harder and trying to put it into and :exe string. Mar 10 at 15:21
  • The title of the question should be rephrased. It's not about removing this and that, it's about fixing a vimscript expression.
    – romainl
    Mar 10 at 17:50

1 Answer 1


The remark of @romainl and @christianbrabandt are of course very valid.

But if you insist to use the execute command you have two possibilities:

  1. Use the literal vimscript string (') where you escape ' with another ' (i.e. '')
silent! execute '%s/\v(\w+(''\w+)*)\_s\1/\1/gi'
  1. Use the non-literal vimscript string (") where you have to escape \ with another \ (i.e. \\)
silent! execute "%s/\\v(\\w+('\\w+)*)\\_s\\1/\\1/gi"
  • 1
    THANK YOU! None of the other proposals worked. Why? I don't know. The only thing that worked was this silent! execute "%s/ \\v(\\w+('\\w+)*)\\_s\\1/ \\1/gi". Appreciated ! ps it would be great if `:%s .... ' worked. I'm sure it's suppose to, but I couldn't get it going for this expression.
    – Kes
    Mar 11 at 8:39

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.