eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

Ruby block conversion macros for Vim

Yet another simple macro for Vim I've become quite fond of. Have you ever written a single-line block like

 myobj.each{|x| do whatever with x }

only to realize later that you actually wanted to do quite a bit more and you need to turn that into the following?

 myobj.each do |x|
   do stuff
   and some more
 end

Or maybe you're refactoring some code and see that a multi-line block can be collapsed into a single line using method chaining?

The following animation shows the behavior I wanted: block-conversion2.gif


This script requires matchit and vim-ruby.

After adding the snippet (ruby-block-conv.txt) to .vimrc, the :B command will turn

 foo{|x| f(x); g; bar(x)}

into

foo do |x|
  f(x)
  g
  bar(x)
end

and contrariwise.

You can also turn do/end into {} by selecting the block in visual mode and using the <Leader>B (in my case ,B) mapping.


Last modified:2005/11/14 08:53:53
Keyword(s):[blog] [ruby] [vim] [macro] [block]
References:[Ruby support for Vim]