I figured out why this doesn't work as I was hoping. The output is echo'd to the console, but when vim
finishes exiting, it removes everything that it echo'd, so it's not visible. This can be demonstrated with this:
autocmd VimLeave *.php :!echo Hello; sleep 5
I can see it for five seconds, then it clears and takes me back to a prompt. I thought I could just do something like this:
autocmd VimLeave *.php :!php -l %; sleep 5
And this does work, but then it creates a five-second sleep every time I exit editing a PHP file. I can Ctrl-C
out of it, but that's a bad habit to get into, and adds another step to the 95% of cases where I exit a file and there are no errors. I thought about changing sleep 5
to sleep 1
so that I wouldn't have to wait so long in those cases, but then it gives me only one second to view any actual errors, which might not be good.
So I wrote something a little more complex:
autocmd VimLeave *.php :!php -l % &> /dev/null; if [ $? -ne 0 ]; then ERRORS=$(( php -l % ) 2>&1 ); clear; printf "\n************************************ ERRORS ************************************\n$ERRORS\n********************************************************************************\n"; sleep 10; fi;
This does the following:
If there are no errors, it just exits as normal, and doesn't display any special output. If there is an error, it gives me a nicely formatted error display like this:
************************************ ERRORS ************************************
PHP Parse error: syntax error, unexpected 'include' (T_INCLUDE) in file.php on line 4
Parse error: syntax error, unexpected 'include' (T_INCLUDE) in ami.php on line 4
Errors parsing file.php
********************************************************************************
It clears the screen before displaying this error, and waits for 10 seconds. That gives me more than enough time to view and comprehend the error. I can always Ctrl-C
out of the display if I'm finished before the 10 seconds is up. If I Ctrl-C
out, or the 10 seconds are up, it goes back to the main console that I saw before opening vim
, just as I would normally see when exiting vim
without any of these modifications.
This can probably be made into an autocmd function so that it's a little cleaner inside my .vimrc
file, if I thought it was worth the time to learn how to do that.