Cover photo for post Comfortable PHP editing with VIM

Comfortable PHP editing with VIM

I've always been a vi-hater, until I really came to know it. As most Unix newbes I first did not know in a way how to deal with vi in general and therefore never even touched it for years (I guess in the very first weeks, I did not even know, it exists).

After some month on Linux, someone threw me into cold water and told me to use vi for editing files and I tried, what resulted in the usual process (I imagine):

  1. "Damn, now I'm in here, but I cannot type anything!" *argh``*``

  2. "Bloody shit, I cannot even exit it anymore" *grmpf``*``

By mid of this year some friends tried to evangelise me to use vi and, I bet you know, the were successful. Even that successful, that I bought "Learning the vi editor"*a few weeks after they convinced me to try out vim. This is now nearly 6 month ago and I slowly start really getting used to some of it's neat functions and have some fixed set of settings, I'd like to provide to others.

Beside the usual options like autoindent, smartindent, nowrap and number, I created some nice shortcuts (for checking PHP syntax and for running "pear package" directly from the editor. But the most important feature I really like is creating code skeletons on the fly, using key mappings.

So, what does that basically mean? There are quite some constructs in PHP which you really use often, like if-statements, foreach-loops, class/method/attribute-definitions and so on. Usually people write that most of these once in a file (mostly with those definition parts) and then copy and paste the stuff. What I do since a few month now, is just typing (e.g.) "=cla" in vim's editing mode and a class defintion (including comments) appears and leaves me on a place where I can comfortably start filling it. Same for the other named constructs and some more.

I guess I sped up my code-writing speed by about 15% with that and it's pretty comfortable. So, if you like, check my current .vimrc here and try it out! Feedback, additions and hints are pretty welcome! :)

*A big recommendation on that book to everyone out there who's new to vi or one of it's clones!

Comments

Try adding this to your vimrc:

And grab http://lerdorf.com/funclist.txt and copy it to /usr/share

Then hit Ctrl-N or Ctrl-P as you are typing a function name. Read up on vim's dictionary completion feature to learn more.

Rasmus at 2004-11-12

VIM is really a great editor! Note that you can use <cr> instead of ^M in your vimrc, this makes it easier to read or view in other applications.

Here are some more mappings for automatically closing quotes and parentheses:

Hope you find this useful.

Jörn Horstmann at 2004-11-12

Rasmus, thanks a lot for your addition! Sounds really pretty useful!

Toby at 2004-11-12

Thanks! I always enjoyed that feature in other editors and wondered how to get that into VIM. :) Beside that, I cannot imagine, which character you meant instead of ^M. Maybe you could write me an email about that?

Toby at 2004-11-12

Hi Rasmus,

That function completion stuff is a gem :) I'm eventually getting my vim to behave better seeing that we have a vim zealot here at work and various tips I've started picking up from various sources.

Jacques at 2004-11-12

I've been playing with my .vimrc today (and thanks Toby for the starting point), and changed these to:

Which will place the cursor between them instead of having to move it manually. I'm lazy.

I also discovered Cream [http://cream.sf.net], which is like Vim without relying so much on editing modes, which are easily the thing that annoy me most. It also supports defining code templates to be expanded be hitting <Shift-Space>x2.

My .cream-user file is at http://www.jellybob.co.uk/~jon/cream.rc for the curious.

Jon Wood at 2004-11-13

Thanks, Jon! Please see Jörns entry for a better solution especially with {}. Jörns one does autoindent correctly.

(Jon's entry was posted when the above so crappy shown &gt; and &lt; signs were not present due to some wired stuff in this blog-software.)

Toby at 2004-11-15
Josh at 2005-05-27

You might like to have a look at my PHP5 template VIM filetype plugin, at http://www.vim.org/scripts/script.php?script_id=1307

Sérgio Carvalho at 2005-06-20

Well I do not like general abbreviations in my .vimrc.

So I done all that stuff in a special file called "php_abb.vim" located in my ~/.vim/ftplugin folder.

It is sourced when editing php-files. So I also put:

into the "augroup filedetect"-section of ~/.vim/filetype.vim and

into the same file.

The abbreviations are just set while working on php then. I am doing a lot of other stuff with vim so I don't get bothered of these settings while writing mails or other non-php related things.

Btw: Thank you for your nice work here!

Yours, wolle

Wolfgang Jacobs at 2006-01-28

Hi!

Please consider my more up2date blogentries anbout VIM (nr 4 is the most actual in the series).

:)

Cheers! Toby

Toby at 2006-01-28

Dead links on some of your a tags.

anonymous at 2006-05-11