Cover image for post Comfortable PHP editing with VIM -6-

Comfortable PHP editing with VIM -6-

I recently added 2 new functions to my PHP FTPlugin for VIM, which I wanted to implement for a longer time and recently needed quite often:

PhpAlign()

Often you have written down an array declaration or a set of variable assignements. Usually things look somewhat ugly the, like

$foo = array( "test" => "test", "foo" => "bar", "something" => "somewhat", "anything more" => "and more and more", );

Aligning this definition properly is an ugly, boring work. The PhpAlign() function takes it from you and aligns the array declaration properly:

$foo = array( "test" => "test", "foo" => "bar", "something" => "somewhat", "anything more" => "and more and more", );

This also works with usual variable assignements:

::

$foo = "bar"; $someVariable = "some value"; $aVar = 23;

becomes

$foo = "bar"; $someVariable = "some value"; $aVar = 23;

PhpUnComment()

Often you want to comment or un-comment a couple of lines, because you currently change those and want to make a backup or simply want to bring alternative code in place. For multiple reasons you may not want to use multi-line commens for this (e.g. because you the closing sequence inside the code or because they simply look ugly. PhpUnComment() simply comments a line which is not commented and un-comments a line that is commented.

function test() { return "test"; } // function test() // { // return 23; // }

Selecting these lines (all of them) and running PhpUnComment() results in:

// function test() // { // return "test"; // } function test() { return 23; }

Both function are mapped to shortcuts in visual mode (<Ctrl>-a for PhpAlign() and <Ctrl>-c for PhpUnComment()), if you are using my FTPlugin.

The changes are already included in my SVN and documented there, too. Do get a recent checkout do:

$ svn co svn://svn.toby.phpugdo.de/PDV

Comments

Tobias, can you remove html entities from code?

> $foo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = "bar"; $someVariable = "some value"; $aVar&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 23;

Pento at 2007-04-30

Hi Pento!

What is the actual problem? Do you see the entities in your browser or something?

Regards, Toby

Toby at 2007-04-30

Yeah, i see them too, Firefox 2.0.0.3, in IE6 everything is ok.

gen2brain at 2007-04-30

The code is full of span class="serendipity_searchQuery" tags which break all the entities into pieces.

Dietrich at 2007-04-30

Seems the problem occured wizth the Serendipity "search highlight" plugin. Thanks for the hint, I deaktivated the plugin now.

Toby at 2007-04-30

Where can we get phpm? =/ All the links on the web are DEAD. Any suggestions?

Benjamin at 2007-05-04

I'm sorry, I could not reach the maintainer of PHPM, too. I think I will re-implement PHPM if I find time...

Regards, Toby

Toby at 2007-05-04

It's a share. It seemed like such a great tool for php/vim users. I haven't been able to find anything else that gives the function prototypes. Anyone found/using anything?

Benjamin at 2007-05-04

There is already an alignment plugin for vim http://vim.sourceforge.net/scripts/script.php?script_id=294

aizatto at 2007-05-12

Ah, thanks, did not see that. I will dig into it and see if I should better use this one! :)

Toby at 2007-05-12

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

Clayton at 2007-06-12

Thanks for the hint! I updated the example!

Toby at 2007-06-12

for all the [inv]noremap mappings, I would change them to: [inv]noremap <buffer> <C-P> .....

the reason for this is so that when I use my mapping it will only apply to php files instead of every new buffer I work on.

test by doing :sp blah.c then <C-P> read about it, :h <buffer>

and, I need <C-P> for autocompletion so I use <C-O> instead.

neither of the above are extremely significant but may be useful to any googler passing through

Clayton at 2007-06-19

Hi Toby... I'm a newbie Vim user looking forward to using it for editing PHP files. I checkout your vim-related files from your svn and explore it a bit.

I wonder, if there is an easier way (keyboard-based) to write if / for / while / etc of-the-like-statements in Vim.

With your .vimrc it goes something like this <i>while ( $i >0 ) <Esc> <$a> {

After entering the conditional expressions (i>0) users need like 4 key-strokes just to type the {. And those keys are quite distant each other (Esc$a{). Could you tell me how to shorten it to fewer keystroke (if possible to 1 keystroke and nearby within finger reach on the alphabet keys, and without conflicting with your current key-mappings?

Thanks in advance. (and your blog here is really helpful indeed for a Vim-newbie to quickstart Vim for PHP)

Fery at 2009-11-29

Note that PDV is now named VIP and hosted on github. For details, please refer to https://schlitt.info/blog/0719_pdv_vip_now_on_github.html

Toby at 2010-02-16

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.)

sound files download at 2010-05-21