blob: e2e77421512a5a7a012ab373d030e98344811638 [file] [log] [blame]
Bram Moolenaar314dd792019-02-03 15:27:20 +01001*if_ruby.txt* For Vim version 8.1. Last change: 2019 Jan 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Shugo Maeda
5
6The Ruby Interface to Vim *ruby* *Ruby*
7
8
91. Commands |ruby-commands|
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100102. The Vim module |ruby-vim|
113. Vim::Buffer objects |ruby-buffer|
124. Vim::Window objects |ruby-window|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135. Global variables |ruby-globals|
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100146. rubyeval() Vim function |ruby-rubyeval|
157. Dynamic loading |ruby-dynamic|
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17{Vi does not have any of these commands}
18 *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273*
19
20The Ruby interface only works when Vim was compiled with the |+ruby| feature.
21
22The home page for ruby is http://www.ruby-lang.org/. You can find links for
23downloading Ruby there.
24
25==============================================================================
261. Commands *ruby-commands*
27
28 *:ruby* *:rub*
Bram Moolenaar9b451252012-08-15 17:43:31 +020029:rub[y] {cmd} Execute Ruby command {cmd}. A command to try it out: >
30 :ruby print "Hello"
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
32:rub[y] << {endpattern}
33{script}
34{endpattern}
35 Execute Ruby script {script}.
36 {endpattern} must NOT be preceded by any white space.
37 If {endpattern} is omitted, it defaults to a dot '.'
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000038 like for the |:append| and |:insert| commands. This
Bram Moolenaar071d4272004-06-13 20:20:40 +000039 form of the |:ruby| command is mainly useful for
40 including ruby code in vim scripts.
41 Note: This command doesn't work when the Ruby feature
42 wasn't compiled in. To avoid errors, see
43 |script-here|.
44
45Example Vim script: >
46
47 function! RedGem()
48 ruby << EOF
49 class Garnet
50 def initialize(s)
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010051 @buffer = Vim::Buffer.current
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 vimputs(s)
53 end
54 def vimputs(s)
55 @buffer.append(@buffer.count,s)
56 end
57 end
58 gem = Garnet.new("pretty")
59 EOF
60 endfunction
61<
Bram Moolenaarabd468e2016-09-08 22:22:43 +020062To see what version of Ruby you have: >
63 :ruby print RUBY_VERSION
64<
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66 *:rubydo* *:rubyd* *E265*
67:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
68 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000069 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 the text, but note that it is not possible to add or
71 delete lines using this command.
72 The default for [range] is the whole file: "1,$".
73
74 *:rubyfile* *:rubyf*
75:rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as
Bram Moolenaar214641f2017-03-05 17:04:09 +010076 `:ruby load 'file'`, but allows file name completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78Executing Ruby commands is not possible in the |sandbox|.
79
80==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100812. The Vim module *ruby-vim*
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010083Ruby code gets all of its access to vim via the "Vim" module.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010085Overview: >
Bram Moolenaar899dddf2006-03-26 21:06:50 +000086 print "Hello" # displays a message
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010087 Vim.command(cmd) # execute an Ex command
88 num = Vim::Window.count # gets the number of windows
89 w = Vim::Window[n] # gets window "n"
90 cw = Vim::Window.current # gets the current window
91 num = Vim::Buffer.count # gets the number of buffers
92 b = Vim::Buffer[n] # gets buffer "n"
93 cb = Vim::Buffer.current # gets the current buffer
Bram Moolenaar899dddf2006-03-26 21:06:50 +000094 w.height = lines # sets the window height
95 w.cursor = [row, col] # sets the window cursor position
96 pos = w.cursor # gets an array [row, col]
97 name = b.name # gets the buffer file name
98 line = b[n] # gets a line from the buffer
99 num = b.count # gets the number of lines
100 b[n] = str # sets a line in the buffer
101 b.delete(n) # deletes a line
102 b.append(n, str) # appends a line after n
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100103 line = Vim::Buffer.current.line # gets the current line
104 num = Vim::Buffer.current.line_number # gets the current line number
105 Vim::Buffer.current.line = "test" # sets the current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106<
107
108Module Functions:
109
110 *ruby-message*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100111Vim::message({msg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 Displays the message {msg}.
113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100114 *ruby-blob*
115Vim::blob({arg})
Bram Moolenaar314dd792019-02-03 15:27:20 +0100116 Return |Blob| literal string from {arg}.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100117
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 *ruby-set_option*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100119Vim::set_option({arg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 Sets a vim option. {arg} can be any argument that the ":set" command
121 accepts. Note that this means that no spaces are allowed in the
122 argument! See |:set|.
123
124 *ruby-command*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100125Vim::command({cmd})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 Executes Ex command {cmd}.
127
128 *ruby-evaluate*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100129Vim::evaluate({expr})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 Evaluates {expr} using the vim internal expression evaluator (see
Bram Moolenaar2b8388b2015-02-28 13:11:45 +0100131 |expression|). Returns the expression result as:
132 - a Integer if the Vim expression evaluates to a number
133 - a Float if the Vim expression evaluates to a float
134 - a String if the Vim expression evaluates to a string
135 - a Array if the Vim expression evaluates to a Vim list
136 - a Hash if the Vim expression evaluates to a Vim dictionary
137 Dictionaries and lists are recursively expanded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001403. Vim::Buffer objects *ruby-buffer*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100142Vim::Buffer objects represent vim buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144Class Methods:
145
146current Returns the current buffer object.
147count Returns the number of buffers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000148self[{n}] Returns the buffer object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 is 0.
150
151Methods:
152
Bram Moolenaaredd6aac2018-07-28 17:29:19 +0200153name Returns the full name of the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154number Returns the number of the buffer.
155count Returns the number of lines.
156length Returns the number of lines.
157self[{n}] Returns a line from the buffer. {n} is the line number.
158self[{n}] = {str}
159 Sets a line in the buffer. {n} is the line number.
160delete({n}) Deletes a line from the buffer. {n} is the line number.
161append({n}, {str})
162 Appends a line after the line {n}.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000163line Returns the current line of the buffer if the buffer is
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000164 active.
165line = {str} Sets the current line of the buffer if the buffer is active.
166line_number Returns the number of the current line if the buffer is
167 active.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001704. Vim::Window objects *ruby-window*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100172Vim::Window objects represent vim windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174Class Methods:
175
176current Returns the current window object.
177count Returns the number of windows.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000178self[{n}] Returns the window object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 is 0.
180
181Methods:
182
183buffer Returns the buffer displayed in the window.
184height Returns the height of the window.
185height = {n} Sets the window height to {n}.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000186width Returns the width of the window.
187width = {n} Sets the window width to {n}.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188cursor Returns a [row, col] array for the cursor position.
Bram Moolenaaredd6aac2018-07-28 17:29:19 +0200189 First line number is 1 and first column number is 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190cursor = [{row}, {col}]
191 Sets the cursor position to {row} and {col}.
192
193==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001945. Global variables *ruby-globals*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196There are two global variables.
197
198$curwin The current window object.
199$curbuf The current buffer object.
200
201==============================================================================
Bram Moolenaare99be0e2019-03-26 22:51:09 +01002026. rubyeval() Vim function *ruby-rubyeval*
203
204To facilitate bi-directional interface, you can use |rubyeval()| function to
205evaluate Ruby expressions and pass their values to Vim script.
206
207The Ruby value "true", "false" and "nil" are converted to v:true, v:false and
208v:null, respectively.
209
210==============================================================================
2117. Dynamic loading *ruby-dynamic*
Bram Moolenaara5792f52005-11-23 21:25:05 +0000212
Bram Moolenaar05365702010-10-27 18:34:44 +0200213On MS-Windows and Unix the Ruby library can be loaded dynamically. The
214|:version| output then includes |+ruby/dyn|.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000215
Bram Moolenaar05365702010-10-27 18:34:44 +0200216This means that Vim will search for the Ruby DLL file or shared library only
217when needed. When you don't use the Ruby interface you don't need it, thus
218you can use Vim even though this library file is not on your system.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000219
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100220
Bram Moolenaard94464e2015-11-02 15:28:18 +0100221MS-Windows ~
222
Bram Moolenaar69154f22010-07-18 21:42:34 +0200223You need to install the right version of Ruby for this to work. You can find
224the package to download from:
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200225http://rubyinstaller.org/downloads/
226Currently that is rubyinstaller-2.2.5.exe
Bram Moolenaar69154f22010-07-18 21:42:34 +0200227
Bram Moolenaara5792f52005-11-23 21:25:05 +0000228To use the Ruby interface the Ruby DLL must be in your search path. In a
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100229console window type "path" to see what directories are used. The 'rubydll'
230option can be also used to specify the Ruby DLL.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000231
232The name of the DLL must match the Ruby version Vim was compiled with.
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200233Currently the name is "msvcrt-ruby220.dll". That is for Ruby 2.2.X. To know
Bram Moolenaar69154f22010-07-18 21:42:34 +0200234for sure edit "gvim.exe" and search for "ruby\d*.dll\c".
235
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200236If you want to build Vim with RubyInstaller 1.9 or 2.X using MSVC, you need
237some tricks. See the src/INSTALLpc.txt for detail.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100239If Vim is built with RubyInstaller 2.4 or later, you may also need to add
240"C:\Ruby<version>\bin\ruby_builtin_dlls" to the PATH environment variable.
241
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100242
Bram Moolenaard94464e2015-11-02 15:28:18 +0100243Unix ~
244
245The 'rubydll' option can be used to specify the Ruby shared library file
246instead of DYNAMIC_RUBY_DLL file what was specified at compile time. The
247version of the shared library must match the Ruby version Vim was compiled
248with.
249
Bram Moolenaara5792f52005-11-23 21:25:05 +0000250==============================================================================
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200251 vim:tw=78:ts=8:noet:ft=help:norl: