Cover photo for post Comfortable PHP editing with VIM -3-

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.

Comments

VIM already maps 'k' to "look up the word under the cursor with man." So if the filetype is PHP, I set keywordprg to w3m pointed at php's manual. I described it here: http://deadman.org/scripts/php_vars.vim

If you put that file in ~/.vim/ftplugin (and your version of Vim supports it) it should get automatically sourced whenever you're editing a php file.

Culley Harrelson also wrote a handy script for editing PHP in Vim that you can find on vim.org.

remapping ctrl-b is only useful if you don't use ctrl-f/ctrl-b for paging..

Sam Rowe at 2005-05-21

Having a lookup of functions only when online sounds a bit odd, since I code most of the time when being offline, so the phpm solution is more convenient.

Can you point me to that script on vim.org directly?

Your issue regarding ctrl-b is fixed in version 1.1pl1. Thanks for the hint!

Toby at 2005-05-22

You might find these interesting: http://cvs.php.net/co.php/phpdoc/funclist.txt?r=1.35 http://cvs.php.net/co.php/phpdoc/funcsummary.txt?r=1.27

S

Sean Coates at 2005-05-22

I use this in my .vim/after/plugin/php.vim file (which gets sourced after all the regular stuff):

What does it do? It only applies the php function list completion to buffers that contain php code, as it's really annoying to have php functions complete in a buffer containing C code :-)

As Sean pointed out, you can get a more up to date function list from the phpdoc repository:

Where php-lang-constructs.txt contains:

(language constructs are not implemented in the same way as the other functions, so the phpdoc tools can't pick them up from the source).

Wez Furlong at 2005-05-23

Inspired by a trackback from here:

This will add a ; to the end of the current line, when there is none. This hack will definitly be part of my .vimrc 1.2.

Toby at 2005-05-23

I'm having trouble using the folding.. the problem is that when I type a left brace, it automatically inserts a carriage return and creates a closing brace (which is good if I'm writing a function, etc.). Is there some way to type 3 consecutive opening braces without invoking the automatic carriage return?

Bryan at 2005-05-24

Simply use CTRL-V + (/{/[/"/' to type only the opening part (as it's said in the article! :)

Toby at 2005-05-24

Thanks.. I swear the CTRL-v wasn't working before.. I was just getting a visual block. It works fine now though.

Bryan at 2005-05-24

Hmm... wired... Maybe you were in command mode (ctrl-v then indicates creating a visual block)?

No matter, now it's working! :) Have fun! :)

Toby at 2005-05-24

When I use code temlates, e.g. =cla, I get * doubled from the 2nd string:

mitjok at 2005-06-21

If you want correct indenting for PHP with VIM you may be interested by my script:

You can find a detail description of what it can do:

http://www.vim.org/scripts/script.php?script_id=1120

or

http://www.2072productions.com/?to=phpindent.txt

John Wellesz at 2005-06-23

I've downloaded the function list, and I located it on my disk two times in the .vimrc file and it doesn't work? I also seem to be getting:

Any ideas why?

Will at 2005-08-01

Looks like to tryed including the funclist instead of adding it to your dictionary...?

The inclusion should look like this:

Toby at 2005-08-01

Ok, I have a good news for all PHP programmers: my script will be included by default in Vim 7 :-)

http://www.vim.org/scripts/script.php?script_id=1120

John

John Wellesz at 2005-08-08

Great stuff! But I think the funclist.txt of Rasmus is incomplete. I'm not sure what exactly is missing, but I grabbed all linked functions from http://php.net/manual/en and got nearly 1.500 functions more than listed in Rasmus file: http://www.kurzefrage.de/~as/contrib/phpfunclist.txt

Adrian at 2005-10-20

Hey, don't you wanna upload your .vimrc again? :-) I would like it, Chris

Christian at 2006-07-08

Please look here for the latest version.

Toby at 2006-07-08

This looks great! but as trying to find out how you did all this neat stuff i cant help to notice that your .vimrc-link is not working.. This would be of great help

Torgeir at 2006-09-19

You should look into the latest entry.

Toby at 2006-09-19

oh, fast reply.. thanx :)

Torgeir at 2006-09-19

The full Php functions updated method:

CoreSh at 2008-12-22

Note that VIP (containing the .vimrc) is now hosted on Github: http://github.com/tobyS/vip

Toby at 2010-03-10

Your link http://www.wiki.cc/php/Epc_phpm gives 404 error. :-(

Fran at 2011-12-06