Fandom

Vim Tips Wiki

Runtime syntax check for php

Redirected from VimTip692

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 692 Printable Monobook Previous Next

created 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>

CommentsEdit


Use

:compiler php

Which will :set makeprg=php -lq, plus it will set your default error format. Then, check for syntax errors by typing

:make %

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>

all of these tips have one drawback...they force you to write the file first. I wanted a check before the write happens. So i modified the things a little.

au! BufWriteCmd  *.php    call PHPsynCHK()

if !exists('*PHPsynCHK')
  function! PHPsynCHK()
    ccl
    let winnum = winnr() " get current window number
    silent execute "%!php -l -f /dev/stdin | sed 's/\\/dev\\/stdin/".bufname("%")."/g' >.vimerr; cat"
    silent cf .vimerr
    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"
    silent undo
    silent cf
    if 1 == len(getqflist())
      w
    endif
  endfunction
endif

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

I used to do :!php -l %. The previous tip has by far worked the best for me since that. I made a small modification to return the cursor to the previous position if there were no errors; as it was above sent the cursor to the first line when successfully saved.

if !exists('*PHPsynCHK')
  function! PHPsynCHK()
    ccl
    let winnum = winnr() " get current window number
    let linenum = line('.')
    let colnum = col('.')
    silent execute "%!php -l -f /dev/stdin | sed 's/\\/dev\\/stdin/".bufname("%")."/g' >.vimerr; cat"
    silent cf .vimerr
    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"
    silent undo
    silent cf
    if 1 == len(getqflist())
      w
      call cursor(linenum, colnum)
    endif
  endfunction
endif


if you are having problems with make and file paths, try using lmake instead

Also on Fandom

Random Wiki