Syntastic syntax checker for VIM

In an earlier post I explained how to make PHP syntax checking available in VIM. Recently I came across the Syntastic VIM plugin which offers a much better VIM integration as well as syntax checking for more than 60 languages. A nice feature of Syntastic is that you can chain several checkers. In the case of PHP you can chain the a regular PHP syntax check with the PHP_CodeSniffer style checker. This way you get warnings about syntax as well as style issues.

In case of an issue Syntastic will highlight the line where the issue occurred:
Syntastic error screen

I recommend to use the pathogen VIM plugin and keep all your VIM plugins in individual subdirectories of .vim/bundle. You can either clone or copy the Syntastic plugin there. Once the plugin is installed you can run the :SyntasticInfo command in any file and you will get list of the active checkers.

Syntastic info for filetype: php
Available checkers: php
Currently active checker(s): php

Syntastic automatically runs when saving the file and you can trigger a syntax check any time by executing :SyntasticCheck

If you want to enable style checks you need to install PHP_CodeSniffer. On my Ubuntu install (Ubuntu-Server 12.04 LTS) the packaged version of PHP_CodeSniffer is too dated to work with Syntastic so I went with installing the PEAR version.

root@ubuntu:~# pear install PHP_CodeSniffer

Then you need to add PHP_CodeSniffer to the checker list for PHP by adding these lines to your .vimrc:

let g:syntastic_php_checkers = ['php', 'phpcs']
let g:syntastic_php_phpcs_args = "--standard=zend -n --report=csv"

You can configure various style standards. I went with the Zend standard. You can change the arguments to your needs. You can see a full list of the available arguments by running phpcs –help.

Syntastic syntax checker for VIM

Using vimdiff as GIT diff tool

If you want to review and edit your currently pending changeset in GIT vimdiff can be very handy. The regular git diff will simply output differences in patch format. If you set up GIT to use vimdiff as diff tool you will be able to see the differences in VIM’s split window view and be able to edit them too.

To use vimdiff as GIT diff tool simply run those 3 commands:

# git config --global diff.tool vimdiff
# git config --global difftool.prompt false
# git config --global alias.d difftool

Alternatively you can add these 3 sections to your .gitconfig

    tool = vimdiff
    prompt = false
    d = difftool

If you now run git d [file] you will see the changes in VIM. You can still use the regular git diff command to get the patch output.

Using vimdiff as GIT diff tool

PHP Syntax error testing in VIM

Please see my article about the Syntastic Vim Plugin for a better way of syntax checking in VIM.

I find it really helpful to be able to test for syntax errors from within VIM. VIM offers this option via filetype plugins and the :make command. To enable this functionality 2 small config changes are necessary.


# Map the make command to CTRL+t 
map <C-T> :w<CR>:make <CR>


# Set the make command to execute the PHP syntax checker
set makeprg=php\ -l\ %
# Update error formatting
set errorformat=%m\ in\ %f\ on\ line\ %l

This will only change the make command for .php  files and keep other files unaffected.

Once you changed those 2 files you can hit CTRL+t and you will see if you have any syntax errors in your code:

PHP Parse error:  syntax error, unexpected ';' in test.php on line 3
Errors parsing test.php

Note: Executing the syntax test will save the file before testing.

PHP Syntax error testing in VIM

PHP Remote Debugging in VIM with XDebug

XDebugs remote debugging feature is very helpful when trying to trace code in an unknown application or debugging a tricky problem. It works with most IDEs but can also be set up to work with Vim.

To get it working you need to install the XDebug Apache module:

root@michael:~# apt-get install php5-xdebug

and configure the XDebug config (/etc/php5/conf.d/xdebug.ini) for remote debugging:


The remote_host needs to be the hostname or IP where Vim is running. The Vim debugging plugin listens on port 9000. Make sure your firewall settings allow for connections to this host and port. The req mode starts the debugger for every request.

Next you need to download and install the DBGp Remote Debugger Interface plugin for Vim. There are several different versions available. Make sure to use the latest version by Hadi Zeftin. To do so, download the file and copy its contents into your Vim plugin directory (~/.vim/plugin/). If you want to make the plugin available to all users on your system you can also copy the plugin files to the central vim plugin directory (/usr/share/vim/vim73/plugin/).

You can make various adjustments if you like. I prefer to set the debugger timeout to 20s and the array/object depth to 10. You can change this by adding the following line to your ~/.vimrc:

let g:debuggerTimeout = 20
let g:debuggerMaxDepth = 10

Next you need enable the debugging mode by calling your script with the ?XDEBUG_SESSION_START=1 query parameter.

You only need to do this once and debugging will be enabled for 1 hour. You can change this time by setting a different xdebug.remote_cookie_expire_time in your xdebug.ini. There are also various browser plugins (Chrome, Firefox) available to enable and disable the debugging mode but in my opinion the query parameter is easy enough.

Once this is done you can open the file you want to debug in Vim and hit F5. You should see a status message like this:

waiting for a new connection on port 9000 for 20 seconds...

Next call your script in your browser and Vim should switch into debugging mode.

You can trace the code by using F2-F4. F12 lets you inspect the property under your cursor. Once you are in debug mode you can also open other files, set breakpoints and run the program till hitting the breakpoint by hitting F5. To set a breakpoint switch to command mode and enter Bp (:Bp).

PHP Remote Debugging in VIM with XDebug