Debugging PHP with Vim + Xdebug + Vdebug

How to debug PHP code with Vim + Xdebug thanks to the Vdebug plugin. The example below assumes that you'll use vim as the editor in the webserver itself but you can use it from a different development machine (just adjust the IP addresses as desired).

Install Vdebug

Install the vdebug plugin so that it's started with your favourite Vim's plugin manager Pathogen/Vundle/plugins. In my case, for pathogen:

# git clone ~/.vim/bundle/vdebug

Install and configure XDebug

Install the xdebug package for your distribution. Either:

apt-get install php5-xdebug


yum install php-pecl-xdebug

Edit the xdebug.ini configuration file to set up xdebug (disabled by default, change to port 10000, disable the profiler, etc):

# cat /etc/php.d/xdebug.ini
; Enable xdebug extension module

Now, enable xdebug only in the desired virtualhost:

# cat /etc/httpd/vhosts.d/my_vhost.conf
php_flag   xdebug.default_enable on
php_flag   xdebug.enable on
php_flag   xdebug.remote_enable on

Configure vdebug

Edit the vimrc file for your developer user in the server and add:

let g:vdebug_options = {'ide_key': 'netbeans-xdebug'}
let g:vdebug_options = {'break_on_open': 0}
let g:vdebug_options = {'server': ''}
let g:vdebug_options = {'port': '10000'}

The predefined keys are:

<F5>: start/run (to next breakpoint/end of script)
<F2>: step over
<F3>: step into
<F4>: step out
<F6>: stop debugging
<F7>: detach script from debugger
<F9>: run to cursor
<F10>: toggle line breakpoint
<F11>: show context variables (e.g. after "eval")
<F12>: evaluate variable under cursor
:Breakpoint <type> <args>: set a breakpoint of any type (see :help VdebugBreakpoints)
:VdebugEval <code>: evaluate some code and display the result
<Leader>e: evaluate the expression under visual highlight and display the result
To stop debugging, press <F6>. Press it again to close the debugger interface.

How to use vdebug

  • Open the web browser and visit your web app. If using an NGINX+Apache environment, it's better if you visit Apache directly (port 8080 or whatever) and skip any proxying.
  • Login into your site if required.
  • Navigate until you reach the page you want to debug.
  • Open in vim the PHP file you want to debug (the page where you want to place the breakpoint).
  • Place a breakpoint in your code by pressing F10 in the desired line.
  • Press F5 to start the debugger. Vim will try to connect now to xdebug (you have 20 seconds to reload the page).
  • Reload the page in the webbrowser. You'll see that Vim has changed and it's displaying a different layout: code, variables, debugger messages, etc.
  • The debugging will start from the first line of the webapp.
  • Press F5 to execute the application until it reaches your breakpoint.
  • Now use:
    • F2 to step over (next line)
    • F3 to step into (next line, including functions)
    • F4 to step out (exit from functions)
    • F9 to run to current cursor position
    • F11 to show all the context variables
    • F12 to show the value of the variable behind the cursor
    • F6 + F6 to exit the debugging session and close the debugger windows

How to redefine vdebug's keys

If you don't feel comfortable with the default keyboard shortcuts for vdebug, you can change them by setting the "vdebug_keymap" variable in your .vimrc.

Keymap example from .

" Vdebug
let g:vdebug_keymap = {
\    "run" : "<Leader>/",
\    "run_to_cursor" : "<Down>",
\    "step_over" : "<Up>",
\    "step_into" : "<Left>",
\    "step_out" : "<Right>",
\    "close" : "q",
\    "detach" : "x",
\    "set_breakpoint" : "<Leader>p",
\    "eval_visual" : "<Leader>e"

<Volver a la sección de GNU/Linux>