Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate PHP syntax in VIM

Tags:

syntax

php

vim

I would like to know how if it's possible to validate if a PHP file is valid in VIM without closing VIM every time?

Thank you

like image 640
Tech4Wilco Avatar asked Sep 01 '11 14:09

Tech4Wilco


2 Answers

You can execute shell commands in vim. This is the same as calling php -l filename.php from the shell:

:!php -l % 

I have this mapped into my ~/.vim/after/ftplugin/php.vim file so that I only have to press F5:

map <F5> :!php -l %<CR> 
like image 192
gpojd Avatar answered Sep 25 '22 10:09

gpojd


Use :make with the following php specific settings:

:set makeprg=php\ -l\ % :set errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G 

Your syntax errors will be in the Quickfix window. You can open this buffer with :copen or :cope for short. If you only want to open the window only if their are errors use :cwindow.

You can use :cnext and :cprev to move through the quickfix list to jump to the corresponding errors. I suggest Tim Pope's excellent unimpared.vim plugin to make moving through the list as simple as [q and ]q.

To simplify the workflow I suggest a mapping like this one:

nnoremap <f5> :update<bar>make<bar>cwindow<cr> 

Now you can just hit <f5> and the buffer will be updated (if necessary), linted, and any errors will appear in the quickfix window.

To make this a bit more robust, add these commands to ~/.vim/after/ftplugin/php.vim. Example ~/.vim/after/ftplugin/php.vim

setlocal makeprg=php\ -l\ % setlocal errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G nnoremap <buffer> <silent> <f5> :update<bar>sil! make<bar>cwindow<cr> 

For more information:

:h quickfix :h makeprg :h errorformat 
like image 37
Peter Rincker Avatar answered Sep 22 '22 10:09

Peter Rincker