Christian Brabandt | b4ddc6c | 2024-01-02 16:51:11 +0100 | [diff] [blame] | 1 | *if_ruby.txt* For Vim version 9.1. Last change: 2019 Jul 21 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Shugo Maeda |
| 5 | |
| 6 | The Ruby Interface to Vim *ruby* *Ruby* |
| 7 | |
| 8 | |
| 9 | 1. Commands |ruby-commands| |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 10 | 2. The Vim module |ruby-vim| |
| 11 | 3. Vim::Buffer objects |ruby-buffer| |
| 12 | 4. Vim::Window objects |ruby-window| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | 5. Global variables |ruby-globals| |
Bram Moolenaar | e99be0e | 2019-03-26 22:51:09 +0100 | [diff] [blame] | 14 | 6. rubyeval() Vim function |ruby-rubyeval| |
| 15 | 7. Dynamic loading |ruby-dynamic| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273* |
| 18 | |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 19 | {only available when Vim was compiled with the |+ruby| feature} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | |
| 21 | The home page for ruby is http://www.ruby-lang.org/. You can find links for |
| 22 | downloading Ruby there. |
| 23 | |
| 24 | ============================================================================== |
| 25 | 1. Commands *ruby-commands* |
| 26 | |
| 27 | *:ruby* *:rub* |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 28 | :rub[y] {cmd} Execute Ruby command {cmd}. A command to try it out: > |
| 29 | :ruby print "Hello" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 31 | :rub[y] << [trim] [{endmarker}] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | {script} |
Bram Moolenaar | 2e693a8 | 2019-10-16 22:35:02 +0200 | [diff] [blame] | 33 | {endmarker} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | Execute Ruby script {script}. |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 35 | |
Bram Moolenaar | 2e693a8 | 2019-10-16 22:35:02 +0200 | [diff] [blame] | 36 | If [endmarker] is omitted, it defaults to a dot '.' |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 37 | like for the |:append| and |:insert| commands. Refer |
| 38 | to |:let-heredoc| for more information. |
| 39 | |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 40 | |
| 41 | This form of the |:ruby| command is mainly useful for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | including ruby code in vim scripts. |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 43 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | Note: This command doesn't work when the Ruby feature |
| 45 | wasn't compiled in. To avoid errors, see |
| 46 | |script-here|. |
| 47 | |
| 48 | Example Vim script: > |
| 49 | |
| 50 | function! RedGem() |
| 51 | ruby << EOF |
| 52 | class Garnet |
| 53 | def initialize(s) |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 54 | @buffer = Vim::Buffer.current |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | vimputs(s) |
| 56 | end |
| 57 | def vimputs(s) |
| 58 | @buffer.append(@buffer.count,s) |
| 59 | end |
| 60 | end |
| 61 | gem = Garnet.new("pretty") |
| 62 | EOF |
| 63 | endfunction |
| 64 | < |
Bram Moolenaar | abd468e | 2016-09-08 22:22:43 +0200 | [diff] [blame] | 65 | To see what version of Ruby you have: > |
| 66 | :ruby print RUBY_VERSION |
| 67 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | |
| 69 | *:rubydo* *:rubyd* *E265* |
| 70 | :[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the |
| 71 | [range], with $_ being set to the text of each line in |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 72 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | the text, but note that it is not possible to add or |
| 74 | delete lines using this command. |
| 75 | The default for [range] is the whole file: "1,$". |
| 76 | |
| 77 | *:rubyfile* *:rubyf* |
| 78 | :rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 79 | `:ruby load 'file'`, but allows file name completion. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | |
| 81 | Executing Ruby commands is not possible in the |sandbox|. |
| 82 | |
| 83 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 84 | 2. The Vim module *ruby-vim* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 86 | Ruby code gets all of its access to vim via the "Vim" module. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 88 | Overview: > |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 89 | print "Hello" # displays a message |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 90 | Vim.command(cmd) # execute an Ex command |
| 91 | num = Vim::Window.count # gets the number of windows |
| 92 | w = Vim::Window[n] # gets window "n" |
| 93 | cw = Vim::Window.current # gets the current window |
| 94 | num = Vim::Buffer.count # gets the number of buffers |
| 95 | b = Vim::Buffer[n] # gets buffer "n" |
| 96 | cb = Vim::Buffer.current # gets the current buffer |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 97 | w.height = lines # sets the window height |
| 98 | w.cursor = [row, col] # sets the window cursor position |
| 99 | pos = w.cursor # gets an array [row, col] |
| 100 | name = b.name # gets the buffer file name |
| 101 | line = b[n] # gets a line from the buffer |
| 102 | num = b.count # gets the number of lines |
| 103 | b[n] = str # sets a line in the buffer |
| 104 | b.delete(n) # deletes a line |
| 105 | b.append(n, str) # appends a line after n |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 106 | line = Vim::Buffer.current.line # gets the current line |
| 107 | num = Vim::Buffer.current.line_number # gets the current line number |
| 108 | Vim::Buffer.current.line = "test" # sets the current line number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | < |
| 110 | |
| 111 | Module Functions: |
| 112 | |
| 113 | *ruby-message* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 114 | Vim::message({msg}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | Displays the message {msg}. |
| 116 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 117 | *ruby-blob* |
| 118 | Vim::blob({arg}) |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 119 | Return |Blob| literal string from {arg}. |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 120 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | *ruby-set_option* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 122 | Vim::set_option({arg}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | Sets a vim option. {arg} can be any argument that the ":set" command |
| 124 | accepts. Note that this means that no spaces are allowed in the |
| 125 | argument! See |:set|. |
| 126 | |
| 127 | *ruby-command* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 128 | Vim::command({cmd}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | Executes Ex command {cmd}. |
| 130 | |
| 131 | *ruby-evaluate* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 132 | Vim::evaluate({expr}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | Evaluates {expr} using the vim internal expression evaluator (see |
Bram Moolenaar | 2b8388b | 2015-02-28 13:11:45 +0100 | [diff] [blame] | 134 | |expression|). Returns the expression result as: |
| 135 | - a Integer if the Vim expression evaluates to a number |
| 136 | - a Float if the Vim expression evaluates to a float |
| 137 | - a String if the Vim expression evaluates to a string |
| 138 | - a Array if the Vim expression evaluates to a Vim list |
| 139 | - a Hash if the Vim expression evaluates to a Vim dictionary |
| 140 | Dictionaries and lists are recursively expanded. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | |
| 142 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 143 | 3. Vim::Buffer objects *ruby-buffer* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 145 | Vim::Buffer objects represent vim buffers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | |
| 147 | Class Methods: |
| 148 | |
| 149 | current Returns the current buffer object. |
| 150 | count Returns the number of buffers. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 151 | self[{n}] Returns the buffer object for the number {n}. The first number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | is 0. |
| 153 | |
| 154 | Methods: |
| 155 | |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 156 | name Returns the full name of the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | number Returns the number of the buffer. |
| 158 | count Returns the number of lines. |
| 159 | length Returns the number of lines. |
| 160 | self[{n}] Returns a line from the buffer. {n} is the line number. |
| 161 | self[{n}] = {str} |
| 162 | Sets a line in the buffer. {n} is the line number. |
| 163 | delete({n}) Deletes a line from the buffer. {n} is the line number. |
| 164 | append({n}, {str}) |
| 165 | Appends a line after the line {n}. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 166 | line Returns the current line of the buffer if the buffer is |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 167 | active. |
| 168 | line = {str} Sets the current line of the buffer if the buffer is active. |
| 169 | line_number Returns the number of the current line if the buffer is |
| 170 | active. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 171 | |
| 172 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 173 | 4. Vim::Window objects *ruby-window* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 175 | Vim::Window objects represent vim windows. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | |
| 177 | Class Methods: |
| 178 | |
| 179 | current Returns the current window object. |
| 180 | count Returns the number of windows. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 181 | self[{n}] Returns the window object for the number {n}. The first number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | is 0. |
| 183 | |
| 184 | Methods: |
| 185 | |
| 186 | buffer Returns the buffer displayed in the window. |
| 187 | height Returns the height of the window. |
| 188 | height = {n} Sets the window height to {n}. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 189 | width Returns the width of the window. |
| 190 | width = {n} Sets the window width to {n}. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | cursor Returns a [row, col] array for the cursor position. |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 192 | First line number is 1 and first column number is 0. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | cursor = [{row}, {col}] |
| 194 | Sets the cursor position to {row} and {col}. |
| 195 | |
| 196 | ============================================================================== |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 197 | 5. Global variables *ruby-globals* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | |
| 199 | There are two global variables. |
| 200 | |
| 201 | $curwin The current window object. |
| 202 | $curbuf The current buffer object. |
| 203 | |
| 204 | ============================================================================== |
Bram Moolenaar | e99be0e | 2019-03-26 22:51:09 +0100 | [diff] [blame] | 205 | 6. rubyeval() Vim function *ruby-rubyeval* |
| 206 | |
| 207 | To facilitate bi-directional interface, you can use |rubyeval()| function to |
| 208 | evaluate Ruby expressions and pass their values to Vim script. |
| 209 | |
| 210 | The Ruby value "true", "false" and "nil" are converted to v:true, v:false and |
| 211 | v:null, respectively. |
| 212 | |
| 213 | ============================================================================== |
| 214 | 7. Dynamic loading *ruby-dynamic* |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 215 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 216 | On MS-Windows and Unix the Ruby library can be loaded dynamically. The |
| 217 | |:version| output then includes |+ruby/dyn|. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 218 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 219 | This means that Vim will search for the Ruby DLL file or shared library only |
| 220 | when needed. When you don't use the Ruby interface you don't need it, thus |
| 221 | you can use Vim even though this library file is not on your system. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 222 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 224 | MS-Windows ~ |
| 225 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 226 | You need to install the right version of Ruby for this to work. You can find |
| 227 | the package to download from: |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 228 | http://rubyinstaller.org/downloads/ |
| 229 | Currently that is rubyinstaller-2.2.5.exe |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 230 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 231 | To use the Ruby interface the Ruby DLL must be in your search path. In a |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 232 | console window type "path" to see what directories are used. The 'rubydll' |
| 233 | option can be also used to specify the Ruby DLL. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 234 | |
| 235 | The name of the DLL must match the Ruby version Vim was compiled with. |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 236 | Currently the name is "msvcrt-ruby220.dll". That is for Ruby 2.2.X. To know |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 237 | for sure edit "gvim.exe" and search for "ruby\d*.dll\c". |
| 238 | |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 239 | If you want to build Vim with RubyInstaller 1.9 or 2.X using MSVC, you need |
| 240 | some tricks. See the src/INSTALLpc.txt for detail. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 241 | |
Bram Moolenaar | 98ef233 | 2018-03-18 14:44:37 +0100 | [diff] [blame] | 242 | If Vim is built with RubyInstaller 2.4 or later, you may also need to add |
| 243 | "C:\Ruby<version>\bin\ruby_builtin_dlls" to the PATH environment variable. |
| 244 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 245 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 246 | Unix ~ |
| 247 | |
| 248 | The 'rubydll' option can be used to specify the Ruby shared library file |
| 249 | instead of DYNAMIC_RUBY_DLL file what was specified at compile time. The |
| 250 | version of the shared library must match the Ruby version Vim was compiled |
| 251 | with. |
| 252 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 253 | ============================================================================== |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 254 | vim:tw=78:ts=8:noet:ft=help:norl: |