History
Article Edit this page Discussion

Runtime syntax check for php

From Vim Tips Wiki

(Redirected from VimTip692)
Jump to: navigation, search

Tip 692 Previous Next Created: April 6, 2004 Complexity: basic Author: vid luther Version: 6.0


Ever wanted to just check your php script to see if it had any syntax errors? Similar to perl -c?. You could always do it by doing php -l, with this little macro, you can do it in your buffer.

Just add the following line in your vimrc, and whenever you want to test, press ctrl-b

map <C-B> :!php -l %<CR>

[edit] Comments

To improve on this great tip, here's how to check PHP syntax without having to save first:

map <C-B> :w !php -l<CR>

I would prefer to invoke the command via :make.

:set makeprg=php\ -l\ %

If you also set 'autowrite' to true the file is automatically written when calling :make

See also:


Setting this:

autocmd QuickFixCmdPre make w

will auto-save the buffer when :make is invoked. (useful if you don't want autowrite on for other commands)


Having this will make Vim jump to the first error occurred during compilation afterwards:

set errorformat=%m\ in\ %f\ on\ line\ %l

Well I'm using this to test php syntax pressing <C-P> in normal mode, if there are errors it automatically opens the error window (you should put the following code into a file that is sourced when editing a php file)

function! PHPsynCHK()
  let winnum =winnr() " get current window number
  silent make -l %
  cw " open the error window if it contains error
  " return to the window with cursor set on the line of the first error (if any)
  execute winnum . "wincmd w"
endfunction

:setl makeprg=php
:set errorformat=%m\ in\ %f\ on\ line\ %l

" Map <CTRL>-P to check the file for syntax
:noremap <C-P> :call PHPsynCHK()<CR>

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Runtime syntax check for php"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation