有没有简单的方法可以切换“执行/结束”和“{}”在 Vim 中的 ruby​​ 中?

发布于 2024-09-05 22:41:18 字数 96 浏览 2 评论 0原文

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

(TextMate 使用 ^{ 执行此操作。)

Is there any easy way to toggle "do/end" and "{}" in ruby in Vim?

(TextMate does this with ^{.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-09-12 22:41:18

您必须使用 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() 定义为 这里

PS:这绝对是一个 SO 问题,因为它结合了 ruby​​ 上下文和高级 vim 脚本。

You'd have to either use searchpair(), or to play with % (as long as matchit is installed, and as you are on begin/end), then mark the two positions, test whether it's text or brackets, and finally update the two lines.

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

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

PS: this is definitively a SO question as it combines ruby context, and advanced vim scripting.

一紙繁鸢 2024-09-12 22:41:18

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

30 个字符垫

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

30 chars pad

烟雨凡馨 2024-09-12 22:41:18

有一个 splitjoin.vim 插件可以很好地完成此操作(用于拆分/连接的 gJ/gS 映射) 。

There is a splitjoin.vim plugin that does this nicely (gJ/gS mappings for splitting/joining).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文