knightly

Blog Archives

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.

Dark theme for Zend Studio 8

Because i was so happy with my new 3 monitor configuration. I decided to refresh my development environment as well. Normally all my files would reside in the Devspace older in my user folder under the /home directory. Now i used a new 500 GB SATA drive and formatted it EXT3. Moving your workspace in Zend Studio has the minor inconvenience that the local settings like fonts / syntax colours are lost. I still haven’t found a way to export this. So i decided to take this opportunity to create a nice new dark theme.. My friend Bart (still no blog?) was friendly enough to let me know how to export the theme related data in ZS. And was nice enough to send me his zenburn theme.

File > Export > General > Preferences

File > Import > General > Preferences

Bart’s zenburn theme:

Most developers i know don’t seem to care much about the colour of their screen and the font they use. But considering the fact that i spend an insanely amount of time behind a screen. It would be nice if this environment i am in all day put the least amount of constrained on my eyes as possible. So i choose to take time and configure this for optimal viewing pleasure. This starts with the font. For years i have been using the Envy Code font by Damien Guard inside my IDE. This all started i think with a post by Jeff Atwood It’s a great font that is extremely easy to read even for terminals. Although still in beta. I advice people to use it when possible.

The second thing i configure are the colour schemes my IDE uses. The standard colour always has a white background. White makes sure things are clear. But staring at a white screen for more then 6 hours always resulted in headaches for me. So the darker the better. And with a dark background you are forced to change the rest as well.

Changing the font is first

General > Appearance > Colors and Fonts
Font : Envy Code R Bold (11 pt)

Second up are some general text editor colours

General > Editors > Text Editors
Line number foreground : #787878 (120, 120, 120)
Current line highlight : #35353D (53, 53, 61)
Background color : #25252D (37, 37, 45)

And finally the PHP syntax coloring

PHP > Editor > Syntax Coloring
Decprecated : #000 (0, 0, 0)
Fields : #FFFFFF (255, 255, 255)
Heredoc : #008282 (0, 130, 130)
Keyword : #DE5727 (222, 87, 39)
Multi-line comment : #557F5F (85, 127, 95)
Normal : #FFF (255, 255, 255)
Number : #FFCECE (255, 206, 206)
PHP tags : #DE5727 (222, 87, 39)
PHP Doc : #FEC601 (254, 198, 1)
PHPDoc Comment : #FEC601 (254, 198, 1)
single line comment : #FEC601 (254, 198, 1)
static fields : #FFFFFF (255, 255, 255)
static methods : #FFFFFF (255, 255, 255)
String : #B3C0C8 (179, 192, 200)
Task Tags : #FEC601 (254, 198, 1)
Variable : #0B91B7 (11, 145, 183)

The result can be viewed below

There are much more configuration options to do but for now this is what i came up with. Setting up the theme is a since in ZS. But one thing bothered me. The function / property name highlighting when you select an element inside the IDE. Was a very light colour. And this made it impossible to read the contents of the selection.

So after searching for a while and setting every possible setting in the configuration tabs. I finally figured out how the set the colour for these two actions. This is done from the Annotations setting in the Text editors panel under the general tab. The two options to change are PHP elements read / write occurrences.

For now i settled with a dark colour. But maybe i will change this in the future since it is not very readable

That’s it for now. This of course only sets up the PHP environment. The syntax colours for XML, HTML, CSS and Javascript still have to be changed. But i will leave it at this.

A new look for lenss.nl

This long Easter weekend gave me some time to create a new theme for this blog. So after a day of work this is the result. I was a bit tired of the dark unreadable format. At the moment i am still tweaking here and there but it looks fine!