Lars Kotthoff
Lars Kotthoff

Reputation: 109292

How do I save a variable between executions of ruby from vim?

With pre-1.9 versions of Ruby, I was able to do something like the following in Vim.

:ruby foo = "bar"
:ruby print foo

This now gives me a NameError because foo isn't defined.

How do I "save" a variable between executions of :ruby? In particular I want to be able to use this functionality from a Vim plugin and store an object (as opposed to a primitive value like a string or number, which could be achieved by storing it in a Vim variable).

Upvotes: 1

Views: 139

Answers (2)

Lars Kotthoff
Lars Kotthoff

Reputation: 109292

The quick and dirty way to do this is to prefix your variable name with a $, which will make it act like a global variable.

:ruby $foo = "bar"
:ruby print $foo

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172768

You could always store the value in a Vim variable:

:ruby VIM::command('let foo = "bar"')
:ruby print VIM::evaluate('foo')

Upvotes: 2

Related Questions