Skip to content

Instantly share code, notes, and snippets.

@Haegin
Created July 31, 2014 10:17
Show Gist options
  • Save Haegin/ddd8870e3142766572a1 to your computer and use it in GitHub Desktop.
Save Haegin/ddd8870e3142766572a1 to your computer and use it in GitHub Desktop.
function! ArrayToHash()
"mark the start and end of the array
normal f[ms%me
" one item per line
's,'es/, /, /g
" re-indent
normal vi[=
" update the ending mark as it might be wrong now
normal! `s%me
" change ':foo,' to 'foo: "foo",'
's,'es/\v:([a-z0-9_]+)/\1: "\1"/
" select the contents of the array/hash
normal! vi[
" use tpope's abolish.vim coerce command to convert the contents of the
" quotes to MixedCase
'<,'>normal f"lcrm
" change the array square brackets to a hashes braces
normal cs[{
endfunction
" This is a utility function to switch between the old style hash rocket syntax
" and the newer json style syntax for Ruby hashes.
function! ChangeHashStyle()
" Set start and end marks
normal vi{
let line = getline(".")
echom line
if line =~ '=>'
echom "Converting from hash rockets to json style"
" From hash rockets to JSON style
'<,'>s/\v:([a-z0-9_]+) *\=\> */\1: /g
else
echom "Converting from json style to hash rockets"
" From JSON style to hash rockets
'<,'>s/\v([a-z0-9_]+):/:\1 =>/g
endif
endfunction
function! FreezeStrings()
" Set start and end marks
normal vi{
" within the marks, replace (".*") with \1.freeze.
'<,'>s/\v(".*")/\1.freeze/g
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment