20

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/...?)

  • 1
    I'd also like to know this for snake_case – Martin Tournoij Feb 10 '15 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). – Martin Tournoij Feb 10 '15 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 '15 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 '15 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 '15 at 5:04
11

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
  • 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 '15 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... – Martin Tournoij Feb 11 '15 at 0:36
  • 1
    I found this plugin to be better. Combining motion with deletion works properly, and it stops at brackets too. – Paschalis May 5 '16 at 14:30
  • Minor point, but in the first example it would surely remove camel and then Case, not Word? – preferred_anon Jan 17 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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