Skip to content

Instantly share code, notes, and snippets.

@ernix
Created April 26, 2017 04:19
Show Gist options
  • Save ernix/34d0712b5612da46bf7454ff9bbb32e1 to your computer and use it in GitHub Desktop.
Save ernix/34d0712b5612da46bf7454ff9bbb32e1 to your computer and use it in GitHub Desktop.
vim-toggle-case.vim
function! s:ToggleCase()
let cur_word = expand("<cword>")
if cur_word == ''
return
endif
let new_word = ''
if toupper(cur_word)==#cur_word " CONST_ISH
let new_word = tolower(cur_word)
elseif cur_word =~ '_' " snake_case
let chunks = split(cur_word, '_')
let new_word = tolower(chunks[0])
for chunk in chunks[1:]
let chunk = tolower(chunk)
let chunk = substitute(chunk, '^.', '\U\0', '')
let new_word = new_word . chunk
endfor
else " camelCase...maybe
let chunks = split(cur_word, '[^A-Z]\zs\ze[A-Z]', 1)
let new_word = toupper(chunks[0])
for chunk in chunks[1:]
let chunk = toupper(chunk)
let new_word = new_word . '_' . chunk
endfor
endif
exec 'norm viwc' . new_word . ''
exec 'norm b'
endfunction
command! ToggleCase call <SID>ToggleCase()
nnoremap <C-n> :ToggleCase<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment