Alex Rivera | Logout

vim :make to compile and run C code if it's a success

Asked 2010-11-04T23:19:50.540
11

to compile C programs and run it, I use ":make" but I would like to run the compiled program too if the compile is a success.

I wrote

au FileType c setlocal makeprg=g++\ %\ \&\&\ ./a.out

in vimrc, that works, but I have a vim error when there's some mistakes in my code, so vim don't put the cursor on the good line. I get this error :

E40: Can't open errorfile /tmp/vEtUQQ2/0

Is there a workaround, a fix or another way to achieve this ?

Edit
Report

1 Answer

8

There is a way with pure vim to do it, but it is a bit of annoying.

Using QuickFixCmdPost(Autocmd Event) to check if there is building error after ':make' ran. And if there are no errors, run the newly compiled program.

autocmd QuickfixCmdPost make call AfterMakeC()
function! AfterMakeC()
    " No any error after make
    if len(getqflist()) == 0
        !./a.out
    endif
    " :~)
endfunction

You may want to put the script under namespace in compiler plugin

answered 2010-11-05T01:44:29.177

Your Answer