xref: /vim-8.2.3635/runtime/doc/if_ruby.txt (revision 6c2b7b80)
198056533SBram Moolenaar*if_ruby.txt*   For Vim version 8.2.  Last change: 2019 Jul 21
2071d4279SBram Moolenaar
3071d4279SBram Moolenaar
4071d4279SBram Moolenaar		  VIM REFERENCE MANUAL    by Shugo Maeda
5071d4279SBram Moolenaar
6071d4279SBram MoolenaarThe Ruby Interface to Vim				*ruby* *Ruby*
7071d4279SBram Moolenaar
8071d4279SBram Moolenaar
9071d4279SBram Moolenaar1. Commands			|ruby-commands|
102c5e8e80SBram Moolenaar2. The Vim module		|ruby-vim|
112c5e8e80SBram Moolenaar3. Vim::Buffer objects		|ruby-buffer|
122c5e8e80SBram Moolenaar4. Vim::Window objects		|ruby-window|
13071d4279SBram Moolenaar5. Global variables		|ruby-globals|
14e99be0e6SBram Moolenaar6. rubyeval() Vim function	|ruby-rubyeval|
15e99be0e6SBram Moolenaar7. Dynamic loading		|ruby-dynamic|
16071d4279SBram Moolenaar
17071d4279SBram Moolenaar			*E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273*
18071d4279SBram Moolenaar
1925c9c680SBram Moolenaar{only available when Vim was compiled with the |+ruby| feature}
20071d4279SBram Moolenaar
21071d4279SBram MoolenaarThe home page for ruby is http://www.ruby-lang.org/.  You can find links for
22071d4279SBram Moolenaardownloading Ruby there.
23071d4279SBram Moolenaar
24071d4279SBram Moolenaar==============================================================================
25071d4279SBram Moolenaar1. Commands						*ruby-commands*
26071d4279SBram Moolenaar
27071d4279SBram Moolenaar							*:ruby* *:rub*
289b451250SBram Moolenaar:rub[y] {cmd}		Execute Ruby command {cmd}.  A command to try it out: >
299b451250SBram Moolenaar				:ruby print "Hello"
30071d4279SBram Moolenaar
31*6c2b7b80SBram Moolenaar:rub[y] << [trim] [{endmarker}]
32071d4279SBram Moolenaar{script}
332e693a88SBram Moolenaar{endmarker}
34071d4279SBram Moolenaar			Execute Ruby script {script}.
355477506aSBram Moolenaar
362e693a88SBram Moolenaar			If [endmarker] is omitted, it defaults to a dot '.'
37*6c2b7b80SBram Moolenaar			like for the |:append| and |:insert| commands.  Refer
38*6c2b7b80SBram Moolenaar			to |:let-heredoc| for more information.
39*6c2b7b80SBram Moolenaar
405477506aSBram Moolenaar
415477506aSBram Moolenaar			This form of the |:ruby| command is mainly useful for
42071d4279SBram Moolenaar			including ruby code in vim scripts.
435477506aSBram Moolenaar
44071d4279SBram Moolenaar			Note: This command doesn't work when the Ruby feature
45071d4279SBram Moolenaar			wasn't compiled in.  To avoid errors, see
46071d4279SBram Moolenaar			|script-here|.
47071d4279SBram Moolenaar
48071d4279SBram MoolenaarExample Vim script: >
49071d4279SBram Moolenaar
50071d4279SBram Moolenaar	function! RedGem()
51071d4279SBram Moolenaar	ruby << EOF
52071d4279SBram Moolenaar	class Garnet
53071d4279SBram Moolenaar		def initialize(s)
542c5e8e80SBram Moolenaar			@buffer = Vim::Buffer.current
55071d4279SBram Moolenaar			vimputs(s)
56071d4279SBram Moolenaar		end
57071d4279SBram Moolenaar		def vimputs(s)
58071d4279SBram Moolenaar			@buffer.append(@buffer.count,s)
59071d4279SBram Moolenaar		end
60071d4279SBram Moolenaar	end
61071d4279SBram Moolenaar	gem = Garnet.new("pretty")
62071d4279SBram Moolenaar	EOF
63071d4279SBram Moolenaar	endfunction
64071d4279SBram Moolenaar<
65abd468edSBram MoolenaarTo see what version of Ruby you have: >
66abd468edSBram Moolenaar	:ruby print RUBY_VERSION
67abd468edSBram Moolenaar<
68071d4279SBram Moolenaar
69071d4279SBram Moolenaar						*:rubydo* *:rubyd* *E265*
70071d4279SBram Moolenaar:[range]rubyd[o] {cmd}	Evaluate Ruby command {cmd} for each line in the
71071d4279SBram Moolenaar			[range], with $_ being set to the text of each line in
72071d4279SBram Moolenaar			turn, without a trailing <EOL>.  Setting $_ will change
73071d4279SBram Moolenaar			the text, but note that it is not possible to add or
74071d4279SBram Moolenaar			delete lines using this command.
75071d4279SBram Moolenaar			The default for [range] is the whole file: "1,$".
76071d4279SBram Moolenaar
77071d4279SBram Moolenaar							*:rubyfile* *:rubyf*
78071d4279SBram Moolenaar:rubyf[ile] {file}	Execute the Ruby script in {file}.  This is the same as
79214641f7SBram Moolenaar			`:ruby load 'file'`, but allows file name completion.
80071d4279SBram Moolenaar
81071d4279SBram MoolenaarExecuting Ruby commands is not possible in the |sandbox|.
82071d4279SBram Moolenaar
83071d4279SBram Moolenaar==============================================================================
842c5e8e80SBram Moolenaar2. The Vim module					*ruby-vim*
85071d4279SBram Moolenaar
862c5e8e80SBram MoolenaarRuby code gets all of its access to vim via the "Vim" module.
87071d4279SBram Moolenaar
882c5e8e80SBram MoolenaarOverview: >
89071d4279SBram Moolenaar	print "Hello"			      # displays a message
902c5e8e80SBram Moolenaar	Vim.command(cmd)		      # execute an Ex command
912c5e8e80SBram Moolenaar	num = Vim::Window.count		      # gets the number of windows
922c5e8e80SBram Moolenaar	w = Vim::Window[n]		      # gets window "n"
932c5e8e80SBram Moolenaar	cw = Vim::Window.current	      # gets the current window
942c5e8e80SBram Moolenaar	num = Vim::Buffer.count		      # gets the number of buffers
952c5e8e80SBram Moolenaar	b = Vim::Buffer[n]		      # gets buffer "n"
962c5e8e80SBram Moolenaar	cb = Vim::Buffer.current	      # gets the current buffer
97071d4279SBram Moolenaar	w.height = lines		      # sets the window height
98071d4279SBram Moolenaar	w.cursor = [row, col]		      # sets the window cursor position
99071d4279SBram Moolenaar	pos = w.cursor			      # gets an array [row, col]
100071d4279SBram Moolenaar	name = b.name			      # gets the buffer file name
101071d4279SBram Moolenaar	line = b[n]			      # gets a line from the buffer
102071d4279SBram Moolenaar	num = b.count			      # gets the number of lines
103071d4279SBram Moolenaar	b[n] = str			      # sets a line in the buffer
104071d4279SBram Moolenaar	b.delete(n)			      # deletes a line
105071d4279SBram Moolenaar	b.append(n, str)		      # appends a line after n
1062c5e8e80SBram Moolenaar	line = Vim::Buffer.current.line       # gets the current line
1072c5e8e80SBram Moolenaar	num = Vim::Buffer.current.line_number # gets the current line number
1082c5e8e80SBram Moolenaar	Vim::Buffer.current.line = "test"     # sets the current line number
109071d4279SBram Moolenaar<
110071d4279SBram Moolenaar
111071d4279SBram MoolenaarModule Functions:
112071d4279SBram Moolenaar
113071d4279SBram Moolenaar							*ruby-message*
1142c5e8e80SBram MoolenaarVim::message({msg})
115071d4279SBram Moolenaar	Displays the message {msg}.
116071d4279SBram Moolenaar
1176e5ea8d2SBram Moolenaar							*ruby-blob*
1186e5ea8d2SBram MoolenaarVim::blob({arg})
119314dd79cSBram Moolenaar	Return |Blob| literal string from {arg}.
1206e5ea8d2SBram Moolenaar
121071d4279SBram Moolenaar							*ruby-set_option*
1222c5e8e80SBram MoolenaarVim::set_option({arg})
123071d4279SBram Moolenaar	Sets a vim option.  {arg} can be any argument that the ":set" command
124071d4279SBram Moolenaar	accepts.  Note that this means that no spaces are allowed in the
125071d4279SBram Moolenaar	argument!  See |:set|.
126071d4279SBram Moolenaar
127071d4279SBram Moolenaar							*ruby-command*
1282c5e8e80SBram MoolenaarVim::command({cmd})
129071d4279SBram Moolenaar	Executes Ex command {cmd}.
130071d4279SBram Moolenaar
131071d4279SBram Moolenaar							*ruby-evaluate*
1322c5e8e80SBram MoolenaarVim::evaluate({expr})
133071d4279SBram Moolenaar	Evaluates {expr} using the vim internal expression evaluator (see
1342b8388bdSBram Moolenaar	|expression|).  Returns the expression result as:
1352b8388bdSBram Moolenaar	- a Integer if the Vim expression evaluates to a number
1362b8388bdSBram Moolenaar	- a Float if the Vim expression evaluates to a float
1372b8388bdSBram Moolenaar	- a String if the Vim expression evaluates to a string
1382b8388bdSBram Moolenaar	- a Array if the Vim expression evaluates to a Vim list
1392b8388bdSBram Moolenaar	- a Hash if the Vim expression evaluates to a Vim dictionary
1402b8388bdSBram Moolenaar	Dictionaries and lists are recursively expanded.
141071d4279SBram Moolenaar
142071d4279SBram Moolenaar==============================================================================
1432c5e8e80SBram Moolenaar3. Vim::Buffer objects					*ruby-buffer*
144071d4279SBram Moolenaar
1452c5e8e80SBram MoolenaarVim::Buffer objects represent vim buffers.
146071d4279SBram Moolenaar
147071d4279SBram MoolenaarClass Methods:
148071d4279SBram Moolenaar
149071d4279SBram Moolenaarcurrent		Returns the current buffer object.
150071d4279SBram Moolenaarcount		Returns the number of buffers.
151071d4279SBram Moolenaarself[{n}]	Returns the buffer object for the number {n}.  The first number
152071d4279SBram Moolenaar		is 0.
153071d4279SBram Moolenaar
154071d4279SBram MoolenaarMethods:
155071d4279SBram Moolenaar
156edd6aacbSBram Moolenaarname		Returns the full name of the buffer.
157071d4279SBram Moolenaarnumber		Returns the number of the buffer.
158071d4279SBram Moolenaarcount		Returns the number of lines.
159071d4279SBram Moolenaarlength		Returns the number of lines.
160071d4279SBram Moolenaarself[{n}]	Returns a line from the buffer. {n} is the line number.
161071d4279SBram Moolenaarself[{n}] = {str}
162071d4279SBram Moolenaar		Sets a line in the buffer. {n} is the line number.
163071d4279SBram Moolenaardelete({n})	Deletes a line from the buffer. {n} is the line number.
164071d4279SBram Moolenaarappend({n}, {str})
165071d4279SBram Moolenaar		Appends a line after the line {n}.
166899dddf8SBram Moolenaarline		Returns the current line of the buffer if the buffer is
167899dddf8SBram Moolenaar		active.
168899dddf8SBram Moolenaarline = {str}    Sets the current line of the buffer if the buffer is active.
169899dddf8SBram Moolenaarline_number     Returns the number of the current line if the buffer is
170899dddf8SBram Moolenaar		active.
171071d4279SBram Moolenaar
172071d4279SBram Moolenaar==============================================================================
1732c5e8e80SBram Moolenaar4. Vim::Window objects					*ruby-window*
174071d4279SBram Moolenaar
1752c5e8e80SBram MoolenaarVim::Window objects represent vim windows.
176071d4279SBram Moolenaar
177071d4279SBram MoolenaarClass Methods:
178071d4279SBram Moolenaar
179071d4279SBram Moolenaarcurrent		Returns the current window object.
180071d4279SBram Moolenaarcount		Returns the number of windows.
181071d4279SBram Moolenaarself[{n}]	Returns the window object for the number {n}.  The first number
182071d4279SBram Moolenaar		is 0.
183071d4279SBram Moolenaar
184071d4279SBram MoolenaarMethods:
185071d4279SBram Moolenaar
186071d4279SBram Moolenaarbuffer		Returns the buffer displayed in the window.
187071d4279SBram Moolenaarheight		Returns the height of the window.
188071d4279SBram Moolenaarheight = {n}	Sets the window height to {n}.
189e344beadSBram Moolenaarwidth		Returns the width of the window.
190e344beadSBram Moolenaarwidth = {n}	Sets the window width to {n}.
191071d4279SBram Moolenaarcursor		Returns a [row, col] array for the cursor position.
192edd6aacbSBram Moolenaar		First line number is 1 and first column number is 0.
193071d4279SBram Moolenaarcursor = [{row}, {col}]
194071d4279SBram Moolenaar		Sets the cursor position to {row} and {col}.
195071d4279SBram Moolenaar
196071d4279SBram Moolenaar==============================================================================
197a5792f58SBram Moolenaar5. Global variables					*ruby-globals*
198071d4279SBram Moolenaar
199071d4279SBram MoolenaarThere are two global variables.
200071d4279SBram Moolenaar
201071d4279SBram Moolenaar$curwin		The current window object.
202071d4279SBram Moolenaar$curbuf		The current buffer object.
203071d4279SBram Moolenaar
204071d4279SBram Moolenaar==============================================================================
205e99be0e6SBram Moolenaar6. rubyeval() Vim function				*ruby-rubyeval*
206e99be0e6SBram Moolenaar
207e99be0e6SBram MoolenaarTo facilitate bi-directional interface, you can use |rubyeval()| function to
208e99be0e6SBram Moolenaarevaluate Ruby expressions and pass their values to Vim script.
209e99be0e6SBram Moolenaar
210e99be0e6SBram MoolenaarThe Ruby value "true", "false" and "nil" are converted to v:true, v:false and
211e99be0e6SBram Moolenaarv:null, respectively.
212e99be0e6SBram Moolenaar
213e99be0e6SBram Moolenaar==============================================================================
214e99be0e6SBram Moolenaar7. Dynamic loading					*ruby-dynamic*
215a5792f58SBram Moolenaar
2160536570fSBram MoolenaarOn MS-Windows and Unix the Ruby library can be loaded dynamically.  The
2170536570fSBram Moolenaar|:version| output then includes |+ruby/dyn|.
218a5792f58SBram Moolenaar
2190536570fSBram MoolenaarThis means that Vim will search for the Ruby DLL file or shared library only
2200536570fSBram Moolenaarwhen needed.  When you don't use the Ruby interface you don't need it, thus
2210536570fSBram Moolenaaryou can use Vim even though this library file is not on your system.
222a5792f58SBram Moolenaar
223e18c0b39SBram Moolenaar
224d94464eeSBram MoolenaarMS-Windows ~
225d94464eeSBram Moolenaar
22669154f22SBram MoolenaarYou need to install the right version of Ruby for this to work.  You can find
22769154f22SBram Moolenaarthe package to download from:
2282ec618c9SBram Moolenaarhttp://rubyinstaller.org/downloads/
2292ec618c9SBram MoolenaarCurrently that is rubyinstaller-2.2.5.exe
23069154f22SBram Moolenaar
231a5792f58SBram MoolenaarTo use the Ruby interface the Ruby DLL must be in your search path.  In a
232e18c0b39SBram Moolenaarconsole window type "path" to see what directories are used.  The 'rubydll'
233e18c0b39SBram Moolenaaroption can be also used to specify the Ruby DLL.
234a5792f58SBram Moolenaar
235a5792f58SBram MoolenaarThe name of the DLL must match the Ruby version Vim was compiled with.
2362ec618c9SBram MoolenaarCurrently the name is "msvcrt-ruby220.dll".  That is for Ruby 2.2.X.  To know
23769154f22SBram Moolenaarfor sure edit "gvim.exe" and search for "ruby\d*.dll\c".
23869154f22SBram Moolenaar
2392ec618c9SBram MoolenaarIf you want to build Vim with RubyInstaller 1.9 or 2.X using MSVC, you need
2402ec618c9SBram Moolenaarsome tricks.  See the src/INSTALLpc.txt for detail.
241a5792f58SBram Moolenaar
24298ef233eSBram MoolenaarIf Vim is built with RubyInstaller 2.4 or later, you may also need to add
24398ef233eSBram Moolenaar"C:\Ruby<version>\bin\ruby_builtin_dlls" to the PATH environment variable.
24498ef233eSBram Moolenaar
245e18c0b39SBram Moolenaar
246d94464eeSBram MoolenaarUnix ~
247d94464eeSBram Moolenaar
248d94464eeSBram MoolenaarThe 'rubydll' option can be used to specify the Ruby shared library file
249d94464eeSBram Moolenaarinstead of DYNAMIC_RUBY_DLL file what was specified at compile time.  The
250d94464eeSBram Moolenaarversion of the shared library must match the Ruby version Vim was compiled
251d94464eeSBram Moolenaarwith.
252d94464eeSBram Moolenaar
253a5792f58SBram Moolenaar==============================================================================
25491f84f6eSBram Moolenaar vim:tw=78:ts=8:noet:ft=help:norl:
255