Vim Tips Wiki
Register
Advertisement
Tip 1025 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


Here are some suggestions that may help when editing PHP files.

Move to next variable on current line[]

PHP variables start with '$', for example, $var. In normal mode, type f$ to jump forwards to the next variable; repeat by pressing ; (next, in same direction), or , (next, in opposite direction). Typing F$ finds '$' in the backwards direction. :help f

If you do this a lot, you may want to use the following mappings in your vimrc so you can press L to jump to the next variable, or H to jump to the previous variable:

noremap L f$
noremap H F$

Fix 4 instead of $[]

It's easy to press 4 instead of $, resulting in code like this:

$var = "sometext"; echo 4var;

With the following map, you can quickly fix this problem by typing \4 while the cursor is still on the same line, after the '4' (this assumes the default backslash <Leader> key):

nnoremap <Leader>4 F4r$A

Here is an alternative that returns the cursor to its initial position:

nnoremap <Leader>4 m`F4r$``
inoremap <Leader>4 <Esc>m`F4r$``a

Abbreviations to insert debug code[]

The following abbreviations provide an easy way to enter debug code. In insert mode, type phpb then the name of a variable to be displayed.

iab phpb exit("<hr>Debug ");
iab phpv echo "<hr><pre>";var_dump($a);exit("debug ");

These display any variables that have been yanked into register a:

iab phpbb exit("<hr>Debug <C-R>a ");
iab phpvv echo "<hr><pre>";var_dump(<C-R>a);exit("debug ");

Var_dump is used for displaying arrays.

This displays all defined variables:

iab phpallv print_r(get_defined_vars());

Replace associative with object style notation[]

The substitute command presented below looks for each PHP array reference like:

$a = $some_array['key_name'];

and replaces it with:

$a = $some_array->key_name;

Command:

:%s/\['\(.\{-}\)'\]/->\1/gc

In the above, the c flag prompts for confirmation of each change.

See also[]

Comments[]

Advertisement