camelCase to underscore in vi(m)

You might want to try out the Abolish plugin by Tim Pope. It provides a few shortcuts to coerce from one style to another. For example, starting with:

MixedCase

Typing crc [mnemonic: CoeRce to Camelcase] would give you:

mixedCase

Typing crs [mnemonic: CoeRce to Snake_case] would give you:

mixed_case

And typing crm [mnemonic: CoeRce to MixedCase] would take you back to:

MixedCase

If you also install repeat.vim, then you can repeat the coercion commands by pressing the dot key.


This is a bit long, but seems to do the job:

:%s/\<\u\|\l\u/\= join(split(tolower(submatch(0)), '\zs'), '_')/gc


I suppose I should have just kept trying for about 5 more minutes. Well... if anyone is curious:

%s/\(\l\)\(\u\)/\1\_\l\2/gc does the trick.

Actually, I realized this works for camelCase, but not CamelCase, which could also be useful for someone.