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

Comfortable PHP editing with VIM -2-

I updated my .vimrc regarding the comments posted for my first entry about "Comfortable PHP editing with VIM". You can see and download it here.

What I changed (in short):

  • Added remappings for automatic completion of brackets and quotation marks.

  • Added Rasmus' dictionary completion of PHP functions.

  • Updated code templates regarding these changes.

  • Switched to the more readable <...> syntax for special characters.

To use the dictionary completion you have to grab Rasmus' function list from here.

I also had a look at Cream which has been recommended in a comment, but pure VIM seems to be enough for me. :)

See the extended entry of a detailed description, what the provided .vimrc gives you.

PHP Syntax checking

By pressing <CTRL>+B in command mode, the current file will be given to the PHP CLI to perform a syntax check. Since I have PHP4 and PHP5 installed in paralell I call "php4" for checking. This has to be changed depending on your system.

PEAR package

By pressing <CTRL>+P in command mode, the current file will be called with the "pear package" command, which will build a package as defined in the file. Though, the file currently edited should be a package.xml.

Standard compliant indentation

Indentation will be performed automatically (forth and back) using 4 spaces.

Line numbers

Line numbers will be diplayed in front of the code by default.

Automatic completion of delimiters

Delimiters like brackets and quotes will be automatically completed. The cursor will afterwards reside between the delimiters. E.g.: You type " and will end up between "".

Auto completion for PHP functions

You just have to hit <CTRL>+K when you typed the beginning of a PHP function to get it completed automatically or get a list of possible completions. (Attention the function list provided by Rasmus is needed! You also have to customize the location of it to fulfill the needs of your environment!)

Code templates

Automatically generated code templates are available in editor mode through specific strings. For example one can type =pfu and get a skeleton for a private method, including documentation using phpdoc tags. The available code templates are:

=req

require

=roq

require_once

=inc

include

=ioc

include_once

=def

define

=cla

class

=puf

public function

=prf

private function

=pua

public $

=pra

private $

=for

for

=foe

foreach

=whi

while

=swi

switch

=if

if

Comments

Another great feature in vim, is the ability to work with several files at the same time. Add this to .vimrc:

" Allow editing of several files without having to save the current one, before moving to another set hidden " map F12 to close a buffer map <F12> :db^M " Move to next buffer map ^N :bn^M " Move to previous buffer map ^B :bp^M " List open buffers map ^L :ls^M

Then use the 'e' command like:

e: text.php

This will open text.php in a seperat buffer, and with CTRL-n or CTRL-p you can navigate between them.

Dennis at 2004-11-24

Hello i also wrote a introduction to vim. More like a small cheat sheet. That incluse some tricks and covers other extension like DBext let you build queries (say you forgot the table name, field name ...) It has auto completation.

It also covers the CVS extension.

It is always usefull to have these tools included in VIM.

http://www.phpquebec.com/modules/mydownloads/visit.php?cid=2&lid=35

I hope this helps. Sorry the document is in french but i guess some of you can understand the concept :)

Yann Larrivee at 2004-11-25

Just a minor point: the main text uses "=pfu" as an example of inserting a private function, while it's actually "=puf". Had me stumped for a while because I naturally used the example in the text while trying to test it! Once I realised the mistake it worked fine :-)

Jonathan Oxer at 2005-02-09

Great tips. Here is another one: Save file and run php CLI on it: imap <F9> :w!<CR>:!php %<CR>

aurelian at 2005-03-09

The buffering is nice, but i think my vim is crashing every time i request a non-existent buffer with ctrl+n/p. Isnt it possible to check for that before it calls the "next" "previous". And the filename in the top of the prompt would help aswell.

Very neat config Tobias, keep up the good work.

Martin Vium at 2005-07-14

It sounds good, but the link to the .vimrc file does not work!!!!

ERROR 404

:-(

Tim Kersten at 2006-05-16

Sorry, please refer to the newer version (from part 3 of the article).

Toby at 2006-05-16

Sweet, thanks :-)

Tim Kersten at 2006-05-17

Great article and tips - but all the links to the .vimrc file are broken, even the one given in reply to the previous commenter who reported the same problem.

Pat at 2007-07-12

Hi!

As you can see in the latest posts of my series, the files are all available through my SVN: http://svn.toby.phpugdo.de/PDV

Cheers! Toby

Toby at 2007-07-14

Just a minor point: the main text uses "=pfu" as an example of inserting a private function, while it's actually "=puf". A little slip of the pen - nothing terrible

programming help at 2010-04-01

Just a comment that the example usage displayed in php-doc.vim should contain ":call PhpDoc()" instead of ":exe PhpDoc()" It will try to execute the returned string when using :exe

downloadable files at 2010-05-21