I want Vim to execute build the project automatically after I write the buffer to a *.c
or *.cpp
file. Here is what I've done:
autocmd BufWritePost *.c,*.cpp make
This one works as expected (but the output is noisy; see the bonus question below):
"main.c" 26L, 448C written
:!make 2>&1| tee /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/vF0bk92/0
clang -g main.c
(1 of 1): clang -g main.c
Press ENTER or type command to continue
The problem is, when there is no makefiles in the current directory, an error is thrown:
"test.c" [New] 0L, 0C written
:!make 2>&1| tee /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/vtVN9SA/0
make: *** No targets specified and no makefile found. Stop.
(1 of 1): make: *** No targets specified and no makefile found. Stop.
Press ENTER or type command to continue
How can I tell autocmd to check if a makefile exists before executing the command?
Bonus question: can I tell autocmd to execute the command quietly? That is, don't show the output of the command, or more preferably, show it in the status line.