VIM for a PHP Developer

For my coding work i mostly use Zend Studio. And i am a big fan of this IDE. But i also do a lot of work in the shell. And that asks for at least basic vim knowledge. My colleague is a big vim fan. And does most of his work in vim. So last week i was compiling a cheat-sheet for my self. And came across a slideshow of one of Andrei Zmievski’s talks. This slide show got me inspired enough to start playing around with vim a bit. And this is the result of it.

We need to have vim installed of course.

$ sudo aptitude install vim

Vim doesn’t seem to create the user folder automatically. So we need to create these our self. We’ll start off with a folder for plugins, color schemes and syntax settings.

  • $ mkdir ~/.vim
  • $ mkdir ~/.vim/{syntax,colors,plugin}

Most of the time i will be editing .php files. So i want some good syntax support for that. Vim has a nice plugin system. And there does seem to be a syntax file for PHP available. It’s a bit outdated. But will do for now. The only thing to do to enable it is copy it to the plugin directory.

$ mv php.vim ~/.vim/syntax

Now we have a very powerful but basic PHP editor. That’s nice and all. I would like to at least have some sort of a project manager. And there is a plugin for that as well. Download & copy!

$ mv project.vim ~/.vim/plugin

Ok that’s pretty cool. But it still looks like crap. Time for some theme sugar. It actually took me quite some time to find a theme i like. Guess i am picky. The theme / colour scheme i settled for is called wombat. The original file didn’t do much for me. But i found a good 256 colour version.

$ mv wombat256mod.vim ~/.vim/colors

  • That’s starting to look like a pretty nice setup. Time to make some settings permanent and to enable some other features.

$ vim ~/.vimrc

And add some settings

  • set rule
  • set wrapscan
  • set number
  • set backspace=start,indent,eol
  • set t_Co=256
  • colorscheme wombat256mod
  • autocmd FileType php set omnifunc=phpcomplete#CompletePHP

In the meantime i found this interesting post on Matthew Weier O’Phinney’s blog about using ctags , VIM and PHP. That looks like interesting stuff. So let’s add it. With ctags it’s possible to scan a source tree. And compile a sort of index file that vim can use for lookups.

  • $ sudo aptitude install exuberant-ctags
  • $ mkdir ~/.vim/tags

Creating an index file is a piece of cake. Move to a source directory and issue the following command.

  • $ cd /some/path
  • $ ctags-exuberant -R -f ~/.vim/tags/filename -h “.php”
  • –exclude=”.svn” –totals=yes
  • –tag-relative=yes –PHP-kinds=+cf
  • –regex-PHP=’/abstract class ([^ ]*)/\1/c/’
  • –regex-PHP=’/interface ([^ ]*)/\1/c/’
  • –regex-PHP=’/(public static abstract protected private )+function ([^ (]*)/\2/f/’

This will scan the whole directory structure recursively while using the regular expressions to extract useful information from the source files it finds. When done and when the parameter –totals=yes is used the following output is displayed. But probably with different numbers.

  • 2375 files, 466018 lines (14628 kB) scanned in 11.7 seconds (1246 kB/s)
  • 199296 tags added to tag file
  • 199296 tags sorted in 0.00 seconds

Time to enable tags. I want to use multiple files. Some standard files for common frameworks / libraries. And a tag file per project.

  • $ vim ~/.vimrc
  • set tags=tags;~/.vim/tags/filename

So let’s start vim and have a look at what we have so far. The first thing we do when vim has started is launch the :Project plugin.

  • $ vim
  • :Project

The project plugin took some time to get used to. But it’s a very convenient plugin. And easy to get the hang of. It’s possible to create the project settings by hand. But the plugin comes with a build in tool for this. While in :Project mode and hitting \C will make it easy to create new projects.

  • $ vim
  • :Project
  • \C

This will guide you through the process with a few short and simple questions.

  • Enter the Name of the Entry: [ project name ]
  • Enter the Absolute Directory to Load: [ absolute path ]
  • Enter the CD parameter: [ path to move into ]
  • Enter the File Filter: [ file filter : *.php ]

When that’s done the screen will be in edit mode inside the .vimproject file. Displaying something like this.

Project name=/path/to/project CD=/path/to/project filter="*.php" {
  filename.ext
  folder----------------------------------------------------------
}

It’s possible to alter the project setup here. Like adding more files. Or even adding files from other locations outside of the project.

Project name=/path/to/project CD=/path/to/project filter="*.php" {
  filename.ext
  folder----------------------------------------------------------
  
  External=/some/other/path CD=/some/other/path filter="*.php" {
    filename.ext
  }
}

When done editing. Just do like normally in vim.

:w

Pretty cool we have a project browser on the left. That is easy to navigate with the arrow keys. And a work canvas on the right.

Time to test the ctags created index files. Place the cursor at the beginning of a string that you want to lookup. And hit CTRL + ] and CTRL + T to get back. Another cool way to open the source file is by using horizontal split windows by using CTRL + W ].

Testing auto-complete is just as easy. Type some php core function name partly _str__ and hit CTRL + x and CTRL + o. This will display the following scrollable drop down..

That’s about it. I had good fun exploring all the features and possibilities. And have only scratched the surface. One more thing i found on Matthew’s blog was a way to test and run PHP script from vim. Awesome stuff. You can read about it here. Although i like vim. I don’t think i will be leaving ZS any time soon.

comments powered by Disqus