Vim Tips Wiki
(Format and roughly merge in tip 1187)
Line 3: Line 3:
 
|id=1016
 
|id=1016
 
|title=Moving through camel case words
 
|title=Moving through camel case words
|created=October 10, 2005 4:13
+
|created=October 10, 2005
 
|complexity=basic
 
|complexity=basic
|author=Anthony Van Ham
+
|author=Anthony Van Ham, Gerald Lai
 
|version=6.0
 
|version=6.0
 
|rating=14/11
 
|rating=14/11
 
|text=
 
|text=
 
}}
If you want to move through a camel case word, like CamelCaseWordWeWantToMoveThrough, word by word (eg from the 'C' on Camel to the 'C' on Case to the 'W' in word), you can add the following maps to assign Ctrl-right and Ctrl-left to do the job
 
   
  +
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.
   
  +
<pre>
 
imap &lt;C-Right&gt; ?:call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
 
imap &lt;C-Left&gt; ?:call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
  +
map &lt;C-Right&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
  +
map &lt;C-Left&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
  +
</pre>
   
  +
== Comments ==
(inspired by http://groups.google.com/group/comp.editors/browse_thread/thread/863891edbd961691/280b68b9686e346e%23280b68b9686e346e?sa=X&amp;oi=groupsr&amp;start=0&amp;num=2)
 
 
Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time.
   
  +
<pre>
 
nnoremap &lt;silent&gt;&lt;C-Left&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\U\@&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
   
 
nnoremap &lt;silent&gt;&lt;C-Right&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\U\@&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\@!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
   
imap &lt;C-Right&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
+
inoremap &lt;silent&gt;&lt;C-Left&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\U\@&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
   
imap &lt;C-Left&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
+
inoremap &lt;silent&gt;&lt;C-Right&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\U\@&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\@!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
  +
</pre>
   
  +
==Enhanced version, originally in tip 1187==
map &lt;C-Right&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
 
  +
With the following, the cursor will stop at:
  +
#The beginning of a word.
  +
#The beginning of a run of camel characters.
  +
#A camel character followed by a run of non-camel characters.
  +
#The start of the file.
  +
#The end of the file.
   
  +
<pre>
map &lt;C-Left&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
 
  +
"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 &lt;silent&gt;&lt;C-Left&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
   
  +
nnoremap &lt;silent&gt;&lt;C-Right&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
}}
 
  +
  +
inoremap &lt;silent&gt;&lt;C-Left&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
  +
  +
inoremap &lt;silent&gt;&lt;C-Right&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
  +
</pre>
   
 
== Comments ==
 
== Comments ==
  +
I believe that the pattern should start with \C, to force case sensitivity, as so:
This tip is pretty handy, but I just wish it would work in select mode so you could get the behavior common in Windows code editors when you do ctrl+shift+&lt;left&gt;or&lt;right&gt;. Any tips out there for this?
 
  +
<pre>
  +
nnoremap &lt;silent&gt;&lt;C-Left&gt; :&lt;C-u&gt;cal search('\C\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
  +
</pre>
   
'''Anonymous'''
 
, March 16, 2006 12:07
 
 
----
 
----
  +
My addition for visual mode (you need to enter visual mode first)
Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time.
 
  +
<pre>
 
nnoremap &lt;silent&gt;&lt;C-Left&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\U\--AT--&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\--AT--!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
+
vnoremap &lt;silent&gt;&lt;C-Right&gt; &lt;Esc&gt;`&gt;:&lt;C-U&gt;cal search('\C\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;v`&lt;o
  +
vnoremap &lt;silent&gt;&lt;C-Left&gt; :&lt;C-U&gt;cal search('\C\&lt;\&lt;Bar&gt;\%(^\&lt;Bar&gt;[^'.g:camelchar.']\@&lt;=\)['.g:camelchar.']\&lt;Bar&gt;['.g:camelchar.']\ze\%([^'.g:camelchar.']\&amp;\&gt;\@!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;v`&gt;o
 
  +
</pre>
nnoremap &lt;silent&gt;&lt;C-Right&gt; :&lt;C-u&gt;cal search('\&lt;\&lt;Bar&gt;\U\--AT--&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\--AT--!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
 
 
inoremap &lt;silent&gt;&lt;C-Left&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\U\--AT--&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\--AT--!\)\&lt;Bar&gt;\%^','bW')&lt;CR&gt;
 
 
inoremap &lt;silent&gt;&lt;C-Right&gt; &lt;C-o&gt;:cal search('\&lt;\&lt;Bar&gt;\U\--AT--&lt;=\u\&lt;Bar&gt;\u\ze\%(\U\&amp;\&gt;\--AT--!\)\&lt;Bar&gt;\%$','W')&lt;CR&gt;
 
 
   
Gerald Lai
 
, March 27, 2006 17:59
 
 
----
 
----
<!-- parsed by vimtips.py in 0.778504 seconds-->
 

Revision as of 10:13, 29 September 2007

Previous TipNext Tip

Tip: #1016 - Moving through camel case words

Created: October 10, 2005 Complexity: basic Author: Anthony Van Ham, Gerald Lai Version: 6.0 Karma: 14/11 Imported from: Tip#1016

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