Vim replaces expression with large text

So, I was looking for a way to take something like this

Two words
Three Words Here

And replace it with

Twowords = myHash["Two words"];
ThreeWordsHere = myHash["Three Words Here"];

I found this question that led me to the substitution teams, and I came up with something like this.

%s/\(\([A-z ]\)\+\)/\=substitute(submatch(1), ' ', '', 'g')/

Now this will lead to a match without spaces, but after the equal sign there will be nothing. Adding to the text after the replacement expression results in the error "E51: Invalid Expression".

My question is: is there a way to end the expression and add more text to the command: s? Something like that.

%s/\(\([A-z ]\)\+\)/\=substitute(submatch(1), ' ', '', 'g') = myHash["\1"];/

I could not find anything. I looked at: help subst-replace - \ = and other sources on the Internet. Thank!

+3
source share
1 answer

You almost didn’t have it.

\= , submatch(). , :

:%s/\(\([A-z ]\)\+\)/\=substitute(submatch(1), ' ', '', 'g') . ' = myHash["' . submatch(1) . '"];'/
+3

All Articles