Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(→‎Comments: onoremap is for use with commands)
Tag: sourceedit
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1016
 
|id=1016
|previous=1015
+
|previous=1014
 
|next=1017
 
|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=
  +
|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 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==
 
<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 <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</pre>
   
  +
==Alternatives==
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;
 
  +
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)
 
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:
 
<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>
 
   
  +
This is nice but what I am looking for is motions that work with other commands like v,y,d,c etc.
----
 
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>
 
   
  +
:Try adapting them to use <code>:onoremap</code> for operator-pending mode. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 21:27, April 19, 2016 (UTC)
----
 

Revision as of 21:27, 19 April 2016

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

This is nice but what I am looking for is motions that work with other commands like v,y,d,c etc.

Try adapting them to use :onoremap for operator-pending mode. --Fritzophrenic (talk) 21:27, April 19, 2016 (UTC)