The coding style for one project may use <tt>CamelCase</tt> for variables, while another may use <tt>under_scores</tt>. Here are some Vim procedures to switch between CamelCase and under_score variable names.

+

The coding style for one project may use <code>CamelCase</code> for variables, while another may use <code>under_scores</code>. Here are some Vim procedures to switch between CamelCase and under_score variable names.

==Converting from legacy PHP==

==Converting from legacy PHP==

−

This is a simple procedure to change variables names from <tt>$this_variable_style</tt> to <tt>$thisVariableStyle</tt>.

+

This is a simple procedure to change variables names from <code>$this_variable_style</code> to <code>$thisVariableStyle</code>.

The commands below define these mappings:

The commands below define these mappings:

−

*<tt>+</tt> &nbsp; Find the next <tt>$variable_with_underscores</tt>.

+

*<code>+</code> &nbsp; Find the next <code>$variable_with_underscores</code>.

−

*<tt>_</tt> &nbsp; Convert the next underscore on the current line.

+

*<code>_</code> &nbsp; Convert the next underscore on the current line.

−

When required, you can yank the following lines in Vim (on the first line, type <tt>2Y</tt>), then execute them (type <tt>@"</tt>) to map the <tt>+</tt> and <tt>_</tt> keys.

+

When required, you can yank the following lines in Vim (on the first line, type <code>2Y</code>), then execute them (type <code>@"</code>) to map the <code>+</code> and <code>_</code> keys.

<pre>

<pre>

:nnoremap + /\$\w\+_<CR>

:nnoremap + /\$\w\+_<CR>

Line 26:

Line 26:

</pre>

</pre>

−

Now you can press <tt>+</tt> to search for the next <tt>$variable_with_underscores</tt>, then press <tt>_</tt> to find and delete the next underscore and toggle the case of the next character. Repeatedly press <tt>_</tt> until all underscores are processed, then press <tt>+</tt> to find the next variable. For example, you may type <tt>+__+_+___</tt> to skip through a file.

+

Now you can press <code>+</code> to search for the next <code>$variable_with_underscores</code>, then press <code>_</code> to find and delete the next underscore and toggle the case of the next character. Repeatedly press <code>_</code> until all underscores are processed, then press <code>+</code> to find the next variable. For example, you may type <code>+__+_+___</code> to skip through a file.

−

Type <tt>+~</tt> for initial capitals.

+

Type <code>+~</code> for initial capitals.

−

The simple procedure above is suitable for manually changing a small number of variables, while inspecting each change. Using a substitute, the process can be automated. The following command will change all variables names from <tt>$this_variable_style</tt> to <tt>$thisVariableStyle</tt>:

+

The simple procedure above is suitable for manually changing a small number of variables, while inspecting each change. Using a substitute, the process can be automated. The following command will change all variables names from <code>$this_variable_style</code> to <code>$thisVariableStyle</code>:

<pre>

<pre>

:%s#\%($\%(\k\+\)\)\@<=_\(\k\)#\u\1#g

:%s#\%($\%(\k\+\)\)\@<=_\(\k\)#\u\1#g

</pre>

</pre>

−

If wanted, the <tt>\v</tt> (very magic) option can be used to reduce the number of backslashes, and the conventional <tt>/</tt> can be used as the delimiter instead of <tt>#</tt>. The following command is equivalent to the above:

+

If wanted, the <code>\v</code> (very magic) option can be used to reduce the number of backslashes, and the conventional <code>/</code> can be used as the delimiter instead of <code>#</code>. The following command is equivalent to the above:

<pre>

<pre>

:%s/\v%(\$%(\k+))@<=_(\k)/\u\1/g

:%s/\v%(\$%(\k+))@<=_(\k)/\u\1/g

Line 55:

Line 55:

</pre>

</pre>

−

Copy the above text into Vim. In Vim, move the cursor to the first substitute command and press <tt>Y</tt> to copy the line. Move the cursor to the line containing the underscore names in the test text and type <tt>@"</tt> to execute the copied substitute command. That will change each underscore name to camel case in the current line.

+

Copy the above text into Vim. In Vim, move the cursor to the first substitute command and press <code>Y</code> to copy the line. Move the cursor to the line containing the underscore names in the test text and type <code>@"</code> to execute the copied substitute command. That will change each underscore name to camel case in the current line.

−

The command uses the <tt>\l</tt> pattern to search for lowercase letters, so it will work correctly to convert <tt>abc_def_ghi</tt> to <tt>AbcDefGhi</tt>, but it will convert <tt>abc1_def2_ghi</tt> to <tt>abc1Def2Ghi</tt> (where the first character is still lowercase).

+

The command uses the <code>\l</code> pattern to search for lowercase letters, so it will work correctly to convert <code>abc_def_ghi</code> to <code>AbcDefGhi</code>, but it will convert <code>abc1_def2_ghi</code> to <code>abc1Def2Ghi</code> (where the first character is still lowercase).

==Change CamelCase to under_scores==

==Change CamelCase to under_scores==

Line 73:

Line 73:

</pre>

</pre>

−

If wanted, the substitute commands can be applied to the whole buffer using <tt>%</tt> to indicate "all lines". For example, the second command applied to the whole buffer would be:

+

If wanted, the substitute commands can be applied to the whole buffer using <code>%</code> to indicate "all lines". For example, the second command applied to the whole buffer would be:

Contents

Converting from legacy PHP

This is a simple procedure to change variables names from $this_variable_style to $thisVariableStyle.

The commands below define these mappings:

+ Find the next $variable_with_underscores.

_ Convert the next underscore on the current line.

When required, you can yank the following lines in Vim (on the first line, type 2Y), then execute them (type @") to map the + and _ keys.

:nnoremap + /\$\w\+_<CR>
:nnoremap _ f_x~

Now you can press + to search for the next $variable_with_underscores, then press _ to find and delete the next underscore and toggle the case of the next character. Repeatedly press _ until all underscores are processed, then press + to find the next variable. For example, you may type +__+_+___ to skip through a file.

Type +~ for initial capitals.

The simple procedure above is suitable for manually changing a small number of variables, while inspecting each change. Using a substitute, the process can be automated. The following command will change all variables names from $this_variable_style to $thisVariableStyle:

:%s#\%($\%(\k\+\)\)\@<=_\(\k\)#\u\1#g

If wanted, the \v (very magic) option can be used to reduce the number of backslashes, and the conventional / can be used as the delimiter instead of #. The following command is equivalent to the above:

:%s/\v%(\$%(\k+))@<=_(\k)/\u\1/g

Change under_scores to CamelCase

The following shows two substitute commands for converting names with underscores to camel case. The first command converts the beginning character to uppercase, while the second leaves it unchanged. Both commands operate on all underscore names in the current line.

" Convert each name_like_this to NameLikeThis in current line.
:s#\(\%(\<\l\+\)\%(_\)\@=\)\|_\(\l\)#\u\1\2#g
" Convert each name_like_this to nameLikeThis in current line.
:s#_\(\l\)#\u\1#g
" Test (first line is original; second and third are results from above).
" CONSTANT ab_cd_ef some words name_like_this and another_name = some_more
" CONSTANT AbCdEf some words NameLikeThis and AnotherName = SomeMore
" CONSTANT abCdEf some words nameLikeThis and anotherName = someMore

Copy the above text into Vim. In Vim, move the cursor to the first substitute command and press Y to copy the line. Move the cursor to the line containing the underscore names in the test text and type @" to execute the copied substitute command. That will change each underscore name to camel case in the current line.

The command uses the \l pattern to search for lowercase letters, so it will work correctly to convert abc_def_ghi to AbcDefGhi, but it will convert abc1_def2_ghi to abc1Def2Ghi (where the first character is still lowercase).

Change CamelCase to under_scores

The following shows two substitute commands for converting camel case names to names with underscores. The first command is slightly simpler but fails if the name contains numbers. Both commands operate on all camel case names in the current line.

" Convert each NameLikeThis to name_like_this in current line.
:s#\(\<\u\l\+\|\l\+\)\(\u\)#\l\1_\l\2#g
" Alternative: accept numbers in name.
:s#\C\(\<\u[a-z0-9]\+\|[a-z0-9]\+\)\(\u\)#\l\1_\l\2#g
" Test (first line is original; second is result from above).
" CONSTANT AbCdEf some words NameLikeThis and AnotherName = someMore
" CONSTANT ab_cd_ef some words name_like_this and another_name = some_more

If wanted, the substitute commands can be applied to the whole buffer using % to indicate "all lines". For example, the second command applied to the whole buffer would be:

:%s#\C\(\<\u[a-z0-9]\+\|[a-z0-9]\+\)\(\u\)#\l\1_\l\2#g

Be sure to run a diff to verify that the substitute has changed what you want.

We couldn't get any of the above to work but succeded with the following command:

:1,$s/_\([a-z]\)/\u\1/g

It looks for any places with an underscore followed by a lower case letter and replaces that with an upper case letter.