blob: ceb3282aa372fc2a06c7150acf37cf947bc3e878 [file] [log] [blame]
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001*if_ruby.txt* For Vim version 7.0b. Last change: 2006 Mar 06
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|
102. The VIM module |ruby-vim|
113. VIM::Buffer objects |ruby-buffer|
124. VIM::Window objects |ruby-window|
135. 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*
28:rub[y] {cmd} Execute Ruby command {cmd}.
29
30:rub[y] << {endpattern}
31{script}
32{endpattern}
33 Execute Ruby script {script}.
34 {endpattern} must NOT be preceded by any white space.
35 If {endpattern} is omitted, it defaults to a dot '.'
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000036 like for the |:append| and |:insert| commands. This
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 form of the |:ruby| command is mainly useful for
38 including ruby code in vim scripts.
39 Note: This command doesn't work when the Ruby feature
40 wasn't compiled in. To avoid errors, see
41 |script-here|.
42
43Example Vim script: >
44
45 function! RedGem()
46 ruby << EOF
47 class Garnet
48 def initialize(s)
49 @buffer = VIM::Buffer.current
50 vimputs(s)
51 end
52 def vimputs(s)
53 @buffer.append(@buffer.count,s)
54 end
55 end
56 gem = Garnet.new("pretty")
57 EOF
58 endfunction
59<
60
61 *:rubydo* *:rubyd* *E265*
62:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
63 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000064 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 the text, but note that it is not possible to add or
66 delete lines using this command.
67 The default for [range] is the whole file: "1,$".
68
69 *:rubyfile* *:rubyf*
70:rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as
71 ":ruby load 'file'", but allows file name completion.
72
73Executing Ruby commands is not possible in the |sandbox|.
74
75==============================================================================
762. The VIM module *ruby-vim*
77
78Ruby code gets all of its access to vim via the "VIM" module.
79
80Overview >
81 print "Hello" # displays a message
82 VIM.command(cmd) # execute an ex command
83 num = VIM::Window.count # gets the number of windows
84 w = VIM::Window[n] # gets window "n"
85 cw = VIM::Window.current # gets the current window
86 num = VIM::Buffer.count # gets the number of buffers
87 b = VIM::Buffer[n] # gets buffer "n"
88 cb = VIM::Buffer.current # gets the current buffer
89 w.height = lines # sets the window height
90 w.cursor = [row, col] # sets the window cursor position
91 pos = w.cursor # gets an array [row, col]
92 name = b.name # gets the buffer file name
93 line = b[n] # gets a line from the buffer
94 num = b.count # gets the number of lines
95 b[n] = str # sets a line in the buffer
96 b.delete(n) # deletes a line
97 b.append(n, str) # appends a line after n
98<
99
100Module Functions:
101
102 *ruby-message*
103VIM::message({msg})
104 Displays the message {msg}.
105
106 *ruby-set_option*
107VIM::set_option({arg})
108 Sets a vim option. {arg} can be any argument that the ":set" command
109 accepts. Note that this means that no spaces are allowed in the
110 argument! See |:set|.
111
112 *ruby-command*
113VIM::command({cmd})
114 Executes Ex command {cmd}.
115
116 *ruby-evaluate*
117VIM::evaluate({expr})
118 Evaluates {expr} using the vim internal expression evaluator (see
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000119 |expression|). Returns the expression result as a string.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 A |List| is turned into a string by joining the items and inserting
121 line breaks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123==============================================================================
1243. VIM::Buffer objects *ruby-buffer*
125
126VIM::Buffer objects represent vim buffers.
127
128Class Methods:
129
130current Returns the current buffer object.
131count Returns the number of buffers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000132self[{n}] Returns the buffer object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 is 0.
134
135Methods:
136
137name Returns the name of the buffer.
138number Returns the number of the buffer.
139count Returns the number of lines.
140length Returns the number of lines.
141self[{n}] Returns a line from the buffer. {n} is the line number.
142self[{n}] = {str}
143 Sets a line in the buffer. {n} is the line number.
144delete({n}) Deletes a line from the buffer. {n} is the line number.
145append({n}, {str})
146 Appends a line after the line {n}.
147
148==============================================================================
1494. VIM::Window objects *ruby-window*
150
151VIM::Window objects represent vim windows.
152
153Class Methods:
154
155current Returns the current window object.
156count Returns the number of windows.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000157self[{n}] Returns the window object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 is 0.
159
160Methods:
161
162buffer Returns the buffer displayed in the window.
163height Returns the height of the window.
164height = {n} Sets the window height to {n}.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000165width Returns the width of the window.
166width = {n} Sets the window width to {n}.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167cursor Returns a [row, col] array for the cursor position.
168cursor = [{row}, {col}]
169 Sets the cursor position to {row} and {col}.
170
171==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001725. Global variables *ruby-globals*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174There are two global variables.
175
176$curwin The current window object.
177$curbuf The current buffer object.
178
179==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00001806. Dynamic loading *ruby-dynamic*
181
182On MS-Windows the Ruby library can be loaded dynamically. The |:version|
183output then includes |+ruby/dyn|.
184
185This means that Vim will search for the Ruby DLL file only when needed. When
186you don't use the Ruby interface you don't need it, thus you can use Vim
187without this DLL file.
188
189To use the Ruby interface the Ruby DLL must be in your search path. In a
190console window type "path" to see what directories are used.
191
192The name of the DLL must match the Ruby version Vim was compiled with.
193Currently the name is "ruby18.dll". That is for Ruby 1.8. To know for sure
194edit "gvim.exe" and search for "ruby\d*.dll\c".
195
196==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 vim:tw=78:ts=8:ft=help:norl: