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

Does anyone know how it can be done?

  • You're looking for :h :exe. I won't be surprised for this question to be a duplicate of an another one. – Luc Hermitte Jan 29 '19 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 '19 at 17:26
  • An extremely close Q/A is here: vi.stackexchange.com/questions/4399/… – Luc Hermitte Jan 29 '19 at 17:41
  • thank you, I am trying, I am near to solution – davide1421 Jan 29 '19 at 17:55

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.


  • 1
    Note, that the argument should not contain a backslash – Christian Brabandt Jan 30 '19 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.