Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing matching { braces } with do/end in Vim (Ruby)

Tags:

vim

ruby

Does anyone have a plugin or macro to replace matching { braces } with do and end in Vim? Preferably turning a single-line statement like this:

foo.each { |f| f.whatever }

into:

foo.each do |f|
  f.whatever
end

I could make a macro myself for that one case, but I'd like something that could also handle converting existing multi-line, potentially complicated blocks, like:

foo.each { |f|
  f.bars.each { |b| b.whatever }
  hash = { a: 123, b: 456 }
}

into:

foo.each do |f|
  f.bars.each { |b| b.whatever }
  hash = { a: 123, b: 456 }
end

I've looked at vim-surround and rails.vim, and haven't found a way with either.

like image 279
Jim Stewart Avatar asked Mar 16 '13 02:03

Jim Stewart


1 Answers

There is a Vim plugin called Vim Blockle that performs this function.

Once you install the plugin you put the cursor on the { } do or end and press <Leader>b to swap the block styles.

like image 108
Dan Reedy Avatar answered Oct 12 '22 23:10

Dan Reedy