2

I would like to use the arguments passed to a function, as an argument to replace a search.

Something like this:

function! Myfunction(...)
    %s/hello/   goodBye a:1, a:2/g
endfunction

Does anyone know how it can be done?

4
  • You're looking for :h :exe. I won't be surprised for this question to be a duplicate of an another one. Jan 29, 2019 at 17:13
  • I have looked into other questions without finding anything, if you can tell me something I can use, it would be appreciated. I am a beginner user, maybe I missed the answer from under my nose, given my inexperience
    – davide1421
    Jan 29, 2019 at 17:26
  • An extremely close Q/A is here: vi.stackexchange.com/questions/4399/… Jan 29, 2019 at 17:41
  • thank you, I am trying, I am near to solution
    – davide1421
    Jan 29, 2019 at 17:55

1 Answer 1

2

If you can have any number of parameters, you'd have the choice between the following (untested) solutions

:exe '%s/hello/  goodbye '.join(a:000, ', ').'/'

(expecting the parameters contain no backslashes) or

:%s/hello/\='  goodbye '.join(a:000, ', ')/

This second solution works only when the dynamic part is used in the replacement text, not in the searched pattern.

See:

1
  • 1
    Note, that the argument should not contain a backslash Jan 30, 2019 at 10:52

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.