3

I use vim for editing my PHP scripts, and I would like vim to automatically execute php -l on any *.php file that I exit to do a syntax check. I've set this autocmd in my ~/.vimrc file:

autocmd VimLeave *.php :!php -l %

What I'm expecting to see is either the message:

No syntax errors detected in (file).

...or whatever parse error was encountered during the syntax check. But nothing is displayed.

When I replace the command above with something that proves that the autocmd is actually running (like echo % >> /tmp/vimtest), I can see that the file gets created and appended to with the correct information. When I replace it with something that should just output something (like echo hello world), I get nothing. So I can only assume that my php -l command is running, but vim doesn't show me the output.

I know virtually nothing about vimscript and have never worked with autocmds, so I'm hoping there's a flag that I can set here that will cause the output to be displayed to the console on exit.

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .