1

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.

3
  • 2
    Why do you use :help :execute to begin with? :help :s is perfectly valid in vimscript on its own. Commented Mar 10, 2024 at 15:07
  • exactly, there is no reason to make your life harder and trying to put it into and :exe string. Commented Mar 10, 2024 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. Commented Mar 10, 2024 at 17:50

1 Answer 1

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
  • 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. Commented Mar 11, 2024 at 8:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.