Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete ruby surrounding block (do/end) in vim

Tags:

vim

ruby

How to delete the surround block delimited by do/end in ruby with vim

For example

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

I want to do something like dsb (delete surround block) and get

  (1..10).map do |j|
    p j
  end
like image 417
geckos Avatar asked Oct 18 '18 20:10

geckos


1 Answers

Maybe you can make nnormap.

Every end/do pair is on the same indent, so firstly you should find pair indent - in this case, next line for the same indent (Cause your cursor is in do line.)

So you can make vimscript function with finding next indent line and delete it.

This is an example of the function. You can customize you want - i.e.) set indent for resting lines.

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>
like image 123
seuling Avatar answered Sep 24 '22 02:09

seuling