blob: 1b4fd40f3cdbb381b171f981f3386919a520722c [file] [log] [blame]
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001*if_ruby.txt* For Vim version 8.1. Last change: 2018 Mar 15
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 Moolenaara5792f52005-11-23 21:25:05 +0000146. Dynamic loading |ruby-dynamic|
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16{Vi does not have any of these commands}
17 *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273*
18
19The Ruby interface only works when Vim was compiled with the |+ruby| feature.
20
21The home page for ruby is http://www.ruby-lang.org/. You can find links for
22downloading Ruby there.
23
24==============================================================================
251. Commands *ruby-commands*
26
27 *:ruby* *:rub*
Bram Moolenaar9b451252012-08-15 17:43:31 +020028:rub[y] {cmd} Execute Ruby command {cmd}. A command to try it out: >
29 :ruby print "Hello"
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31:rub[y] << {endpattern}
32{script}
33{endpattern}
34 Execute Ruby script {script}.
35 {endpattern} must NOT be preceded by any white space.
36 If {endpattern} is omitted, it defaults to a dot '.'
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000037 like for the |:append| and |:insert| commands. This
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 form of the |:ruby| command is mainly useful for
39 including ruby code in vim scripts.
40 Note: This command doesn't work when the Ruby feature
41 wasn't compiled in. To avoid errors, see
42 |script-here|.
43
44Example Vim script: >
45
46 function! RedGem()
47 ruby << EOF
48 class Garnet
49 def initialize(s)
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010050 @buffer = Vim::Buffer.current
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 vimputs(s)
52 end
53 def vimputs(s)
54 @buffer.append(@buffer.count,s)
55 end
56 end
57 gem = Garnet.new("pretty")
58 EOF
59 endfunction
60<
Bram Moolenaarabd468e2016-09-08 22:22:43 +020061To see what version of Ruby you have: >
62 :ruby print RUBY_VERSION
63<
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
65 *:rubydo* *:rubyd* *E265*
66:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
67 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000068 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 the text, but note that it is not possible to add or
70 delete lines using this command.
71 The default for [range] is the whole file: "1,$".
72
73 *:rubyfile* *:rubyf*
74:rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as
Bram Moolenaar214641f2017-03-05 17:04:09 +010075 `:ruby load 'file'`, but allows file name completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
77Executing Ruby commands is not possible in the |sandbox|.
78
79==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100802. The Vim module *ruby-vim*
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010082Ruby code gets all of its access to vim via the "Vim" module.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010084Overview: >
Bram Moolenaar899dddf2006-03-26 21:06:50 +000085 print "Hello" # displays a message
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010086 Vim.command(cmd) # execute an Ex command
87 num = Vim::Window.count # gets the number of windows
88 w = Vim::Window[n] # gets window "n"
89 cw = Vim::Window.current # gets the current window
90 num = Vim::Buffer.count # gets the number of buffers
91 b = Vim::Buffer[n] # gets buffer "n"
92 cb = Vim::Buffer.current # gets the current buffer
Bram Moolenaar899dddf2006-03-26 21:06:50 +000093 w.height = lines # sets the window height
94 w.cursor = [row, col] # sets the window cursor position
95 pos = w.cursor # gets an array [row, col]
96 name = b.name # gets the buffer file name
97 line = b[n] # gets a line from the buffer
98 num = b.count # gets the number of lines
99 b[n] = str # sets a line in the buffer
100 b.delete(n) # deletes a line
101 b.append(n, str) # appends a line after n
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100102 line = Vim::Buffer.current.line # gets the current line
103 num = Vim::Buffer.current.line_number # gets the current line number
104 Vim::Buffer.current.line = "test" # sets the current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105<
106
107Module Functions:
108
109 *ruby-message*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100110Vim::message({msg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 Displays the message {msg}.
112
113 *ruby-set_option*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100114Vim::set_option({arg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 Sets a vim option. {arg} can be any argument that the ":set" command
116 accepts. Note that this means that no spaces are allowed in the
117 argument! See |:set|.
118
119 *ruby-command*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100120Vim::command({cmd})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 Executes Ex command {cmd}.
122
123 *ruby-evaluate*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100124Vim::evaluate({expr})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 Evaluates {expr} using the vim internal expression evaluator (see
Bram Moolenaar2b8388b2015-02-28 13:11:45 +0100126 |expression|). Returns the expression result as:
127 - a Integer if the Vim expression evaluates to a number
128 - a Float if the Vim expression evaluates to a float
129 - a String if the Vim expression evaluates to a string
130 - a Array if the Vim expression evaluates to a Vim list
131 - a Hash if the Vim expression evaluates to a Vim dictionary
132 Dictionaries and lists are recursively expanded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
134==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001353. Vim::Buffer objects *ruby-buffer*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100137Vim::Buffer objects represent vim buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139Class Methods:
140
141current Returns the current buffer object.
142count Returns the number of buffers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000143self[{n}] Returns the buffer object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 is 0.
145
146Methods:
147
148name Returns the name of the buffer.
149number Returns the number of the buffer.
150count Returns the number of lines.
151length Returns the number of lines.
152self[{n}] Returns a line from the buffer. {n} is the line number.
153self[{n}] = {str}
154 Sets a line in the buffer. {n} is the line number.
155delete({n}) Deletes a line from the buffer. {n} is the line number.
156append({n}, {str})
157 Appends a line after the line {n}.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000158line Returns the current line of the buffer if the buffer is
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000159 active.
160line = {str} Sets the current line of the buffer if the buffer is active.
161line_number Returns the number of the current line if the buffer is
162 active.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
164==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001654. Vim::Window objects *ruby-window*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100167Vim::Window objects represent vim windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169Class Methods:
170
171current Returns the current window object.
172count Returns the number of windows.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173self[{n}] Returns the window object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 is 0.
175
176Methods:
177
178buffer Returns the buffer displayed in the window.
179height Returns the height of the window.
180height = {n} Sets the window height to {n}.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000181width Returns the width of the window.
182width = {n} Sets the window width to {n}.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183cursor Returns a [row, col] array for the cursor position.
184cursor = [{row}, {col}]
185 Sets the cursor position to {row} and {col}.
186
187==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001885. Global variables *ruby-globals*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190There are two global variables.
191
192$curwin The current window object.
193$curbuf The current buffer object.
194
195==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001966. Dynamic loading *ruby-dynamic*
197
Bram Moolenaar05365702010-10-27 18:34:44 +0200198On MS-Windows and Unix the Ruby library can be loaded dynamically. The
199|:version| output then includes |+ruby/dyn|.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000200
Bram Moolenaar05365702010-10-27 18:34:44 +0200201This means that Vim will search for the Ruby DLL file or shared library only
202when needed. When you don't use the Ruby interface you don't need it, thus
203you can use Vim even though this library file is not on your system.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000204
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100205
Bram Moolenaard94464e2015-11-02 15:28:18 +0100206MS-Windows ~
207
Bram Moolenaar69154f22010-07-18 21:42:34 +0200208You need to install the right version of Ruby for this to work. You can find
209the package to download from:
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200210http://rubyinstaller.org/downloads/
211Currently that is rubyinstaller-2.2.5.exe
Bram Moolenaar69154f22010-07-18 21:42:34 +0200212
Bram Moolenaara5792f52005-11-23 21:25:05 +0000213To use the Ruby interface the Ruby DLL must be in your search path. In a
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100214console window type "path" to see what directories are used. The 'rubydll'
215option can be also used to specify the Ruby DLL.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000216
217The name of the DLL must match the Ruby version Vim was compiled with.
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200218Currently the name is "msvcrt-ruby220.dll". That is for Ruby 2.2.X. To know
Bram Moolenaar69154f22010-07-18 21:42:34 +0200219for sure edit "gvim.exe" and search for "ruby\d*.dll\c".
220
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200221If you want to build Vim with RubyInstaller 1.9 or 2.X using MSVC, you need
222some tricks. See the src/INSTALLpc.txt for detail.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000223
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100224If Vim is built with RubyInstaller 2.4 or later, you may also need to add
225"C:\Ruby<version>\bin\ruby_builtin_dlls" to the PATH environment variable.
226
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100227
Bram Moolenaard94464e2015-11-02 15:28:18 +0100228Unix ~
229
230The 'rubydll' option can be used to specify the Ruby shared library file
231instead of DYNAMIC_RUBY_DLL file what was specified at compile time. The
232version of the shared library must match the Ruby version Vim was compiled
233with.
234
Bram Moolenaara5792f52005-11-23 21:25:05 +0000235==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 vim:tw=78:ts=8:ft=help:norl: