Vim Tips Wiki
(Format and roughly merge in tip 1187)
(Adjust previous/next navigation + minor manual clean)
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1016
 
|id=1016
  +
|previous=1014
|title=Moving through camel case words
 
  +
|next=1017
|created=October 10, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Anthony Van Ham, Gerald Lai
 
|author=Anthony Van Ham, Gerald Lai
 
|version=6.0
 
|version=6.0
 
|rating=14/11
 
|rating=14/11
  +
|category1=
|text=
 
  +
|category2=
 
}}
 
}}
 
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 alternate versions are given, starting with something simple and more easily understood.
   
  +
==Simple move==
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>
 
<pre>
imap &lt;C-Right&gt; ?:call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
+
nnoremap <C-Left> :call search('\<\<Bar>\u', 'bW')<CR>
imap &lt;C-Left&gt; ?:call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
+
nnoremap <C-Right> :call search('\<\<Bar>\u', 'W')<CR>
map &lt;C-Right&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'W')&lt;CR&gt;
+
inoremap <C-Left> <C-o>:call search('\<\<Bar>\u', 'bW')<CR>
map &lt;C-Left&gt; :call search('\&lt;\&lt;Bar&gt;\u', 'Wb')&lt;CR&gt;
+
inoremap <C-Right> <C-o>:call search('\<\<Bar>\u', 'W')<CR>
 
</pre>
 
</pre>
   
  +
==Better handling of all-uppercase words==
== 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.
 
 
<pre>
 
<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 <silent><C-Left> :<C-u>call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
 
nnoremap <silent><C-Right> :<C-u>call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
 
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;
+
inoremap <silent><C-Left> <C-o>:call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
 
inoremap <silent><C-Right> <C-o>:call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
 
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;
 
 
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>
 
</pre>
   
==Enhanced version, originally in tip 1187==
+
==Enhanced version==
 
With the following, the cursor will stop at:
 
With the following, the cursor will stop at:
#The beginning of a word.
+
*The beginning of a word.
#The beginning of a run of camel characters.
+
*The beginning of a run of camel characters.
#A camel character followed by a run of non-camel characters.
+
*A camel character followed by a run of non-camel characters.
#The start of the file.
+
*The start and end of the file.
#The end of the file.
 
 
 
<pre>
 
<pre>
"Use one of the following to define the camel characters.
+
" Use one of the following to define the camel characters.
"Stop on capital letters.
+
" Stop on capital letters.
:let g:camelchar = "A-Z"
+
let g:camelchar = "A-Z"
"Also stop on numbers.
+
" Also stop on numbers.
:let g:camelchar = "A-Z0-9"
+
let g:camelchar = "A-Z0-9"
"Include '.' for class member, ',' for separator, ';' end-statement,
+
" Include '.' for class member, ',' for separator, ';' end-statement,
"and <[< bracket starts and "'` quotes.
+
" and <[< bracket starts and "'` quotes.
:let g:camelchar = "A-Z0-9.,;:{([`'\""
+
let g:camelchar = "A-Z0-9.,;:{([`'\""
 
nnoremap <silent><C-Left> :<C-u>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>
 
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 <silent><C-Right> :<C-u>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>
 
inoremap <silent><C-Left> <C-o>:call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>
 
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 <silent><C-Right> <C-o>:call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>
 
vnoremap <silent><C-Left> :<C-U>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>v`>o
 
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;
+
vnoremap <silent><C-Right> <Esc>`>:<C-U>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>v`<o
 
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>
 
</pre>
   
== Comments ==
+
==Alternatives==
  +
The {{script|id=1905|text=camelcasemotion}} script is based on this tip, but much refined and enhanced. It can move through both CamelCase and underscore_notation words. It provides configurable mappings for normal mode, operator-pending mode and visual mode, plus a corresponding "inner" text object. -- [[User:Inkarkat|Inkarkat]] 12:12, 5 April 2009 (UTC)
I believe that the pattern should start with \C, to force case sensitivity, as so:
 
<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>
 
 
----
 
My addition for visual mode (you need to enter visual mode first)
 
<pre>
 
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>
 
   
 
==Comments==
----
 

Revision as of 02:12, 16 May 2012

Tip 1016 Printable Monobook Previous Next

created 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. Some alternate versions are given, starting with something simple and more easily understood.

Simple move

nnoremap <C-Left> :call search('\<\<Bar>\u', 'bW')<CR>
nnoremap <C-Right> :call search('\<\<Bar>\u', 'W')<CR>
inoremap <C-Left> <C-o>:call search('\<\<Bar>\u', 'bW')<CR>
inoremap <C-Right> <C-o>:call search('\<\<Bar>\u', 'W')<CR>

Better handling of all-uppercase words

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>call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
nnoremap <silent><C-Right> :<C-u>call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
inoremap <silent><C-Left> <C-o>:call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
inoremap <silent><C-Right> <C-o>:call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>

Enhanced version

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 and 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>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>
nnoremap <silent><C-Right> :<C-u>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>
inoremap <silent><C-Left> <C-o>:call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>
inoremap <silent><C-Right> <C-o>:call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>
vnoremap <silent><C-Left> :<C-U>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>v`>o
vnoremap <silent><C-Right> <Esc>`>:<C-U>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>v`<o

Alternatives

The camelcasemotion script is based on this tip, but much refined and enhanced. It can move through both CamelCase and underscore_notation words. It provides configurable mappings for normal mode, operator-pending mode and visual mode, plus a corresponding "inner" text object. -- Inkarkat 12:12, 5 April 2009 (UTC)

Comments