SublimeText2 XDebug

Debug PHP with Xdebug in Sublime Text 2 (in Linux mint)

After learning to know about Sublime Text 2, it seems to be a perfect companion to VIM and Eclipse, my other favourite editors/IDE:s. Now I also got it set up to debug PHP scripts with Xdebug :) See for yourself:

Sublime as the perfect companion to VIM and Eclipse

VIM, with some modifications, is perfect for quickly editing scripts while navigating around in the command line, and is also available virtually everywhere.

Eclipse is of course the standard workhorse for development, but sometimes it just feels to heavy, and even the simplest debug session will require setting up a new project with a lot of settings, defining a workspace etc etc. And this is where Sublime comes in.

Sublime is more easy to work with than Eclipse/PyDev, Eclipse/PDT (PHP) etc for a number of reasons:

1. Working with projects and folders

The first thing is how it works with folders and project.

In Sublime, most of the time you don't even need a project. It's enough to open a folder (File > Open folder ..., or "sublime <foldername>" on the command line). This will enable the super nice file opening tool (Ctrl + P), that suggests (and previews in realtime) files as you type (with powerful fuzzy-search), which is really handy when you forgot what that file in your project was named.

On the other hand, setting up a project is also very quick and easy. Just use the "Open Folder" feature as described above, and then go "Project > Save Project As ...". This will add one setting in the project definition: the path to the folder. For configuring Xdebug for PHP debugging (more info on installing the required extensions further below), you just need to add a remote URL setting. This is what I configured for working with my RDFIO SMW extension (accessed with "Project > Edit project"):

{
    "folders":
    [
        {
            "path": "/var/www/smw/w/extensions/RDFIO"
        }
    ],

    "settings": {
        "xdebug": { "url": "http://localhost/smw/wiki/Special:RDFImport" }
    }
}

2. The code overview / "minitature"

I really like the "code miniature" that you see to the right when having a file open, so that you get a graphical hint of how the file looks, and where you are. Actually in some ways better than a code outline feature like in other editors, since the "graphical structure" of the code seems easier to recognize for the brain, than the logical, at least for me.

3. Easy access to snippets

I hade trying to figure out how to write that method that makes you python file automatically run the main method, so that you can have it at the top ... In sublime I just go Ctrl+Shift+P and start writing "main()" in a python file, and I will find it right away.

4. Powerful auto-completion

Otherwise, I really like the autocompletion of both keywords and brackets and stuff. It is helpful and seem to just work, a lot more often than many other editors.

5. Easy zooming of text

Something I really miss in Eclipse is easy zooming of the text size. I have quite bad eyes, and sometimes need to really zoom up the text size a lot to be able to work at all. Ctrl + + and Ctrl + - is all you need.

5. Vi(m) mode!

I'm very happy about this. The shortcuts for navigating around in vim are quite hard to beat, even for sublime. Happily enough, sublime has the (vi)ntage mode, so that you can use the most common vim shortcuts within sublime itself.

All in all, Sublime makes me feel more productive than I (think) I have been before!

Setting up Xdebug with Sublime Text 2

Setting up Xdebug to work with Sublime requires a few steps, but it's not too cumbersome.

Note, this assumes you use Linux Mint (or maybe some other recent Ubuntu based distro).

  • Install xdebug. Should be enough with something like:

      • sudo apt-get install php5-xdebug
      • Configure settings in /etc/php5/conf.d/xdebug.ini.
      • My file looks like so:

        zend_extension=/usr/lib/php5/20090626/xdebug.so
        xdebug.remote_enable=On
        xdebug.remote_host="localhost"
        xdebug.remote_port=9000
        xdebug.remote_handler="dbgp"
        xdebug.remote_autostart=1
        ;xdebug.remote_log="/tmp/xdebug.log
      • (Well, the "remote_log" is not really needed, I just use it right now for some debugging)
      • Restart Apache (sudo service apach2 restart)
  • Check out the SublimeXdebug Sublime plugin from https://github.com/Kindari/SublimeXdebug and place it in ~/.config/sublime-text-2/

    • cd ~/.config/sublime-text-2/
      git clone git://github.com/Kindari/SublimeXdebug.git .
  • Follow Kindari's hint, for resolving the missing py expat stuff in Ubuntu/Linux Mint (see bottom of the github page):
    • Download python2.6 files from Ubuntu Archives
    • Extract the files: dpkg-deb -x python2.6_2.6.5-1ubuntu6_i386.deb python2.6
    • Copy the extracted usr/lib/python2.6 folder to {Sublime Text directory}/lib
    • (My note, leave the existing python26.zip file intact!)
  • Create a sublime project:
  • Open the folder where your PHP app is ("File > Open folder ...")
  • "Project > Save as project"
  • "Project > Edit project"
  • Add the "remote URL", something like so:


    {
        "folders":
        [
            {
                "path": "/var/www/myapp/"
            }
        ],
    
        "settings": {
            "xdebug": { "url": "http://localhost/myapp/index.php" }
        }
    }
    
  • Open the index.php file (or some other file you want to debug)
  • Create a breakpoint (Shift + F8 will give you the debug menu. Navigate down to "Add breakpoint)
    • Caveat: Do NOT use the "Right click > Toggle breakpoint" ... it does not seem to come from sublimexdebug, but probably from the GDB plugin that (I at least) did install.
  • Start the debugging: Shift+F8 > "Start debugging"
  • Use:
    • Ctrl+Shift+F6 - Step over
    • Ctrl+Shift+F7 - Step into
    • Ctrl+Shift+F8 - Step out from
    • Ctrl+Shift+F5 - Continue (until next breakpoint)

That's it, I think!