DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > 2 years of Vim and PHP distilled

2 years of Vim and PHP distilled

Giorgio Sironi user avatar by
Giorgio Sironi
·
Apr. 05, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
16.74K Views

Join the DZone community and get the full member experience.

Join For Free

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.

PHP Vim (text editor)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • Debugging the Java Message Service (JMS) API Using Lightrun
  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Upsert in SQL: What Is an Upsert, and When Should You Use One?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo