Skip to content

Instantly share code, notes, and snippets.

@coreysan
Last active November 16, 2015 07:51
Show Gist options
  • Save coreysan/c5ed805216cee8561174 to your computer and use it in GitHub Desktop.
Save coreysan/c5ed805216cee8561174 to your computer and use it in GitHub Desktop.
Debugging with vim in Homestead

#Debugging with vim in Homestead/Vagrant

This is a step-by-step on how to set up vim debugging in a vagrant (homestead) environment. This assumes that you have homestead set up.

The following tasks are performed on your Homestead box.

Install Pathogen

Pathogen allows simple installation of vim plugins. We'll need it to install vdebug. Get it here.

Install Vdebug

  1. Installation of Vdebug with pathogen is a synch. Follow these instructions.

  2. Once installed, open ~/.vimrc and add the following lines to the top of the file:

execute pathogen#infect()
call pathogen#helptags()
syntax on
filetype plugin indent on
  1. Test that it works:

  2. Run $ vim

  3. Within vim, run :help Vdebug. You should see the graphic display. Read through chapter 3 as we'll be getting there.

  4. Set the vdebug listening port. To avoid collisions with other services on port 9000, we'll use port 9001.

  5. $ vim ~/.vim/bundle/vdebug/plugin/vdebug.vim

  6. Scroll down to below the hash defined by let g:vdebug_options_defaults = { ... }

  7. Below that hash, add the following: let g:vdebug_options_defaults["port"] = 9001

Install XDebug

  1. $ pecl install xdebug
  2. Open /etc/php5/fpm/conf.d/20-xdebug.ini and enter the following:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9001

Notice how the remote_port above corresponds to the vdebug.vim port entered earlier.

  1. Restart nginx and php5-fpm:

  2. $ sudo service nginx restart

  3. $ sudo service php5-fpm restart

  4. Create a script to run files through the vdebugger

  5. Check that /usr/local/bin/ is on your path with $ echo $PATH

  6. Create a new file: $ vim /usr/local/bin/php-xdebug

  7. Add this: #!/bin/bash export XDEBUG_CONFIG="idekey=xdebug" /usr/bin/php "$@"

Command Line Test

  1. Create a new test file at your homestead web server root: $ vim ~/Code/sandbox/phpinfo.php
  2. Enter the following in it:
<?php
$test = "here";

phpinfo();
?>
  1. Place your cursor on the line $test = "here"; and run :Breakpoint to set a vdebug breakpoint
  2. In a new terminal tab (tab B), type (but don't run) $ php-xdebug ~/Code/sandbox/phpinfo.php
  3. Back in tab A (with vim open), press F5 to start the debugger listening.
  4. In tab B, press enter. The line should hang as execution rests in tab A.
  5. Switch to tab A. You should see a red line through the line we stuck the breakpoint on. Refer to the vdebug shortcuts to navigate around.

Browser Test

In a true web app, you'll need to debug via browser requests.

  1. Create a Homestead mapping to this same sandbox. In my Homestead.yaml file, I put this:
sites:
   - map: sandbox.app
     to: /home/vagrant/Code/sandbox
  1. Run homestead provision to process the mapping.
  2. In the browser, type the url (but don't press enter): sandbox.app/phpinfo.php
  3. Back in vim, press F5 to start the debugger listening.
  4. Press enter in the browser
  5. Observe how execution halts on the first line of code in your file
  6. Press F5 again to play through the rest of the file, rendering the phpinfo to screen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment