1

I have to replace a lot of variables s1.size() with wa, and replace s2.size() with wb accordingly. I have made next substitution request:

:'<,'>s/\vs([1|2])\.size\(\)/w\={"1":"a","2":"b"}[submatch(1)]/g

But it wont work. Also, if I remove that w from replacement field,

:'<,'>s/\vs([1|2])\.size\(\)/\={"1":"a","2":"b"}[submatch(1)]/g

it does almost what I want but of course it will replace just s1.size() with a and s2.size() with b without w.

How to put the vim script evaluation command \= and some text together in replacement field of substitute command?

I could not find help about \= with :help, if you know, please provide me a correct prompt to get vim help about \= command.

1 Answer 1

2

You can find help about the \= sub-replace-special command under :help :s\= (agreed, that's not very obvious.)

In any case, you can see that it says "When the {string} starts with \= it is evaluated as an expression", which indicates it can only be used at the beginning of a replacement and not in the middle.

In order to fix that for your issue, it seems the simplest is to simply include the "w" as part of the replacement in each value in your Dict:

:'<,'>s/\vs([12])\.size\(\)/\={"1":"wa","2":"wb"}[submatch(1)]/g

Or another possibility, to avoid repetition (especially when instead of "w" you have a longer replacement) is to use the . operator to concatenate strings (see :help expr-.):

:'<,'>s/\vs([12])\.size\(\)/\="w".({"1":"a","2":"b"}[submatch(1)])/g
3
  • 1
    Thank you a lot for detailed answer! Jun 21, 2023 at 6:57
  • 2
    To find the help you can use :helpgrep @VladyslavRehan
    – D. Ben Knoble
    Jun 21, 2023 at 13:13
  • 1
    Yes I did :helpgrep \= followed by :cwin (to show the results in a window) then switch to that window and press "Return" on the relevant one. (Though using the website at vimhelp.org is even easier, auto-complete on the search box helps with that!)
    – filbranden
    Jun 21, 2023 at 16:27

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.