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,eolset t_Co=256
colorscheme wombat256modautocmd 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.








Twitter
I am Thijs Lensselink a Webdeveloper from the Netherlands.
I love Vim so this blog post is great! :) A few things though:
I don’t believe you need to use that long command for ctags, I’m sure it has php support so I just use:
ctags -R –languages=+PHP src/
If you work on multiple projects, setting the tags file in the .vimrc won’t cut it. To get round this, one can place the tags file in the root of the project. When editing files in the project, I always run vim in the root folder (where the tags file is) and use the NERDTree plugin to navigate.
Vim is brilliant with NERDTree, autocomplete, tabs and an interactive debugger.
D’oh, having just re-read the post. You can have multiple tags files per project. Ignore me.
*whistles*
I tried the ctags command and this does seem to do exactly the same thing. You’re right on that :)
VIM is pretty damn cool. will take a look at NERDTree later.
For Netbeans i would recommend the jVi plugin. It can be found via sourceforge.
I’ve been using it for a while now and it is simply great. Your other short-cut keys still work (though i am using the zend/eclipse shortcuts insteads of netbeans’s).
Nice to see your blog pop-up on the register!
I will have a look at netbeans when i have some time. Didn’t look into a plugin for eclipse yet as well. So i have some more investigation to do.
Nice to see the post got some attention :) How is your blog coming along?
[...] = {"data_track_clickback":true}; Thijs Lensselink has a new post to his blog today showing a step-by-step guide to setting up a vim editor environment to provide a richer experience than the plain-text defaults [...]
[...] Lensselink has a new post to his blog today showing a step-by-step guide to setting up a vim editor environment to provide a richer experience than the plain-text defaults [...]
For Netbeans i would recommend the jVi plugin. It can be found via sourceforge.
I’ve been using it for a while now and it is simply great.
Hi, thank you very much for your post It s really helpful. I am trying to configure my own vim and I am having trouble following your instructions and the result it s not the same as you. I ll put it here so you can see if you can help.
First thing: the ‘set rule’ launch an error when starting vim: unknown option: rule line 1.
Looking around I ve seen it must be ruler. But I do not see file name in the below bar, like in your screenshots.
Second I install wombat scheme color it has changed a little bit scheme but when I open a php file there is not highlight code.
Third: Order :Project vim says there is no a command like this. And previosly in your article it is not mentioned to install a plugin project.
ctags it is installed and process ok, but I cant see the navigator on left and shorcuts key doesnt work.
Sorry for the brick!
And thanks again
Sorry I wanted to paste my vimrc: http://pastebin.com/E5UmwD0t
By the way I solved everything, cleaning my eyes with fresh water and I realized that I hadnt uncompress the files :P
Hey Stephen,
I had no time to reply more soon. So it’s good to hear you resolved all issues. Sometimes we just look over small things like that.
Happy coding,
Thijs
Dont worry. And yes about your comment. I understand. You know sometimes we go fast because we want the recipe fast and then everything fails.
Im doing a spanish version of your article whit expanded information an I want to add the option of compile through console. When I get ready I ll credit you of course. I hope you dont mind. Do you know how to make autocomplete with tab or similar instead with control+x+o.
Thanks
Good to hear you’re writing an article about it as well. Will read it for sure. Although my Spanish is not that great. Im fine with the credits. Ill post a link back to your article once you are done.
As far as the autocomplete function concerns. Check this out : http://od-eon.com/blogs/nai/learning-vim-tab-autocomplete/
Let me know how it all works out.
Cheers,
Thijs
curl http://j.mp/spf13-vim3 -L -o – | sh
that nails it
Nice one! Thanks.
[...] like to develop my PHP code in gedit“. Well that is a very good choice and so is “using vim for PHP“. And if you download and install Eclipse you might get disappointed by the (lack of) [...]