2010-06-16 55 views

回答

6

你必须要么使用和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:由于它结合了ruby上下文和先进的vim脚本,因此这绝对是一个SO问题。

+0

是的,没有理由为复杂的解决方案。只需使用matchit.vim。 :) – graywh 2010-07-14 21:32:26

+0

Matchit没有解决OP的请求。 (即使没有vim参与,它仍然涉及开发工具的调整,用于开发目的) – 2010-07-15 18:12:24

0

有一个splitjoin.vim插件,这是否很好(GJ/GS映射分割/结合)。

相关问题