vim macro to convert CamelCase to lowercase_with_underscores

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

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

Tags:

Vim

Macros