I am making my own function to call a bash script that compiles and runs my program.

Here is the code I have tried.

function Compile_and_run(game, major, minor)
    !_compile a:game a:major a:minor

The only issue is it seems to parse a:game instead of the value.

How would I get a:game, a:major, a:minor to parse their value to the function?

1 Answer 1


Using :execute

All command arguments in Vim are interpreted literally unless designed to be evaluated. To build a command dynamically, you can use :execute. Each argument to :execute is evaluated, and they are all joined by a space to produce the final command.

function Compile_and_run(game, major, minor)
    execute '!_compile' a:game a:major a:minor

Incorporating shellescape()

It's a good idea to escape arguments being passed to the shell using shellescape(). It takes a string and escapes it for use in a shell command.

function Compile_and_run(game, major, minor)
    execute '!_compile' shellescape(a:game) shellescape(a:major) shellescape(a:minor)
  • Why do I need '' for !_compile other than other arguments?
    – nn0p
    Commented Apr 23, 2016 at 11:09
  • 1
    :execute's arguments are VimScript expressions that should evaluate to strings. If you pass multiple arguments, they will be joined with spaces. Since the first argument is just a literal string, we enclose it in quotes.
    – tommcdo
    Commented Apr 23, 2016 at 11:18

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.