vim, xdebug and PHP 5.4's development server

I recently began using xdebug and vim for debugging and breakpoints. I had been using Eclipse and xdebug, and missed this feature when I switched back to vim.

I found a few articles on the subject, but most were fairly old, missed some key points and didn't mention the 5.4 development server. I used this article as my starting point, but I had to fill in a few gaps.

  1. First off, make sure xdebug is installed by checking phpinfo for the section on xdebug. I use the ppa/ondrej repository for managing PHP installations and was able to install xdebug through apt. Installation was as simple as apt-get install php5-xdebug.
  2. Once you have xdebug installed, you'll need to install the vim remote debugger interface. It is a straightforward install - just copy the files into your plugin directory. For me, this was ~/.vim/plugin.
  3. Now, you have to modify your php.ini file in order to allow remote debugging. I also turned on remote autostart so I don't have to deal with the ?XDEBUG_SESSION_START query string on my development server. Since I was using the PHP 5.4 development server, I had to do some extra work to find out where my php.ini file was located.

    Open up phpinfo again and check for the "Loaded Configuration File" entry. When you do this, make sure you access phpinfo as served by the 5.4 development server. If you access a phpinfo served through Apache2, it may not be the same file. For me, the correct file was the default /etc/php5/cli/php.ini.

    Add these lines to your php.ini to turn on remote debugging and remote autostart. 


  4. Now that you've got all of that set up, you can give vim and xdebug a try. Open a file in vim and press F5 to initiate the connection. Then go to your browser and navigate to a page that is being served by the PHP 5.4 development server and go back to vim. If all works well, you should see that a connection has been established. Press enter to begin debugging.

A few useful tips:

  • :Bp toggles a breakpoint on the current line. I remapped this to Ctrl+B by adding this to my .vimrc file:
     map <C-B> :Bp<Enter>
  • While debugging, move the cursor over a variable and press F12 to view details. This will come up in the WATCH_WINDOW in the top right. If the variable is too large to easily see, you can switch to that window. To navigate between panes, press Ctrl+W followed by either h, j, k, or l. I remapped these to Ctrl+(h/j/k/l) in my .vimrc
  • While on a breakpoint, you can hit ,e in command mode to go into eval mode. This lets you see variables or run small commands to test out code before writing it.