有什么简单的方法可以在 Vim 的 ruby 中切换“do/end”和“{}”吗?
(TextMate 使用 ^{
完成此操作。)
最佳答案
你必须要么使用 searchpair(),要么使用 %(只要安装了 matchit,并且当你在开始/结束时),然后标记两个位置,测试它是文本还是括号,最后更新这两行。
nnoremap <buffer> <c-x>{ :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
lh#position#char_at_mark()
的定义 here .
PS:这绝对是一个 SO 问题,因为它结合了 ruby 上下文和高级 vim 脚本。
关于vim - 有什么简单的方法可以在 Vim 的 ruby 中切换 "do/end"和 "{}"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090701/