Skip to content

Instantly share code, notes, and snippets.

@b108
Created August 31, 2013 12:29
Show Gist options
  • Save b108/6397904 to your computer and use it in GitHub Desktop.
Save b108/6397904 to your computer and use it in GitHub Desktop.
PHP auto syntax check (*nix only). Pre-writing file into /tmp directory because current dir may be remote mounted and writing can be very slow.
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' >/tmp/vimerr; cat"
let tmp_file = '/tmp/vim_tmp.php'
silent execute "w! " . tmp_file
silent execute "!php -l -f '" . tmp_file . "' 2>&1 | sed 's\\#" . tmp_file . "\\#".bufname("%")."\\#g' >/tmp/vimerr"
set errorformat=%m\ in\ %f\ on\ line\ %l
silent cf /tmp/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
au! BufWriteCmd *.php call PHPsynCHK()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment