0

Say I have a function that takes multiple arguments, how would I pass them as a single string argument to another function?

function test(...)
    " vimgrep is just an example
    vimgrep a:000 /some/path/**/*.txt
endfunction

When bound to a key, eg

command! -nargs=+ FIND call SimpleGrep(<args>)
nnoremap <M-f> :FIND<SPACE>

I get the error that the input is an unknown variable, using string(a:000) doesn't fix things.

How would a:000 be converted into a string so FIND a b c could be evaluated as if FIND "a b c" had been written?

1

Join the arguments together with join().

function test(...)
    " vimgrep is just an example
    execute 'vimgrep /'.join(a:000).'/ /some/path/**/*.txt'
endfunction
command! -nargs=+ FIND call SimpleGrep(<f-args>)
nnoremap <M-f> :FIND<SPACE>

Note: join() will join together with a single space and <f-args> will ignore extra spaces, so :FIND a b c is equivalent to :FIND a b c

For more help see:

:h join()
:h <f-args>
:h a:000
  • Not sure escaping is correct here, needed to do execute 'vimgrep '.join(a:000) l:root (l:root is the variable for the root dir). – ideasman42 Sep 26 '16 at 22:47
  • In this case, the space stripping is a bit of a problem, possibly better to manually quote the string then. Even so, handy to know how to join varargs. – ideasman42 Sep 26 '16 at 22:49

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.