Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 13 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
    
Tim Pope is awesome. So many great plugins. – Hendrik Nov 26 '14 at 14:34

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

lh-dev also provides commands to convert between naming styles:

:NameConvert snake

will convert the word under the cursor to snake_case.

:%ConvertNames/\<m_\k\+(/getter/gc

will convert each occurrence of the pattern to a getter name, assuming the user confirms the transformation (:h :s_flags)

The styles supported are of two kinds:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .