JohnBeckett (talk | contribs) (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= |
+ | |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= |
||
}} |
}} |
||
⚫ | |||
+ | ==Simple move== |
||
⚫ | |||
− | |||
<pre> |
<pre> |
||
− | + | 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> |
|
</pre> |
</pre> |
||
+ | ==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. |
Here is a more thorough version. It does not go through a CAPITALIZED word one character at a time. |
||
− | |||
<pre> |
<pre> |
||
− | nnoremap |
+ | nnoremap <silent><C-Left> :<C-u>call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR> |
⚫ | |||
− | |||
− | + | inoremap <silent><C-Left> <C-o>:call search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR> |
|
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
</pre> |
</pre> |
||
− | ==Enhanced version |
+ | ==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 run of camel characters. |
|
− | + | *A camel character followed by a run of non-camel characters. |
|
− | + | *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" |
|
− | "Also stop on numbers. |
+ | " Also stop on numbers. |
− | + | 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.,;:{([`'\"" |
|
⚫ | |||
− | |||
− | nnoremap |
+ | 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-Right> <C-o>:call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR> |
|
⚫ | |||
− | |||
− | + | 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> |
</pre> |
||
− | == |
+ | ==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> |
||
⚫ | |||
− | </pre> |
||
− | |||
− | ---- |
||
− | My addition for visual mode (you need to enter visual mode first) |
||
− | <pre> |
||
− | 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 |
||
⚫ | |||
− | </pre> |
||
⚫ | |||
− | ---- |
Revision as of 02:12, 16 May 2012
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)