Scripting Vim With Ruby

Page 14. Calling Ruby From Vim. 108221123, Ryan Lane/Vetta. Page 15 ... Three Way To Call Ruby. 108281011, Jun Takahashi/Riser. Page 17. Inline Ruby.
4MB Sizes 12 Downloads 253 Views
Scripting Vim With Ruby

Matt Margolis Getty Images Madison, WI @mrmargolis

sb10068645j-001, Tara Moore/Riser

Scripting Vim With Ruby

73029870, Sean Justice/Stockbyte

VimScript

The scripting language that comes with Vim You have already seen it if you have ever edited your .vimrc file or configured a plugin

VimScript

VimScript

To Learn More :help vim-script

VimScript + Ruby

BA61201, Hulton Archive

VimScript + Ruby Adds a dependency on Ruby, but you get a lot in return.

• Ruby’s Standard Library • RubyGems • You are probably more comfortable in Ruby than VimScript

How?

81899092, Daisy Gilardini/The Image Bank

:help ruby

• image of :help ruby

Ruby Support?

No rubies? Oh gnoes!!

97602532, Miss Maggie/Flickr

Which Ruby?

• MRI 1.8.7 is the safe choice • The internets indicate that 1.9.2 is possible but buggy

Vim With Ruby Support

http://stackoverflow.com/questions/3794895/ installing-vim-with-ruby-support-ruby https://github.com/b4winckler/macvim

Calling Ruby From Vim

108221123, Ryan Lane/Vetta

Calling Ruby From Vim

To load these examples either • Put the code in your .vimrc • Put the code in a .vim file Then :source /path/to/file/you/used

Three Way To Call Ruby

108281011, Jun Takahashi/Riser

Inline Ruby

:ruby puts “awesome”.upcase Vim will echo AWESOME at the bottom of your screen

Inline Ruby Example: How I figured out which ruby is being used function! RubyInfo() ruby rubyrubyruby

Calling Vim From Ruby

108268278, Mlenny Photography/Vetta

VIM Module When Ruby executes inside of Vim you are provided with the VIM module which gives you access to interrogate and manipulate Vim’s Settings

• •Buffers •Windows •Cursor location •Content

VIM Module From Vim’s ruby documentation print "Hello" VIM.command(cmd) num = VIM::Window.count w = VIM::Window[n] cw = VIM::Window.current num = VIM::Buffer.count b = VIM::Buffer[n] cb = VIM::Buffer.current w.height = lines w.cursor = [row, col] pos = w.cursor name = b.name line = b[n] num = b.count b[n] = str b.delete(n) b.append(n, str) line = VIM::Buffer.current.line

# # # # # # # # # # # # # # # # # #

displays a message execute an Ex command gets the number of windows gets window "n" gets the current window gets the number of buffers gets buffer "n" gets the current buffer sets the window height sets the window cursor position gets an array [row, col] gets the buffer file name gets a line from the buffer gets the number of lines sets a line in the buffer deletes a line appends a line after n gets the current line

Vim + RubyGems

200377335-001, Thomas Northcut/Photodisc

Vim + RubyGems

:call TwitterSearch()

Vim + RubyGems

Vim + RubyGems + RVM RVM environment changes can cause Vim to get confused and look in the wrong gem directory. When Vim gets confused you will get angry. You might do bad things.

Vim + RubyGems + RVM AA010437, Adam Crowleyt/Stockbyte

Vim + RubyGems + RVM

But it’s really not that hard to fix.

Vim + RubyGems + RVM https://github.com/robgleeson/hammer.vim/issues/8

In your .zshrc or .bashrc vim() { (unset GEM_PATH GEM_HOME; command vim "[email protected]") }

Real World Examples

103165513, Paper Boat Creative/Digital Vision

Vim github dashboard https://github.com/lukaszkorecki/vim-GitHubDashBoard

Vim github dashboard

The Lusty Plugins

Over 3,000 lines of VimScript + Ruby

Questions? http://spkr8.com/t/8156

Matt Margolis Getty Images Madison, WI @mr