The coding style for one project may use CamelCase for variables, while another may use under_scores. Here are some Vim procedures to switch between CamelCase and under_score variable names.
Converting from legacy PHP
This is a simple procedure for manually changing a few 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, then 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.
Change under_scores to CamelCase
The following mapping changes the visually selected text.
" Change selected text from name_like_this to NameLikeThis. vnoremap ,c :s/_\([a-z]\)/\u\1/g<CR>gUl
To use this you would go into Visual Mode (press v) then select the word (iw, which stands for inner word) then type ,c in visual mode.
This changes this_is_my_func to ThisIsMyFunc.
If you want the style to be thisIsMyFunc you can remove the gUl which changes the first character to Uppercase.
How it works
It substitutes the characters so that _a becomes A. The \u\1 uppercases the match. The <CR> hits the enter key for you and the cursor ends up at the beginning of the selection. We use gUl to Uppercase the character under the cursor.
Some might have used the ~ (tilde) to uppercase it, but if it was already uppercase it would have made it lowercase. Also, the tilde moves the cursor to the right which is not what we want.
Change CamelCase to under_scores
The following mapping changes the visually selected text.
" Change selected text from NameLikeThis to name_like_this. vnoremap ,u :s/\<\@!\([A-Z]\)/\_\l\1/g<CR>gul
This changes ThisIsMyFunc to this_is_my_func.
How it works
It substitutes the characters so that A becomes _a. The \_ put in the underscore the \l\1 (which difficult to see that the first is a lowercase L, the second is the number 1) forces the matched character to be lowercase.
The \<\@! tells substitute to ignore the first match. The gul changes the character under the cursor to be lower-case.
See also
Comments
- Following comment applies to the PHP legacy code part of the tip.
I've decided to keep this short tip (which I have expanded) in anticipation that future changes may introduce other methods for converting between different variable name formats. There is probably a substitute command that would help. JohnBeckett 03:33, 5 April 2009 (UTC)
I couldn't resist:
:s#\%($\%(\k\+\)\)\@<=_\(\k\)#\u\1#g
or with very magic:
:s#\v%(\$%(\k+))@<=_(\k)#\u\1#g
--Fritzophrenic 03:54, 22 July 2009 (UTC)
TO DO
- Following comment applies to the "under_scores to CamelCase" and vice versa tips.
- Need to fix some bugs, I think.
- The :s/// will act on each whole line in the selected area. Perhaps fix with \%V before and after pattern.
- The patterns need to use \C to make them independent of 'ignorecase'.
- Probably should add e flag (:s///ge) so no error if not found.
- Can the ,u mapping use a pattern like \C\l\(\u\) so it does not convert an all uppercase name?
- The gUl and gul code (which could be vU or vu) seems to assume something that I can't quite work out at the moment.
JohnBeckett 08:21, September 20, 2010 (UTC)