blob: b0f8029985e5e2cfc4d74c80538eaa14a44f7cd8 [file] [log] [blame]
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001*if_ruby.txt* For Vim version 7.4. Last change: 2015 Dec 03
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<
61
62 *:rubydo* *:rubyd* *E265*
63:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
64 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000065 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 the text, but note that it is not possible to add or
67 delete lines using this command.
68 The default for [range] is the whole file: "1,$".
69
70 *:rubyfile* *:rubyf*
71:rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as
72 ":ruby load 'file'", but allows file name completion.
73
74Executing Ruby commands is not possible in the |sandbox|.
75
76==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100772. The Vim module *ruby-vim*
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010079Ruby code gets all of its access to vim via the "Vim" module.
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010081Overview: >
Bram Moolenaar899dddf2006-03-26 21:06:50 +000082 print "Hello" # displays a message
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010083 Vim.command(cmd) # execute an Ex command
84 num = Vim::Window.count # gets the number of windows
85 w = Vim::Window[n] # gets window "n"
86 cw = Vim::Window.current # gets the current window
87 num = Vim::Buffer.count # gets the number of buffers
88 b = Vim::Buffer[n] # gets buffer "n"
89 cb = Vim::Buffer.current # gets the current buffer
Bram Moolenaar899dddf2006-03-26 21:06:50 +000090 w.height = lines # sets the window height
91 w.cursor = [row, col] # sets the window cursor position
92 pos = w.cursor # gets an array [row, col]
93 name = b.name # gets the buffer file name
94 line = b[n] # gets a line from the buffer
95 num = b.count # gets the number of lines
96 b[n] = str # sets a line in the buffer
97 b.delete(n) # deletes a line
98 b.append(n, str) # appends a line after n
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +010099 line = Vim::Buffer.current.line # gets the current line
100 num = Vim::Buffer.current.line_number # gets the current line number
101 Vim::Buffer.current.line = "test" # sets the current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102<
103
104Module Functions:
105
106 *ruby-message*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100107Vim::message({msg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 Displays the message {msg}.
109
110 *ruby-set_option*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100111Vim::set_option({arg})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 Sets a vim option. {arg} can be any argument that the ":set" command
113 accepts. Note that this means that no spaces are allowed in the
114 argument! See |:set|.
115
116 *ruby-command*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100117Vim::command({cmd})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 Executes Ex command {cmd}.
119
120 *ruby-evaluate*
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100121Vim::evaluate({expr})
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 Evaluates {expr} using the vim internal expression evaluator (see
Bram Moolenaar2b8388b2015-02-28 13:11:45 +0100123 |expression|). Returns the expression result as:
124 - a Integer if the Vim expression evaluates to a number
125 - a Float if the Vim expression evaluates to a float
126 - a String if the Vim expression evaluates to a string
127 - a Array if the Vim expression evaluates to a Vim list
128 - a Hash if the Vim expression evaluates to a Vim dictionary
129 Dictionaries and lists are recursively expanded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001323. Vim::Buffer objects *ruby-buffer*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100134Vim::Buffer objects represent vim buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136Class Methods:
137
138current Returns the current buffer object.
139count Returns the number of buffers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000140self[{n}] Returns the buffer object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 is 0.
142
143Methods:
144
145name Returns the name of the buffer.
146number Returns the number of the buffer.
147count Returns the number of lines.
148length Returns the number of lines.
149self[{n}] Returns a line from the buffer. {n} is the line number.
150self[{n}] = {str}
151 Sets a line in the buffer. {n} is the line number.
152delete({n}) Deletes a line from the buffer. {n} is the line number.
153append({n}, {str})
154 Appends a line after the line {n}.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000155line Returns the current line of the buffer if the buffer is
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000156 active.
157line = {str} Sets the current line of the buffer if the buffer is active.
158line_number Returns the number of the current line if the buffer is
159 active.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
161==============================================================================
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +01001624. Vim::Window objects *ruby-window*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar2c5e8e82015-12-05 20:59:21 +0100164Vim::Window objects represent vim windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
166Class Methods:
167
168current Returns the current window object.
169count Returns the number of windows.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170self[{n}] Returns the window object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 is 0.
172
173Methods:
174
175buffer Returns the buffer displayed in the window.
176height Returns the height of the window.
177height = {n} Sets the window height to {n}.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000178width Returns the width of the window.
179width = {n} Sets the window width to {n}.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180cursor Returns a [row, col] array for the cursor position.
181cursor = [{row}, {col}]
182 Sets the cursor position to {row} and {col}.
183
184==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001855. Global variables *ruby-globals*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187There are two global variables.
188
189$curwin The current window object.
190$curbuf The current buffer object.
191
192==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001936. Dynamic loading *ruby-dynamic*
194
Bram Moolenaar05365702010-10-27 18:34:44 +0200195On MS-Windows and Unix the Ruby library can be loaded dynamically. The
196|:version| output then includes |+ruby/dyn|.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000197
Bram Moolenaar05365702010-10-27 18:34:44 +0200198This means that Vim will search for the Ruby DLL file or shared library only
199when needed. When you don't use the Ruby interface you don't need it, thus
200you can use Vim even though this library file is not on your system.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000201
Bram Moolenaard94464e2015-11-02 15:28:18 +0100202MS-Windows ~
203
Bram Moolenaar69154f22010-07-18 21:42:34 +0200204You need to install the right version of Ruby for this to work. You can find
205the package to download from:
206http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html
207Currently that is ruby-1.9.1-p429-i386-mswin32.zip
208
Bram Moolenaara5792f52005-11-23 21:25:05 +0000209To use the Ruby interface the Ruby DLL must be in your search path. In a
210console window type "path" to see what directories are used.
211
212The name of the DLL must match the Ruby version Vim was compiled with.
Bram Moolenaar69154f22010-07-18 21:42:34 +0200213Currently the name is "msvcrt-ruby191.dll". That is for Ruby 1.9.1. To know
214for sure edit "gvim.exe" and search for "ruby\d*.dll\c".
215
216If you want to build Vim with Ruby 1.9.1, you need to edit the config.h file
217and comment-out the check for _MSC_VER.
Bram Moolenaar9b451252012-08-15 17:43:31 +0200218You may also need to rename the include directory name to match the version,
219strangely for Ruby 1.9.3 the directory is called 1.9.1.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000220
Bram Moolenaard94464e2015-11-02 15:28:18 +0100221Unix ~
222
223The 'rubydll' option can be used to specify the Ruby shared library file
224instead of DYNAMIC_RUBY_DLL file what was specified at compile time. The
225version of the shared library must match the Ruby version Vim was compiled
226with.
227
Bram Moolenaara5792f52005-11-23 21:25:05 +0000228==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 vim:tw=78:ts=8:ft=help:norl: