Is there an easy way to switch "do / end" and "{}" in ruby โ€‹โ€‹in Vim?

Is there an easy way to switch "do / end" and "{}" in ruby โ€‹โ€‹in Vim?

(TextMate does this with ^{ .)

+7
ruby vim macvim textmate
source share
3 answers

You will need to either use searchpair () or play with% (until matchit is set, and when you start / end), then mark the two positions, check if they are text or brackets, and finally update the two strings.

 nnoremap <buffer> <cx>{ :call <sid>ToggleBeginOrBracket()<cr> let s:k_be = [ 'begin', 'end' ] function! s:ToggleBeginOrBracket() let c = lh#position#char_at_mark('.') if c =~ '[{}]' " don't use matchit for {,} exe 'normal! %s'.s:k_be[1-(c=='}')]."\<esc>``s".s:k_be[(c=='}')]."\<esc>" else let w = expand('<cword>') if w == 'begin' " use mathit normal % exe "normal! ciw}\<esc>``ciw{\<esc>" elseif w == 'end' " use mathit normal % exe "normal! ciw{\<esc>``ciw}\<esc>" else throw 'Cannot toggle block: cursor is not on {, }, begin, nor end' endif endif endfunction 

Where lh#position#char_at_mark() is defined here .

PS: This is finally a SO question, as it combines the ruby โ€‹โ€‹context and the vim extended scripts.

+6
source share

Check out this new plugin: https://github.com/jgdavey/vim-blockle .

30 barcodes

+3
source share

There is a plugin splitjoin.vim that does it nicely (gJ / gS mappings for split / merge).

0
source share

All Articles