Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there an easy way to move to the next capital letter with vim? I'm often working with camel-cased variables and it could be useful.

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

There are a few scripts out there to redefine the word motion commands (b, e, w) to stop at capital letters in CamelCase words; camelcasemotion looks well-established (disclaimer: I've never used it). The Vim wiki has a few examples of simpler scripts if you prefer to do it yourself. Here's a relatively simple way to remap C-Left and C-Right to handle caml-cased words.

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>
share|improve this answer
    
+1 because using search() doesn't screw up your current search pattern –  jw013 Oct 6 '11 at 0:34
    
Accepted because of CamelCaseMotion. –  greg0ire Feb 5 '12 at 16:00
    
More awesomness on this page –  greg0ire Feb 5 '12 at 16:10
add comment

The following Vim command maps the Control-t key combination (use any convenient one you wish) to the command /[A-Z] (followed by return), which will search for the next uppercase letter in "normal mode" (command mode). You can put this in your .vimrc file if you wish (minus the :). The <c-t> and <return> is typed as I've written them here, and Vim will understand.

:nmap <c-t> /[A-Z]<return>
share|improve this answer
    
+1, it works! Every capital letter gets highlighted though... I was looking for something more native but I'm not sure it exists. –  greg0ire Oct 5 '11 at 21:52
1  
@greg0ire: Yeah, that's annoying. You have the hlsearch option set for one reason or other. Unset it with the :nolhsearch command. –  Kusalananda Oct 5 '11 at 22:03
    
@Kusalananda: try nmap <tab> :call search('[A-Z]', 'W')<CR> –  kev Nov 10 '11 at 5:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.