Convert snake_case to CamelCase in Vim

Kay Gosho - Apr 17 '18 - - Dev Community

This is a small snippet.

Create this function in your shell:

# .bashrc / .zshrc

camelcase() {
    perl -pe 's#(_|^)(.)#\u$2#g'
}
Enter fullscreen mode Exit fullscreen mode

If you use fish:

# .config/fish/config.fish

function camelcase
    perl -pe 's#(_|^)(.)#\u$2#g'
end
Enter fullscreen mode Exit fullscreen mode

Then, you can convert standard input to CamelCase:

~> echo array_map | camelcase
ArrayMap
Enter fullscreen mode Exit fullscreen mode

These functions are useful themselves. Furthermore, you can use them in Vim.

Select snake_case lines to be converted, then type !camelcase then press return key.

out.gif

Appendix

You can convert CamelCase to snake_case with this function:

function snakecase
    perl -pe 's#([A-Z])#_\L$1#g' | perl -pe 's#^_##'
end
Enter fullscreen mode Exit fullscreen mode

We can enhance vim using command line function.
Hope this article helps you have a better CUI experience in Vim.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player