8

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
endfunction

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?

11

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
endfunction

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)
endfunction
  • Why do I need '' for !_compile other than other arguments? – nn0p Apr 23 '16 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 Apr 23 '16 at 11:18

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.