Cover image for post Comfortable PHP Editing With VIM -8-

Comfortable PHP Editing With VIM -8-

As promised, I'm explaining my new VIM setup in some more detail, staring with the collection of plugins I use by now to enhance my VIM experience. I already have some in mind which I need to try out, but I would be happy to know which additional plugins you can recommend in relation to PHP programming (and beyond). Please leave a comment!

TLDR:

Vundle

Vundle is a VIM plugin manager and builds the basis for the setup. It takes care of installing, updating, removing and including other VIM plugins (and itself). For that, it supports plugins located on Github or vim.org. Using Vundle is as easy as adding some line like

Bundle "tobyS/skeletons.vim"

to your .vimrc and running

:BundleInstall

inside your VIM.

UltiSnips

TextMate made the functionality provided by this tool popular: UltiSnips allows you to define small, re-usable text (or code) snippets that contain placeholders. These snippets can the be bound to autocomplete-functionality, reacting on certain trigger strings and a trigger key. When both occur in your typing, the corresponding snippet is inserted and you can jump through the placeholders, which might be already pre-configured with sensible defaults.

For example, the following snippet:

public function ${1} (${2}) { ${3:return ${4};} }

could be bound to pub<tab>, automatically leaving you with a method stub and sensible jump marks (1. function name, 2. parameters, 3. statements with a return statement as its default.

UltiSnip can even do more powerful stuff like obtaining its default values from scripts and so own. I originally had SnipMate as my snippet engine, but that one is sadly unmaintained since some time now and UltiSnip offered the functionality I needed to use it in PDV and Skeletons out of the box.

PDV

PDV (phpDocumentor for VIM) allows you to generate doc blocks from your PHP code (classes, methods, attributes) and tries to guess as much information for you as possible, filling out sensible defaults.

In this brand new version (which ships even with automated tests o/) you can use Vmustache templates to define how your doc blocks should look like. That was pretty hard in the previous version.

Even better, the new version supports snippets out of the box, using UltiSnips. What that means is that you can provide jump marks in your doc templates and pre-fill them with the values guessed by PDV. So

/** * ${1:{{name}}} * * @var ${2:{{type}}{{^type}}mixed{{/type}}} */

Could be your template for attributes (thats the default snip template shipped with PDV). The first jump mark (1) will be pre-filled with the name of the property, you will most likely overwrite that. The second jump mark (2) is right after the @var. If PDV was able to guess a type (by a default value in the code), it will have that value pre-filled. This raises the chance that you can just skip over it (e.g. for scalars). If PDV could not guess a type, it fills our mixed, which you will most likely want to overwrite when tabbing on it.

Skeletons

My old VIM setup contained a little hack to paste some template text when I created a new PHP file (mainly the opening <?php and some empty lines). I liked that functionality but always wanted to extend it quite a bit, to give me even more convenience. The result is the little Skeletons plugin.

Skeletons allows you to create skeleton files per file type (VIM FT detection), which are pasted as you create a new file. However, these skeletons can again be UltiSnips snippets, which can again contain default values generated by embedded VIM script. As a result, when I now start editing a new file src/main/Qafoo/Some/Cool/Class.php (PSR-0), I get the following default content:

<?php namespace Qafoo\Some\Cool; class Class { }

Additionally, I have jump marks on the namespace, the class name and finally in the class content, that directly guide me to editing the important areas, if needed.

Powerline

The VIM status line is already quite powerful, if you configure it in the right way. However, Powerline is mind blowing. I shouldn't say much more, but simply direct you to the Powerline website for screenshots. Go, install it!

Syntastic

I still have the habit of typing :make everytime I saved a PHP file, which triggered a php -l on that file in my old setup, telling me if I had any compile errors. But these times are gone thanks to Syntastic. This little gem runs code analysis on your files whenever you save them, displaying markers directly on the lines where issue occured. And even better: It does not only support linting, but also PHPMD and PHP_CodeSniffer. Awesome!

VIM Colors Solarized

The Solarized color scheme (available in a dark and light version) has been crafted especially for readability and has also extremely pleasing colors. I still wonder how I can live without it in some places of my system.

Whitespacetrail

This little tool takes care of showing me trailing whitespaces in my source code files and even for stripping them automatically, as they are occasionally created.

Rename

So simple, but so problem solving. Rename provides the :Rename command which allows you to rename the currently edited file on disk. How often do you start writing a class and after some editing decide to change its name? Save each time ~3 commands with this plugin.

Easymotion

VIM text objects are a powerful tool for moving around in text files. However, I never got used to using them. Easymotion takes a differnet approach. Bound to a leader command (<,><w> for me), all coloring of your text vanishes and potential jump marks appear where you might want to go, alphabetically numbered. Just hit the corresponing key to jump to that place. TLDR; take a look at the Easymotion website for a video how that works.

Vim Pasta

Moving code around often requires you to adjust its indentation. For example if you extract code into a dedicated method to make code more readable. The Pasta plugin attempts to take this job over for you.

Comments

After a remark to my setup, I replaced ":Rename" with https://github.com/tpope/vim-eunuch, which provides the same command + some more cool shell commands.

Toby at 2013-01-28

I think you need to add some imagess or/and videos. It will be cool.

Ilya at 2013-01-29

Have fun coding like it's 1970.

Michael at 2013-02-01

Well, I do. :) I once tried migrating to an IDE last year, but failed after a month. IDEs are just not made for what I do the largest amount of my time when coding: Editing text. They are so bloody inefficient with that.

Toby at 2013-02-01