Skip to content

Instantly share code, notes, and snippets.

@lackac
Created November 29, 2010 12:41
Show Gist options
  • Save lackac/719909 to your computer and use it in GitHub Desktop.
Save lackac/719909 to your computer and use it in GitHub Desktop.
" Toggle ruby blocks
" requires the matchit plugin
function! s:ToggleRubyBlocks()
let c = getline(".")[col(".")-1]
if c =~ '[{}]'
" don't use matchit for {,}
exe 'normal! %s'.(c=='}' ? 'do' : 'end')."\<esc>``s".(c=='}' ? 'end' : 'do')."\<esc>"
else
let w = expand('<cword>')
if w == 'do'
" use matchit
normal %
exe "normal! ciw}\<esc>``ciw{\<esc>"
elseif w == 'end'
" use matchit
normal %
exe "normal! ciw{\<esc>``ciw}\<esc>"
else
throw 'Cannot toggle block: cursor is not on {, }, do, nor end'
endif
endif
endfunction
autocmd! BufReadPost *.rb,*.erb :nnoremap <buffer> <leader>b :call <sid>ToggleRubyBlocks()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment