27

It is common to use either camelCase or snake_case to separate multiple words which form an identifier. It would be nice if the "word" movements (b, w, e) would detect those inner-identifier boundaries.

How can this behavior be achieved? (Plugin/vimrc/...?)

6
  • 1
    I'd also like to know this for snake_case Feb 10, 2015 at 14:14
  • You may be able to do something with the iskeyword setting, but I fear this will have many additional side-effects ... (see :help word). Feb 10, 2015 at 14:19
  • @Carpetsmoker I thought that it worked for snake_case. Seems that it doesn't, included it in the quesiton.
    – Jasper
    Feb 10, 2015 at 14:45
  • 4
    VimWiki's got you covered: vim.wikia.com/wiki/Moving_through_camel_case_words I'll write up a proper answer at some point if someone else doesn't beat me to it.
    – Rich
    Feb 10, 2015 at 14:48
  • I personally view a camalCased word as a whole word and don't like the idea of breaking half way into a word because of case or underscore. Usually I use the f and t keys to manage inner word movements.
    – Sukima
    Mar 2, 2015 at 5:04

1 Answer 1

16

The plugin CamelCaseMotion does what you want. By default it maps to the keys ,w, ,b, ,e which are equivalent to normal w, b, e. It works for both CamelCase and snake_case:

Example:

camelCaseWord

Using d,w will remove camel; using it again will remove Case.

Or with snake case:

snake_case_word

d,w will remove snake_, and on the second time it will remove case_.

"Inner" motions (such as diw) should also work as expected (as di,w).

If you prefer, you could also override the default w, b, and e with:

map <silent> w <Plug>CamelCaseMotion_w
map <silent> b <Plug>CamelCaseMotion_b
map <silent> e <Plug>CamelCaseMotion_e
sunmap w
sunmap b
sunmap e
omap <silent> iw <Plug>CamelCaseMotion_iw
xmap <silent> iw <Plug>CamelCaseMotion_iw
omap <silent> ib <Plug>CamelCaseMotion_ib
xmap <silent> ib <Plug>CamelCaseMotion_ib
omap <silent> ie <Plug>CamelCaseMotion_ie
xmap <silent> ie <Plug>CamelCaseMotion_ie
4
  • 2
    Note that this plugin's default mappings shadow the , command (repeat last f/t/F/T motion), meaning if you want to use that command, you'll have to wait for a delay (configured by 'timeoutlen'). Fortunately, the plugin allows you to use alternative mappings of your choosing.
    – tommcdo
    Feb 10, 2015 at 22:44
  • There seem to be some side-effects with this plugin, ie: dw on , key); when cursor is at k seems to remove the entire line (including newline), while I expected just key to be removed... Feb 11, 2015 at 0:36
  • 2
    I found this plugin to be better. Combining motion with deletion works properly, and it stops at brackets too.
    – Paschalis
    May 5, 2016 at 14:30
  • Minor point, but in the first example it would surely remove camel and then Case, not Word? Jan 17, 2019 at 10:16

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.