vim macro to convert CamelCase to lowercase_with_underscores

11

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

Neil G

Posted 2011-04-15T22:06:28.533

Reputation: 402

Answers

19

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

Chris Johnsen

Posted 2011-04-15T22:06:28.533

Reputation: 31 786

Tim Pope is awesome. So many great plugins. – Hendrik – 2014-11-26T14:34:38.983

perfect solution – Neil G – 2011-04-16T07:00:46.367

6

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

Daniel DiPaolo

Posted 2011-04-15T22:06:28.533

Reputation: 863

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 – 2011-04-15T22:49:25.970

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 – 2013-06-28T09:40:45.027

1

I created a command like this:

command! Ctl
    \ exec "norm \"xygn" |
    \ let @y = substitute(@x, "\\([^A-Z]\\)\\([A-Z]\\)", "\\1_\\2", "g") |
    \ let @y = tolower(@y) |
    \ exec "norm cgn\<C-r>y" |
    \ let @@ = ":Ctl\n"

You first must search for a string you want to replace /nameToReplace and then you run :Ctl, and the next search match will become name_to_replace. The command puts itself into the @@ register so you can repeat the action by pressing @@.

Jake

Posted 2011-04-15T22:06:28.533

Reputation: 579

1

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:

Luc Hermitte

Posted 2011-04-15T22:06:28.533

Reputation: 1 575