10

How would I use set iskeyword to be able to use w to count Capital letters as words?

For example for the following sentence,

ThisIsAShortSentence

Pressing w will jump from T -> I -> A -> S -> S

1
  • 1
    FWIW, in cases like this I find the normal f command quite handy: For example, to get to the A, I’s simply type fA, or, to get to the beginning of the Sentence word, I’d say 2fS. Check :help f. 8-) Sep 20, 2017 at 7:50

3 Answers 3

8

This is not how w and iskeyword works. Consider this example:

!his!s!!hort!entence
^^  ^^^ ^   ^^

Pressing w repeatedly, you will see the cursor stops where sequences of !s meet sequences of alpha characters- there are 8 stops in total (including the first position). Put differently, w will only skip over sequences of non-keyword characters if those characters happen to be spaces, tabs, or EOL. Otherwise, it will stop at symbols before moving on to the next word.

Alternative

I would recommend not removing alpha characters from iskeyword because its effect is too far-reaching. Consider the following map:

nnoremap <c-k> :<c-u>call search('\u')<cr>

Pressing CTRL-K will move to the next upper case character.

Update for recent vim

Since the introduction of <cmd>, we can handle visual mode easily

noremap <silent> <c-k> <cmd>call search('\u')<cr>
1
  • Unfortunately, it does not work with visual selection like w and b do.
    – ricko zoe
    Nov 20 at 22:17
5

You may be interested in a plugin like camelCaseMotion which defines some new motions to navigate in the different parts of a camel case word.

It will not override w cause that's not how this motion is designed to work but it will provide some functions that you can map to the key sequence that you want.

Note that it should also work with snake_case words.

1
  • Actually, CamelCaseMotion has some code in their readme that can be pasted into your .vimrc to override w, e, etc.
    – apostl3pol
    Oct 21, 2020 at 3:59
2

I have copied this wonderful solution from somewhere, but i don't know the source anymore. So it is not my work, but a wonderful documented solution. I only use it as text-object in commands with H and L but everything else is there als comment.

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

" Move CamelCase with H & L but only as text-object
onoremap <silent>H :<C-u>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%^','bW')<CR>
onoremap <silent>L :<C-u>call search('\C\<\<Bar>\%(^\<Bar>[^'.g:camelchar.']\@<=\)['.g:camelchar.']\<Bar>['.g:camelchar.']\ze\%([^'.g:camelchar.']\&\>\@!\)\<Bar>\%$','W')<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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