# VIM/ruby

**URL:** <https://rubytalk.org/t/vim-ruby/533>\
**Category:** ruby-talk\
**Created:** [5 July 2002 22:11 UTC](https://rubytalk.org/t/vim-ruby/533 "2002-07-05T22:11:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Tuckner2](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Steve\_Tuckner2](https://rubytalk.org/u/Steve_Tuckner2)\
**Post date:** [5 July 2002 22:11 UTC](https://rubytalk.org/t/vim-ruby/533/1 "2002-07-05T22:11:43Z")

</div>

I am want to build a class or set of classes to use in VIM so that I can  
more easily script vim/ruby. The problem with the current set of classes  
is that I want VIM::Buffer, VIM::Window, and the buffer lists to act  
just like arrays so that I could do the following:

: ruby $curbuf.sort {|a,b| a.to\_i \<=\> b.to\_i}

My solution is to build a proxy class for the lines in a buffer that is  
a sub-class of an array so that I would be able to do the following:

: ruby VIMbuf($curbuf).sort {|a,b| a.to\_i \<=\> b.to\_i}

Is there a better way to do this or should I proceed as above?

Steve Tuckner

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [5 July 2002 22:58 UTC](https://rubytalk.org/t/vim-ruby/533/2 "2002-07-05T22:58:00Z")

</div>

You don’t need to subclass Array at all to do this. Just mix in  
Enumerable and define each().

For instance, source this in Vim:

ruby \<\<EOF  
module VIM  
class B  
include Enumerable  
def initialize(b = VIM::Buffer.current)  
@b = b  
end  
def each(&block)  
(1..@b.count).each { |n| block.call(@b[n]) }  
end  
end  
end

def p(s)  
b = VIM::Buffer.current  
b.append(b.count, s.inspect)  
end

# print out all the lines of this file

def beach(&block)  
b = VIM::B.new  
b.each(&block)  
end  
beach { |s| puts s }

# append all the lines of this file to the end.

b = VIM::B.new  
b.sort.each { |s| p “s: #{s}” }

EOF

Have you seen Benoit Cerrina’s VimRubyX?

There may be some good ideas in that..

[http://vim.sourceforge.net/scripts/script.php?script\_id=248](http://vim.sourceforge.net/scripts/script.php?script_id=248)

> **···**
>
> On Friday 05 July 2002 03:11 pm, Steve Tuckner wrote:
> 
> > I am want to build a class or set of classes to use in VIM so that  
> > I can more easily script vim/ruby. The problem with the current set  
> > of classes is that I want VIM::Buffer, VIM::Window, and the buffer  
> > lists to act
> > 
> > just like arrays so that I could do the following:  
> > : ruby $curbuf.sort {|a,b| a.to\_i \<=\> b.to\_i}
> > 
> > My solution is to build a proxy class for the lines in a buffer  
> > that is
> > 
> > a sub-class of an array so that I would be able to do the following:  
> > : ruby VIMbuf($curbuf).sort {|a,b| a.to\_i \<=\> b.to\_i}
> > 
> > Is there a better way to do this or should I proceed as above?
> 
> –  
> Ned Konz  
> [http://bike-nomad.com](http://bike-nomad.com)  
> GPG key ID: BEEA7EFE

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [5 July 2002 23:15 UTC](https://rubytalk.org/t/vim-ruby/533/3 "2002-07-05T23:15:02Z")

</div>

Or you could just mix in each() to VIM::Buffer and VIM::Window…  
now you can sort them, etc.

ruby \<\<EOF  
module VIM  
class Buffer  
include Enumerable

# each line in buffer…

def each(&block)  
(1..count).each { |n| block.call(self[n]) }  
end

# each buffer…

def self.each(&block)  
(0..count-1).each { |n| block.call(self[n]) }  
end  
end  
class Window  
include Enumerable

# each window…

def self.each(&block)  
(0..count-1).each { |n| block.call(self[n]) }  
end  
end  
end

def p(s)  
b = VIM::Buffer.current  
b.append(b.count, s.inspect)  
end

# print out all the lines of this file

def beach(&block)  
b = VIM::Buffer.current  
b.each(&block)  
end  
beach { |s| puts s }

# append all the lines of this file to the end.

b = VIM::Buffer.current  
b.sort.each { |s| p “s: #{s}” }

EOF

> **···**
>
> On Friday 05 July 2002 03:11 pm, Steve Tuckner wrote:
> 
> > I am want to build a class or set of classes to use in VIM so that  
> > I can more easily script vim/ruby. The problem with the current set  
> > of classes is that I want VIM::Buffer, VIM::Window, and the buffer  
> > lists to act
> > 
> > just like arrays so that I could do the following:  
> > : ruby $curbuf.sort {|a,b| a.to\_i \<=\> b.to\_i}
> 
> –  
> Ned Konz  
> [http://bike-nomad.com](http://bike-nomad.com)  
> GPG key ID: BEEA7EFE

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [5 July 2002 23:48 UTC](https://rubytalk.org/t/vim-ruby/533/4 "2002-07-05T23:48:49Z")

</div>

OK, here’s a file that you can put in your ~/.vim/plugin directory  
that will extend Vim’s Ruby model to allow sort, each, etc.  
as well as whole buffer replacements like:

ruby $curbuf.set\_to( $curbuf.sort )  
ruby $curbuf.set\_to( $curbuf.grep(/abc/))

" Extension to built-in Ruby support to add enumerable (each, sort,  
etc.) to  
" both VIM::Buffer (class & instance) and VIM::Window (class)  
if has(“ruby”)  
ruby \<\<EOF  
module VIM  
class Buffer  
include Enumerable

# each line in buffer…

def each(&block)  
(1…count).each { |n| block.call(self[n]) }  
end  
def each\_with\_index(&block)  
(1…count).each { |n| block.call(self[n],n) }  
end

# set me to the given array of strings

# so you can do (for instance):

# ruby $curbuf.set\_to( $curbuf.sort )

# ruby $curbuf.set\_to( $curbuf.grep(/abc/))

def set\_to(arr)  
if arr.size \<= count  
arr.each\_with\_index { |s,i| self[i+1] = s }  
while count \> arr.size  
self.delete(count)  
end  
else  
arr.each\_with\_index do |s,i|  
if i+1 \<= count  
self[i+1] = s  
else  
append(i, s)  
end  
end  
end  
end

# each buffer…

def self.each(&block)  
(0…count-1).each { |n| block.call(self[n]) }  
end  
end  
class Window  
include Enumerable

# each window…

def self.each(&block)  
(0…count-1).each { |n| block.call(self[n]) }  
end  
end  
end  
EOF  
endif

---

<div class="post-metadata">

**Author:** ![Steve\_Tuckner2](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Steve\_Tuckner2](https://rubytalk.org/u/Steve_Tuckner2)\
**Post date:** [8 July 2002 13:49 UTC](https://rubytalk.org/t/vim-ruby/533/5 "2002-07-08T13:49:58Z")

</div>

Thanks so…much for your work! The solution (of course) was much more  
elegant than I thought it could be. I am glad I posted it for consideration  
first…

Steve Tuckner

> **···**
>
> -----Original Message-----  
> From: Ned Konz [[mailto:ned@bike-nomad.com](mailto:ned@bike-nomad.com)]  
> Sent: Friday, July 05, 2002 6:49 PM  
> To: [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Subject: Ruby extensions for VIM
> 
> OK, here’s a file that you can put in your ~/.vim/plugin directory  
> that will extend Vim’s Ruby model to allow sort, each, etc.  
> as well as whole buffer replacements like:
> 
> ruby $curbuf.set\_to( $curbuf.sort )  
> ruby $curbuf.set\_to( $curbuf.grep(/abc/))
> 
> " Extension to built-in Ruby support to add enumerable (each, sort,  
> etc.) to  
> " both VIM::Buffer (class & instance) and VIM::Window (class)  
> if has(“ruby”)  
> ruby \<\<EOF  
> module VIM  
> class Buffer  
> include Enumerable
> 
> # each line in buffer…
> 
> def each(&block)  
> (1…count).each { |n| block.call(self[n]) }  
> end  
> def each\_with\_index(&block)  
> (1…count).each { |n| block.call(self[n],n) }  
> end
> 
> # set me to the given array of strings
> 
> # so you can do (for instance):
> 
> # ruby $curbuf.set\_to( $curbuf.sort )
> 
> # ruby $curbuf.set\_to( $curbuf.grep(/abc/))
> 
> def set\_to(arr)  
> if arr.size \<= count  
> arr.each\_with\_index { |s,i| self[i+1] = s }  
> while count \> arr.size  
> self.delete(count)  
> end  
> else  
> arr.each\_with\_index do |s,i|  
> if i+1 \<= count  
> self[i+1] = s  
> else  
> append(i, s)  
> end  
> end  
> end  
> end
> 
> # each buffer…
> 
> def self.each(&block)  
> (0…count-1).each { |n| block.call(self[n]) }  
> end  
> end  
> class Window  
> include Enumerable
> 
> # each window…
> 
> def self.each(&block)  
> (0…count-1).each { |n| block.call(self[n]) }  
> end  
> end  
> end  
> EOF  
> endif
