blob: 3ca8cc510b1aa235e82328de7a229d18f506c732 [file] [log] [blame]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001*if_ruby.txt* For Vim version 7.0aa. Last change: 2005 Mar 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|
102. The VIM module |ruby-vim|
113. VIM::Buffer objects |ruby-buffer|
124. VIM::Window objects |ruby-window|
135. Global variables |ruby-globals|
14
15{Vi does not have any of these commands}
16 *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273*
17
18The Ruby interface only works when Vim was compiled with the |+ruby| feature.
19
20The home page for ruby is http://www.ruby-lang.org/. You can find links for
21downloading Ruby there.
22
23==============================================================================
241. Commands *ruby-commands*
25
26 *:ruby* *:rub*
27:rub[y] {cmd} Execute Ruby command {cmd}.
28
29:rub[y] << {endpattern}
30{script}
31{endpattern}
32 Execute Ruby script {script}.
33 {endpattern} must NOT be preceded by any white space.
34 If {endpattern} is omitted, it defaults to a dot '.'
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000035 like for the |:append| and |:insert| commands. This
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 form of the |:ruby| command is mainly useful for
37 including ruby code in vim scripts.
38 Note: This command doesn't work when the Ruby feature
39 wasn't compiled in. To avoid errors, see
40 |script-here|.
41
42Example Vim script: >
43
44 function! RedGem()
45 ruby << EOF
46 class Garnet
47 def initialize(s)
48 @buffer = VIM::Buffer.current
49 vimputs(s)
50 end
51 def vimputs(s)
52 @buffer.append(@buffer.count,s)
53 end
54 end
55 gem = Garnet.new("pretty")
56 EOF
57 endfunction
58<
59
60 *:rubydo* *:rubyd* *E265*
61:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
62 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000063 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 the text, but note that it is not possible to add or
65 delete lines using this command.
66 The default for [range] is the whole file: "1,$".
67
68 *:rubyfile* *:rubyf*
69:rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as
70 ":ruby load 'file'", but allows file name completion.
71
72Executing Ruby commands is not possible in the |sandbox|.
73
74==============================================================================
752. The VIM module *ruby-vim*
76
77Ruby code gets all of its access to vim via the "VIM" module.
78
79Overview >
80 print "Hello" # displays a message
81 VIM.command(cmd) # execute an ex command
82 num = VIM::Window.count # gets the number of windows
83 w = VIM::Window[n] # gets window "n"
84 cw = VIM::Window.current # gets the current window
85 num = VIM::Buffer.count # gets the number of buffers
86 b = VIM::Buffer[n] # gets buffer "n"
87 cb = VIM::Buffer.current # gets the current buffer
88 w.height = lines # sets the window height
89 w.cursor = [row, col] # sets the window cursor position
90 pos = w.cursor # gets an array [row, col]
91 name = b.name # gets the buffer file name
92 line = b[n] # gets a line from the buffer
93 num = b.count # gets the number of lines
94 b[n] = str # sets a line in the buffer
95 b.delete(n) # deletes a line
96 b.append(n, str) # appends a line after n
97<
98
99Module Functions:
100
101 *ruby-message*
102VIM::message({msg})
103 Displays the message {msg}.
104
105 *ruby-set_option*
106VIM::set_option({arg})
107 Sets a vim option. {arg} can be any argument that the ":set" command
108 accepts. Note that this means that no spaces are allowed in the
109 argument! See |:set|.
110
111 *ruby-command*
112VIM::command({cmd})
113 Executes Ex command {cmd}.
114
115 *ruby-evaluate*
116VIM::evaluate({expr})
117 Evaluates {expr} using the vim internal expression evaluator (see
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000118 |expression|). Returns the expression result as a string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120==============================================================================
1213. VIM::Buffer objects *ruby-buffer*
122
123VIM::Buffer objects represent vim buffers.
124
125Class Methods:
126
127current Returns the current buffer object.
128count Returns the number of buffers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000129self[{n}] Returns the buffer object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 is 0.
131
132Methods:
133
134name Returns the name of the buffer.
135number Returns the number of the buffer.
136count Returns the number of lines.
137length Returns the number of lines.
138self[{n}] Returns a line from the buffer. {n} is the line number.
139self[{n}] = {str}
140 Sets a line in the buffer. {n} is the line number.
141delete({n}) Deletes a line from the buffer. {n} is the line number.
142append({n}, {str})
143 Appends a line after the line {n}.
144
145==============================================================================
1464. VIM::Window objects *ruby-window*
147
148VIM::Window objects represent vim windows.
149
150Class Methods:
151
152current Returns the current window object.
153count Returns the number of windows.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000154self[{n}] Returns the window object for the number {n}. The first number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 is 0.
156
157Methods:
158
159buffer Returns the buffer displayed in the window.
160height Returns the height of the window.
161height = {n} Sets the window height to {n}.
162cursor Returns a [row, col] array for the cursor position.
163cursor = [{row}, {col}]
164 Sets the cursor position to {row} and {col}.
165
166==============================================================================
1674. Global variables *ruby-globals*
168
169There are two global variables.
170
171$curwin The current window object.
172$curbuf The current buffer object.
173
174==============================================================================
175 vim:tw=78:ts=8:ft=help:norl: