Vim Tips Wiki
(Format and roughly merge in tip 1187)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1016
 
|id=1016
  +
|previous=1015
|title=Moving through camel case words
 
  +
|next=1017
 
|created=October 10, 2005
 
|created=October 10, 2005
 
|complexity=basic
 
|complexity=basic
Line 8: Line 9:
 
|version=6.0
 
|version=6.0
 
|rating=14/11
 
|rating=14/11
|text=
 
 
}}
 
}}
 
 
Some programs use CamelCaseWords (or camelCaseWords). The following maps allow you to move word by word, for example, from the 'C' of Camel, to the 'C' of Case, to the 'W' of Words.
 
Some programs use CamelCaseWords (or camelCaseWords). The following maps allow you to move word by word, for example, from the 'C' of Camel, to the 'C' of Case, to the 'W' of Words.
   
Line 20: Line 19:
 
</pre>
 
</pre>
   
== Comments ==
+
==Comments==
 
Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time.
 
Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time.
   
Line 60: Line 59:
 
</pre>
 
</pre>
   
== Comments ==
+
==Comments==
 
I believe that the pattern should start with \C, to force case sensitivity, as so:
 
I believe that the pattern should start with \C, to force case sensitivity, as so:
 
<pre>
 
<pre>

Revision as of 03:34, 16 December 2007

Tip 1016 Printable Monobook Previous Next

created October 10, 2005 · complexity basic · author Anthony Van Ham, Gerald Lai · version 6.0


Some programs use CamelCaseWords (or camelCaseWords). The following maps allow you to move word by word, for example, from the 'C' of Camel, to the 'C' of Case, to the 'W' of Words.

imap <C-Right> ?:call search('\<\<Bar>\u', 'W')<CR>
imap <C-Left> ?:call search('\<\<Bar>\u', 'Wb')<CR>
map <C-Right> :call search('\<\<Bar>\u', 'W')<CR>
map <C-Left> :call search('\<\<Bar>\u', 'Wb')<CR>

Comments

Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time.

nnoremap <silent><C-Left> :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>

nnoremap <silent><C-Right> :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>

inoremap <silent><C-Left> <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>

inoremap <silent><C-Right> <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>

Enhanced version, originally in tip 1187

With the following, the cursor will stop at:

  1. The beginning of a word.
  2. The beginning of a run of camel characters.
  3. A camel character followed by a run of non-camel characters.
  4. The start of the file.
  5. The end of the file.
"Use one of the following to define the camel characters.
"Stop on capital letters.
:let g:camelchar = "A-Z"
"Also stop on numbers.
:let g:camelchar = "A-Z0-9"
"Include '.' for class member, ',' for separator, ';' end-statement,
"and <[< bracket starts and "'` quotes.
:let g:camelchar = "A-Z0-9.,;:{([`'\""

nnoremap <silent><C-Left> :<C-u>cal search('\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>

nnoremap <silent><C-Right> :<C-u>cal search('\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>

inoremap <silent><C-Left> <C-o>:cal search('\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>

inoremap <silent><C-Right> <C-o>:cal search('\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>

Comments

I believe that the pattern should start with \C, to force case sensitivity, as so:

nnoremap <silent><C-Left> :<C-u>cal search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>

My addition for visual mode (you need to enter visual mode first)

vnoremap <silent><C-Right> <Esc>`>:<C-U>cal search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>v`<o
vnoremap <silent><C-Left> :<C-U>cal search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>v`>o