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
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityThis 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.
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.
Since the introduction of <cmd>
, we can handle visual mode easily
noremap <silent> <c-k> <cmd>call search('\u')<cr>
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.
w
, e
, etc.
Oct 21, 2020 at 3:59
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>
f
command quite handy: For example, to get to theA
, I’s simply typefA
, or, to get to the beginning of theSentence
word, I’d say2fS
. Check:help f
. 8-)