3

I am making a function to compile and run my application. I have been able to get it to run the shell command however I cannot get it to save the file.

Here is my code so far.

Try 1)

function Compile_and_run(game, major, minor)
    :w <CR>
    execute '!_compile' shellescape(a:game) shellescape(a:major) shellescape(a:minor)
endfunction

Try 2)

function Compile_and_run(game, major, minor)
    execute ":w <CR>"
    execute '!_compile' shellescape(a:game) shellescape(a:major) shellescape(a:minor)
endfunction

What is it that Im doing wrong? How would I get this function to save before the execute '!_compile' command?

9

Something like :w<CR> makes sense for creating a Normal mode mapping. In Normal mode, you need the : to enter Command-line mode, and the <CR> to execute the command and return to Normal mode.

Writing VimScript

Here, since you're writing VimScript, every line is interpreted as if it's in Command-line mode already. You don't need to lead with a : (though it's harmless if you do), and you don't need the <CR> to execute the command (and in this case, it's incorrect to use it).

You only need write (or w for short) in this context.

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

Take a look at :help vim-script-intro to learn more.

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.