ruby - 如何在 vim 中删除 ruby​​ 周围的 block (做/结束)

标签 ruby vim

如何用vim删除ruby中do/end分隔的环绕 block

例如

(10..20).map do |i| <CURSOR HERE>
  (1..10).map do |j|
    p j
  end
end

我想做一些类似dsb的事情(删除环绕 block )并得到

  (1..10).map do |j|
    p j
  end

最佳答案

也许你可以制作 nnormap。

每个 end/do 对都在同一个缩进上,所以首先你应该找到对缩进 - 在这种情况下,下一行相同的缩进(因为你的光标在 do 行。)

所以你可以让 vimscript 函数找到下一个缩进线并删除它。

这是函数的一个例子。您可以自定义您想要的 - 即)为休息行设置缩进。

function! DeleteWithSameIndent(inc)
    " Get the cursor current position
    let currentPos = getpos('.')
    let currentLine = currentPos[1]
    let firstLine = currentPos[1]
    let matchIndent = 0
    d

    " Look for a line with the same indent level whithout going out of the buffer
    while !matchIndent && currentLine != line('$') + 1 && currentLine != -1
        let currentLine += a:inc
        let matchIndent = indent(currentLine) == indent('.')
    endwhile

    " If a line is found go to this line
    if (matchIndent)
        let currentPos[1] = currentLine
        call setpos('.', currentPos)
        d
    endif
endfunction

nnoremap di :call DeleteWithSameIndent(1)<CR>

关于ruby - 如何在 vim 中删除 ruby​​ 周围的 block (做/结束),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52881710/

相关文章:

git - 快速切换版本

ruby-on-rails - Rails 控制台不会自动加载第二个数据库的模型

ruby - 为什么不迭代字符串的每个索引不能正常工作?

ruby - 如何找出 gem 安装选项?

vim - 如何在 Vim 中每三行追加一次?

带字符串连接的Vim设置选项

vim - 如何在 ViM 中创建自动完成列表?

Vim:软换行和带有 at 符号的长行

对 https 的 Ruby 请求 - "in ` read_nonblock':连接由对等方重置(Errno::ECONNRESET)”

ruby - 从对象中存储和检索变量