Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created August 21, 2010 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/541723 to your computer and use it in GitHub Desktop.
Save bootleq/541723 to your computer and use it in GitHub Desktop.
[.vimrc] toggle between CamelCased/under_scored word
" TODO when no keyword is under/after the cursor, try looking backward to find a word.
fun! WordTransform()
let w = expand("<cword>")
let x = ''
let c = strpart(getline('.'), col('.') - 1, 1)
if match(w, '_') > -1
let x = substitute(w, '_\([a-z]\)', '\u\1', 'g')
else
let x = substitute(w, '\C[A-Z]', '_\L\0', 'g')
endif
if x == w
echohl WarningMsg | echomsg "Not a camelCased/under_scored word." | echohl None
return
endif
if match(w, c) < 0 " cursor might sit before keyword
exec "normal f" . strpart(w, 0, 1)
exec "normal \"_ciw\<C-R>=x\<CR>"
else
exec "normal \"_ciw\<C-R>=x\<CR>"
endif
endf
nnoremap <LocalLeader>x :call WordTransform()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment