Comfortable PHP editing with VIM -3- - Blog - Open Source - - php, photography and private stuff

Comfortable PHP editing with VIM -3-

It's time to continue my series, since I again updated my .vimrc in the past weeks for much more comfortable editing of PHP sources using VIM. There are several new features I added:

  • Auto completion of functions using <TAB>

  • Auto reloading of .vimrc when changed

  • Enclose visually selected text by braces/quotes

  • Much more convenient scrolling through autoclosing folds

  • Search the PHP manual directly in VIM using phpm

  • Added settings for misconfigured machines

  • More convenient creation of phpdoc blocks with correct indenting and automatic * chars

To use some of this (and the older) features, you will have to download Rasmus' function list and install phpm (which is very easy).

Thanks a lot to everyone on the web providing me with support/tips!

For detailed information on what has changed, please read the extended entry.

A short hint for those of you who fight with auto completing enclosing chars (braces/qutotes) when you only want 1 exemplar: Use <CTRL>-v + the char to get only 1 of it!

Sadly I lost a link I found while browsing the web where a script allows you to jump to PHP compile errors when running PHP CLI from VIM. Maybe someone can help me out?

Update: There were some <CR> missing in the <CTRL>-p mapping for running PHP CLI on the current file. I fixed that and replaced the download with version 1.1pl1. If anyone finds more bugs, please comment on this blog entry or email me!

Auto completion using <TAB>

Bash users (as I am) are used to get their commands automatically completed when hitting the <TAB> key after typing parts of a command. This works in VIM's command mode, too. Until now, you needed to remember that <CTRL>-k is used in insert mode to complete function names (using VIM's dictionary feature). From now on you will not have to change your practice, since auto completion will work using <TAB>. Since the <TAB> key is also used for indenting, a function in the .vimrc determines automatically, if you want to indent code (you are at the start of your code in the line) or complete a function name. In those rare cases where you really need a <TAB> not for indenting you can simply use <CTRL>-v + <TAB>.

Auto reload .vimrc

Everytime you changed your .vimrc you usually have to either restart VIM to make changes take effect, type all changes manually into command mode or reload .vimrc using :source ~/.vimrc (which is of course the easiest way). Having this new feature, your .vimrc will automatically be reloaded when you save it.

Enclose visually selected text by braces/quotes

Sometimes you want to include text you already typed text into braces or (more likely) into quotes. Using this feature you can simply mark the text (go into visual mode using v/<CTRL>-v/<SHIFT>-v/... in command mode) and type the char you want to wrap around the text (like ( for (), ' for '',...) and it will be enclosed.

Thanks to a commentor on this blog entry.

More convenient scrolling through autoclosing folds

The new version of my .vimrc has folding activated by default, using fold markers ({ { { and } } }). Since I often have to edit huge files I let folds get automatically closed, when I leave them. This is really cool since you always have a great overview on your code (presumed that you have convenient fold markers). But in some way it's a bit nerving, that you accedentally move out of a fold by scrolling out of the screen (you maybe don't see the fold ending). A new setting will automatically scroll 5 lines further, when you hit the end of the screen. And another option indicates moving out of the screen 3 lines before you really hit it. Using this, you always have a good overview on the context of your currently line and see fold endings before you hit them.

Search the PHP manual directly in VIM using phpm

phpm is a neat little tool which allows you to search the PHP manual for functions from the shell. This enables you to simply run ":!phpm function_name" in command mode and see the prototype of the function. In the new version of my .vimrc I added a mapping for phpm which will show you the prototype of the function your cursor is currently on. You can simply type a function name (or the start of it, e.g. "mysql_"), type <CTRL>-h and phpm will show you the prototype.

phpm is provided by Havard Eide and can be downloaded here. It needs PHP5.

Added settings for misconfigured machines

I recently had to edit some files on a somewhat misconfigured machine, where I had some wired problems with deleting already existant text and deleting empty lines. Some new settings in .vimrc fix this behavior for you, if you once run into the same problem:

:set backspace=start,eol

More convenient creation of phpdoc blocks with correct indenting and automatic * chars

When usually creating phpdoc blocks for your code blocks you have to manually move the cursor on blank forward in the second line and manually add the * chars at the start of each line. A new setting in .vimrc will do that automatically for you.

If you liked this blog post or learned something, please consider using flattr to contribute back: .



Add new comment

Fields with bold names are mandatory.