Skip to content

Instantly share code, notes, and snippets.

@hit9
Last active July 26, 2024 13:34
Show Gist options
  • Save hit9/ce2cbb9f37d9ece360e03b47591bf4fc to your computer and use it in GitHub Desktop.
Save hit9/ce2cbb9f37d9ece360e03b47591bf4fc to your computer and use it in GitHub Desktop.
vim change name case style
"Word case converters (\%V runs substitute on visual selections). --- {{{
"Convert selected text to snake_case style:
"firstly lower the first letter, then make each upper letter lower
"and insert a underscore in front of that.
command! -range ToSnake :s/\%V\<./\l&/e | s/\%V\u/_\l&/e
"Convert selected text to camelCase style:
"Find the underscores and upper the following letter, also removing the underscore.
"Then make sure the first letter is in lower case.
command! -range ToCamel :s/\%V_\([^_]\)/\u\1/e | s/\%V\<./\l&/e
"Convert selected text to PascalCase style:
"Find the underscores and upper the following letter, also removing the underscore.
"Then make sure the first letter is in upper case.
command! -range ToPascal :s/\%V_\([^_]\)/\u\1/e | s/\%V\<./\u&/e
"Convert selected text to UPPER_SNAKE_CASE style:
"firstly insert a underscore between lower and upper letters.
"and then makes the whole string upper.
command! -range ToUpperSnake :s/\%V\(\l\)\(\u\)/\1_\2/e | s/\%V\w\+/\U&/e
"--- }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment