Convert snake_case to CamelCase in Vim

acro5piano

Kay Gosho

Posted on April 17, 2018

Convert snake_case to CamelCase in Vim

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.

💖 💪 🙅 🚩
acro5piano
Kay Gosho

Posted on April 17, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related