sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #1187: Extension of cursor through CaMeL case

 tip karma   Rating 7/4, Viewed by 856 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   March 27, 2006 21:19      complexity:   intermediate
author:   Gerald Lai      as of Vim:   5.7

This is an extension of

  vimtip#1016: Moving through camel case words

Please read it to understand the basis of this following tip.

In the basic case of vimtip#1016, the cursor would move through capital letters and word beginnings. Essentially,

  :let g:camelchar = "A-Z"

would be the regex that defines how Ctrl-Left and Ctrl-Right would move the cursor backwards and forward, respectively.

We could include numbers, for instance, since there are times when numbers are lodged in together with an alphabetical word, or in equations, and we wish to have the cursor stop on the number itself for us to edit:

  :let g:camelchar = "A-Z0-9"

We could then extend this to include "." to denote end-of-sentence or class member, "," to denote list separator, ";" to denote end-of-programming-statement, {[( bracket starts, and "'` quotes to define strings/items, etc.

  :let g:camelchar = "A-Z0-9.,;:{([`'\""

This tip is basically a customized Shift-Left/Right functionality. The cursor will either stop at

1. the beginning of a word
2. the beginning of a run of g:camelchar's
3. a g:camelchar followed by a run of non-g:camelchar's
4. the start of the file
5. the end of the file

"place these 4 lines in vimrc
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>

 rate this tip  Life Changing Helpful Unfulfilling 

<< change to directory of the opened file | Use ptags.py to generate tags file for python >>

Additional Notes

Anonymous, April 4, 2006 10:55
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>
Anonymous, April 6, 2006 10:10
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
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo