Is there a vim macro to convert CamelCase to lowercase_with_underscores and vice versa?

link|improve this question
feedback

2 Answers

up vote 2 down vote accepted

Tim Pope’s abolish.vim can convert among camelCase, MixedCase, snake_case, and UPPER_CASE, as well as convert (one-way) to dash-case.

Position the cursor on any of fooBar, FooBar, foo_bar, or FOO_BAR and use

  • crc to convert to fooBar
  • crm to convert to FooBar
  • cr_ or
    crs to convert to foo_bar
  • cru to convert to FOO_BAR
  • cr- to convert to foo-bar
link|improve this answer
perfect solution – Neil G Apr 16 '11 at 7:00
feedback

Yes there is, and as a bonus there's one there to go the opposite direction as well!

Quote from the wiki in case it goes away:

" Change selected text from NameLikeThis to name_like_this.
vnoremap ,u :s/\<\@!\([A-Z]\)/\_\l\1/g<CR>gul

and for the opposite direction:

" Change selected text from name_like_this to NameLikeThis.
vnoremap ,c :s/_\([a-z]\)/\u\1/g<CR>gUl
link|improve this answer
Thanks! It's almost perfect, except on my vim, it doesn't change the first letter of the CamelCase name. I'm not sure why. – Neil G Apr 15 '11 at 22:49
feedback

Your Answer

 
or
required, but never shown

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