r/vim • • Mar 24 '20

Vim-camelsnek: Quickly convert between camelCase and snek_case

https://github.com/nicwest/vim-camelsnek
28 Upvotes

9 comments sorted by

19

u/sunnynuts Mar 25 '20

Coercion is built in to Tim Pope's abolish plugin.

Coercion

Want to turn fooBar into foo_bar? Press crs (coerce to snake_case). MixedCase (crm), camelCase (crc), snake_case (crs), UPPER_CASE (cru), dash-case (cr-), dot.case (cr.), space case (cr<space>), and Title Case (crt) are all just 3 keystrokes away.

I use it all the time!

1

u/chameleon_world Mar 25 '20

Neat! thanks for sharing : )

1

u/hovissimo Mar 25 '20

My biggest complaint here is that I can't repeat the coercion with .

8

u/[deleted] Mar 24 '20

https://github.com/arthurxavierx/vim-caser

^ This one provides vim operators. Not sure the other one does.

2

u/chameleon_world Mar 24 '20

Oh neat. This one I think will operate automatically on the current word under the cursor, or with whatever is in the selection.

3

u/[deleted] Mar 24 '20

Oh yes, that's right. I created operators myself by adding a bit of code. I also added an option list, so I don't have to remember everything.

https://imgur.com/a/e0OLwrb

It works with custom text objects and the dot (repeat) operator.

Here is the relevant code from my vimconfig:

let g:caser_no_mappings = 1

nmap <expr>       <leader>c       vimrc#choose_case(0)
xmap <expr>       <leader>c       vimrc#choose_case(1)

function! vimrc#choose_case(visual)
  let l:options = [
        \ "&MixedCase",
        \ "&camelCase",
        \ "snake&_case",
        \ "&UPPER_CASE",
        \ "&Title Case",
        \ "&Sentence case",
        \ "space& case",
        \ "kebab&-case",
        \ "dot&.case"]
  let l:choice = confirm("Change case?", join(l:options, "\n"))
  let l:operation = [
        \ "MixedCase",
        \ "CamelCase",
        \ "SnakeCase",
        \ "UpperCase",
        \ "TitleCase",
        \ "SentenceCase",
        \ "SpaceCase",
        \ "KebabCase",
        \ "DotCase"][l:choice - 1]
  if a:visual
    return "\<Plug>CaserV".l:operation
  else
    return "\<Plug>Caser".l:operation
  end
endfunction

6

u/xigoi delete character and insert "goi" Mar 25 '20
" This setting also changes the name of a command:
" :Snek -> :Snake
let g:camelsnek_i_am_an_old_fart_with_no_sense_of_humour_or_internet_culture = 0

LOL

2

u/chameleon_world Mar 24 '20

I found this plugin recently and it has already been extremely helpful with my work. You can also convert to kebab-case

:EDIT I typo'd the title, snake_case but you already know that