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
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>
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
:w !php -l
The real credit goes to Can I see changes before I save my file in Vim? for the idea so up vote there.
But to explain on this post (mostly taken from above): The above command works as follows:
The syntax for saving a file in vim is:
:w <filename>
The syntax for executing a shell command in vim is:
:!<command>
Executing the save command without a filename but rather a shell command behind it causes vim to write the files content to stdin of the shell instead of saving it in a physical file. You can verify this by executing
:w !cat
This should always print the files current content (which would have been written to a file instead).
4 You can check code with php -l
from STDIN
by piping it in
The file is "saved" to stdin, php lint is run with the stdin as input.
To check PHP syntax without having to save first you can use:
map :w !php -l
http://vim.wikia.com/wiki/Runtime_syntax_check_for_php
You should try a plugin like Neomake. It will show you in the gutter error markers for every PHP syntax errors. Go on the line to see the error itself. You can as well link it to PHP Mess detector and PHP Stan to show you errors and possible improvements in your code.
This combo is very powerful!
In case you are interested I wrote an article how to make a Vim PHP IDE. This is basically a list of essential plugins you should try out! Of course Neomake is part of it.
For just syntax highlight (what sometimes gives clues about errors) a non yet saved file (usually black and white on vim) just source the syntax file:
:source $VIMRUNTIME/syntax/[the syntaxfile].vim
Examples
:source $VIMRUNTIME/syntax/sh.vim
:source $VIMRUNTIME/syntax/php.vim
:source $VIMRUNTIME/syntax/javascript.vim
You can manually run the PHP linting commands, but if you're doing full time development in PHP then it's easier to use a generic syntax checking plugin.
Syntastic (as recommended by @lucapette) has long been the main syntax plugin for Vim with fairly relaxed requirements:
Syntastic itself has rather relaxed requirements: it doesn't have any external dependencies, and it needs a version of Vim compiled with a few common features:
autocmd
,eval
,file_in_path
,modify_fname
,quickfix
,reltime
,statusline
, anduser_commands
. Not all possible combinations of features that include the ones above make equal sense on all operating systems, but Vim version 7 or later with the "normal", "big", or "huge" feature sets should be fine.
Since Vim 8, which allows for asynchronous syntax checking, there are now two good plugins. These work by continuously linting your code so you don't have to save your code for errors to show up:
- Asyncronous Lint Engine (ale)
- Neomake (as recommended by @matthieu)
All of these should automatically call php -l
for the current file as long as the syntax for that file is set to PHP (:set syntax=php
if Vim doesn't automatically recognise the syntax).
精彩评论