【问题标题】:Is there any easy way to toggle "do/end" and "{}" in ruby in Vim?有什么简单的方法可以在 Vim 的 ruby​​ 中切换“do/end”和“{}”?
【发布时间】:2010-06-16 19:53:13
【问题描述】:

有没有什么简单的方法可以在 Vim 的 ruby​​ 中切换“do/end”和“{}”?

(TextMate 使用^{。)

【问题讨论】:

    标签: vim textmate ruby macvim


    【解决方案1】:

    你必须要么使用 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 脚本。

    【讨论】:

    • 是的,复杂的解决方案没有理由。只需使用 matchit.vim。 :)
    • Matchit 无法解决 OP 的请求。 (即使不涉及 vim,它仍然涉及开发工具的调优,用于开发目的)
    【解决方案2】:

    查看这个新插件:https://github.com/jgdavey/vim-blockle

    30 个字符填充

    【讨论】:

      【解决方案3】:

      有一个 splitjoin.vim 插件可以很好地做到这一点(用于拆分/连接的 gJ/gS 映射)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 2011-04-04
        相关资源
        最近更新 更多