I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 638 posts at DZone. You can read more from them at their website. View Full User Profile

2 years of Vim and PHP distilled

04.05.2012
| 14405 views |
  • submit to reddit

So you want to run Vim instead of your heavy IDE that trashes the hard disk, takes minutes to open and gets in your way while versioning files and automate commands.
I've written two years ago about my directives and opinions on Vim for PHP, but I learned more about which features are important in an editor, and what can be outsourced to the shell and to automation tools.

In the case of PHP development, you'll need to add some configuration and plugins to speed up your activities. Most of the responsibilities of an IDE - like testing and version control - are outsourced to the terminal while running Vim; however, there are some tweaks that make writing and editing code faster, along with aiding in discovery classes and methods in a PHP codebase.

Basics

There are some prerequisites you should be aware of for using Vim effectively.

First of all, almost all the letters on the keyboard correspond to an handy command that can be run in normal mode. u is undo, p is paste, hjkl move around, bwe move between words, and so on for each letter.

After learning to use these commands and the normal, insert, visual and visual block modes, add these directives to your .vimrc:

syntax on
filetype on
filetype plugin on

which will make sure that PHP-related plugins are activated while in a .php file. For example, syntax highlighting for the PHP language is bundled in most Linux distributions, so you won't need to download external files to enable it but only to add these lines.

Plugins: Command-T

I know, Command-T is written in Ruby, but it works very well for discovering classes in a codebase.

While running Vim with Command-t, <Leader>t will open a list of files where you can put sequence of characters used to filter them by name in the current directory:

[No Name]                                                     0,0-1          All
> PHPUnit/Extensions/Selenium2TestCase/Session.php                              
  PHPUnit/Extensions/Selenium2TestCase/Session/Cookie.php
  PHPUnit/Extensions/Selenium2TestCase/Session/Storage.php
  PHPUnit/Extensions/Selenium2TestCase/SessionStrategy.php
  PHPUnit/Extensions/Selenium2TestCase/Session/Timeouts.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/Url.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/Frame.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/Window.php
  PHPUnit/Extensions/Selenium2TestCase/Session/Cookie/Builder.php
  PHPUnit/Extensions/Selenium2TestCase/SessionStrategy/Shared.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/AlertText.php
  PHPUnit/Extensions/Selenium2TestCase/SessionStrategy/Isolated.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/AcceptAlert.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/DismissAlert.php
  PHPUnit/Extensions/Selenium2TestCase/SessionCommand/GenericAccessor.php
  vendor/phpunit/Tests/Regression/578.phpt
  vendor/phpunit/Tests/Regression/684.phpt
  vendor/phpunit/Tests/Regression/783.phpt
  vendor/phpunit/Tests/Regression/1021.phpt
GoToFile                                                      1,1            All
>> phpsession

In this case, I have entered phpsession to find (case-insensitively) the PHPUnit/Extensions/Selenium2TestCase/Session.php file, which due to the PSR-0 convention contains the PHPUnit_Extensions_Selenium2TestCase_Session class.

In the filter, you can omit slashes and arbitrary characters: the only requirement is that what you type is contained in the relative file name seen as a string. After finding your file (you can also move up and down if it's not the first choice), press Enter for opening it or <Ctrl>T for opening it in a new tab. Press <Ctrl>C instead for closing the selection.

Autocompletion?

Command-T substitutes NERDTree and similar tools for displaying the file system inside Vim; you can also use the sheel for organizing files. In my case Command-T has also substituted another popular tool, ctags, which generates a series of tags that can be used for autocompletion from the source code of a project.

What I do with Command-T when I need autocompletion for a class is just to quickly open it in a tab, and use <Ctrl>N in insert mode to get a basic word autocompletion. This gets you far before you need to generate tags for your project and maintain them.

Plugins: snipMate

snipMate has the ability to quickly generate code from templates called snippets; the highly repetitive activity of writing 'public function x()' can be alleviated by generating most of it via a snippet.

After installation, place new snippets in .vim/snippets/php/trigger.snippet (you will have to create the php/ folder). In insert mode, trigger<TAB> will paste the snippet and put you in ${1} position; further tabs will move you to the next placeholders.

Given this snippet in .vim/snippets/php/test.snippet:

class ${1} extends PHPUnit_Framework_TestCase
{
    public function test${2}()
    {
        ${3}
    }
}

in insert mode, test<TAB>MyTest<TAB>WillWork<TAB> will generate:

class MyTest extends PHPUnit_Framework_TestCase
{
    public function testWillWork()
    {
        
    }
}

placing you inside the method where you will be able to write test code immediately.

Conclusions

A few plugins and lots of exercise is what you need for writing PHP applications with Vim: the comparison is not between Eclipse and Vim, but between Eclipse and Unix+Vim. Remember that every routine saved in your IDE can't be reused inside Continuous Integration, while it's only natural for Unix commands to be ported there.

Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: