Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 6 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
share|improve this answer
    
perfect solution –  Neil G Apr 16 '11 at 7:00

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
share|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
    
Very nice. I did find I needed to make this change for the snake to camel direction: vnoremap tos :s/\C\(\U\&\S\)\([A-Z]\)/\1_\l\2/g<CR> –  Jonah Jun 28 '13 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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