Wikia

Vim Tips Wiki

Changes: PHP editing

Edit

Back to page

(move new edit to see also)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 14: Line 14:
   
 
==Move to next variable on current line==
 
==Move to next variable on current line==
PHP variables start with '<tt>$</tt>', for example, <tt>$var</tt>. In normal mode, type <tt>f$</tt> to jump forwards to the next variable; repeat by pressing <tt>;</tt> (next, in same direction), or <tt>,</tt> (next, in opposite direction). Typing <tt>F$</tt> finds '<tt>$</tt>' in the backwards direction. {{help|f}}
+
PHP variables start with '<code>$</code>', for example, <code>$var</code>. In normal mode, type <code>f$</code> to jump forwards to the next variable; repeat by pressing <code>;</code> (next, in same direction), or <code>,</code> (next, in opposite direction). Typing <code>F$</code> finds '<code>$</code>' 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 <tt>L</tt> to jump to the next variable, or <tt>H</tt> to jump to the previous variable:
+
If you do this a lot, you may want to use the following mappings in your [[vimrc]] so you can press <code>L</code> to jump to the next variable, or <code>H</code> to jump to the previous variable:
 
<pre>
 
<pre>
 
noremap L f$
 
noremap L f$
Line 23: Line 23:
   
 
==Fix 4 instead of $==
 
==Fix 4 instead of $==
It's easy to press <tt>4</tt> instead of <tt>$</tt>, resulting in code like this:
+
It's easy to press <code>4</code> instead of <code>$</code>, resulting in code like this:
 
<pre>
 
<pre>
 
$var = "sometext"; echo 4var;
 
$var = "sometext"; echo 4var;
 
</pre>
 
</pre>
   
With the following map, you can quickly fix this problem by typing <tt>\4</tt> while the cursor is still on the same line, after the '4' (this assumes the default backslash <Leader> key):
+
With the following map, you can quickly fix this problem by typing <code>\4</code> while the cursor is still on the same line, after the '4' (this assumes the default backslash <Leader> key):
 
<pre>
 
<pre>
 
nnoremap <Leader>4 F4r$A
 
nnoremap <Leader>4 F4r$A
Line 40: Line 40:
   
 
==Abbreviations to insert debug code==
 
==Abbreviations to insert debug code==
The following abbreviations provide an easy way to enter debug code. In insert mode, type <tt>phpb</tt> then the name of a variable to be displayed.
+
The following abbreviations provide an easy way to enter debug code. In insert mode, type <code>phpb</code> then the name of a variable to be displayed.
 
<pre>
 
<pre>
 
iab phpb exit("<hr>Debug ");
 
iab phpb exit("<hr>Debug ");
Line 46: Line 46:
 
</pre>
 
</pre>
   
These display any variables that have been yanked into register <tt>a</tt>:
+
These display any variables that have been yanked into register <code>a</code>:
 
<pre>
 
<pre>
 
iab phpbb exit("<hr>Debug <C-R>a ");
 
iab phpbb exit("<hr>Debug <C-R>a ");
Line 52: Line 52:
 
</pre>
 
</pre>
   
<tt>Var_dump</tt> is used for displaying arrays.
+
<code>Var_dump</code> is used for displaying arrays.
   
 
This displays all defined variables:
 
This displays all defined variables:
Line 75: Line 75:
 
</pre>
 
</pre>
   
In the above, the <tt>c</tt> flag prompts for confirmation of each change.
+
In the above, the <code>c</code> flag prompts for confirmation of each change.
   
 
==See also==
 
==See also==

Latest revision as of 06:04, July 13, 2012

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 lineEdit

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 $Edit

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 codeEdit

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 notationEdit

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 alsoEdit

CommentsEdit

Around Wikia's network

Random Wiki