Bram Moolenaar | bb76f24 | 2016-09-12 14:24:39 +0200 | [diff] [blame] | 1 | *if_ruby.txt* For Vim version 8.0. Last change: 2016 Sep 01 |
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 | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 14 | 6. Dynamic loading |ruby-dynamic| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
| 16 | {Vi does not have any of these commands} |
| 17 | *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273* |
| 18 | |
| 19 | The Ruby interface only works when Vim was compiled with the |+ruby| feature. |
| 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 | |
| 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 Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 37 | like for the |:append| and |:insert| commands. This |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | 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 | |
| 44 | Example Vim script: > |
| 45 | |
| 46 | function! RedGem() |
| 47 | ruby << EOF |
| 48 | class Garnet |
| 49 | def initialize(s) |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 50 | @buffer = Vim::Buffer.current |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | 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 Moolenaar | abd468e | 2016-09-08 22:22:43 +0200 | [diff] [blame] | 61 | To see what version of Ruby you have: > |
| 62 | :ruby print RUBY_VERSION |
| 63 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | |
| 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 Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 68 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | 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 |
| 75 | ":ruby load 'file'", but allows file name completion. |
| 76 | |
| 77 | Executing Ruby commands is not possible in the |sandbox|. |
| 78 | |
| 79 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 80 | 2. The Vim module *ruby-vim* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 82 | 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] | 83 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 84 | Overview: > |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 85 | print "Hello" # displays a message |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 86 | 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 Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 93 | 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 Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 102 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 105 | < |
| 106 | |
| 107 | Module Functions: |
| 108 | |
| 109 | *ruby-message* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 110 | Vim::message({msg}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | Displays the message {msg}. |
| 112 | |
| 113 | *ruby-set_option* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 114 | Vim::set_option({arg}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | 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 Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 120 | Vim::command({cmd}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | Executes Ex command {cmd}. |
| 122 | |
| 123 | *ruby-evaluate* |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 124 | Vim::evaluate({expr}) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | Evaluates {expr} using the vim internal expression evaluator (see |
Bram Moolenaar | 2b8388b | 2015-02-28 13:11:45 +0100 | [diff] [blame] | 126 | |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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | |
| 134 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 135 | 3. Vim::Buffer objects *ruby-buffer* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 137 | Vim::Buffer objects represent vim buffers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | |
| 139 | Class Methods: |
| 140 | |
| 141 | current Returns the current buffer object. |
| 142 | count Returns the number of buffers. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 143 | 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] | 144 | is 0. |
| 145 | |
| 146 | Methods: |
| 147 | |
| 148 | name Returns the name of the buffer. |
| 149 | number Returns the number of the buffer. |
| 150 | count Returns the number of lines. |
| 151 | length Returns the number of lines. |
| 152 | self[{n}] Returns a line from the buffer. {n} is the line number. |
| 153 | self[{n}] = {str} |
| 154 | Sets a line in the buffer. {n} is the line number. |
| 155 | delete({n}) Deletes a line from the buffer. {n} is the line number. |
| 156 | append({n}, {str}) |
| 157 | Appends a line after the line {n}. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 158 | line Returns the current line of the buffer if the buffer is |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 159 | active. |
| 160 | line = {str} Sets the current line of the buffer if the buffer is active. |
| 161 | line_number Returns the number of the current line if the buffer is |
| 162 | active. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | |
| 164 | ============================================================================== |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 165 | 4. Vim::Window objects *ruby-window* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | |
Bram Moolenaar | 2c5e8e8 | 2015-12-05 20:59:21 +0100 | [diff] [blame] | 167 | Vim::Window objects represent vim windows. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | |
| 169 | Class Methods: |
| 170 | |
| 171 | current Returns the current window object. |
| 172 | count Returns the number of windows. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 173 | 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] | 174 | is 0. |
| 175 | |
| 176 | Methods: |
| 177 | |
| 178 | buffer Returns the buffer displayed in the window. |
| 179 | height Returns the height of the window. |
| 180 | height = {n} Sets the window height to {n}. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 181 | width Returns the width of the window. |
| 182 | width = {n} Sets the window width to {n}. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | cursor Returns a [row, col] array for the cursor position. |
| 184 | cursor = [{row}, {col}] |
| 185 | Sets the cursor position to {row} and {col}. |
| 186 | |
| 187 | ============================================================================== |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 188 | 5. Global variables *ruby-globals* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | |
| 190 | There are two global variables. |
| 191 | |
| 192 | $curwin The current window object. |
| 193 | $curbuf The current buffer object. |
| 194 | |
| 195 | ============================================================================== |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 196 | 6. Dynamic loading *ruby-dynamic* |
| 197 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 198 | On MS-Windows and Unix the Ruby library can be loaded dynamically. The |
| 199 | |:version| output then includes |+ruby/dyn|. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 200 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 201 | This means that Vim will search for the Ruby DLL file or shared library only |
| 202 | when needed. When you don't use the Ruby interface you don't need it, thus |
| 203 | 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] | 204 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 205 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 206 | MS-Windows ~ |
| 207 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 208 | You need to install the right version of Ruby for this to work. You can find |
| 209 | the package to download from: |
| 210 | http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html |
Bram Moolenaar | 64d8e25 | 2016-09-06 22:12:34 +0200 | [diff] [blame] | 211 | Currently that is ruby-1.9.2-p136-i386-mswin32.zip |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 212 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 213 | 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] | 214 | console window type "path" to see what directories are used. The 'rubydll' |
| 215 | option can be also used to specify the Ruby DLL. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 216 | |
| 217 | The name of the DLL must match the Ruby version Vim was compiled with. |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 218 | Currently the name is "msvcrt-ruby191.dll". That is for Ruby 1.9.1. To know |
| 219 | for sure edit "gvim.exe" and search for "ruby\d*.dll\c". |
| 220 | |
| 221 | If you want to build Vim with Ruby 1.9.1, you need to edit the config.h file |
| 222 | and comment-out the check for _MSC_VER. |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 223 | You may also need to rename the include directory name to match the version, |
| 224 | strangely for Ruby 1.9.3 the directory is called 1.9.1. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 225 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 226 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 227 | Unix ~ |
| 228 | |
| 229 | The 'rubydll' option can be used to specify the Ruby shared library file |
| 230 | instead of DYNAMIC_RUBY_DLL file what was specified at compile time. The |
| 231 | version of the shared library must match the Ruby version Vim was compiled |
| 232 | with. |
| 233 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 234 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | vim:tw=78:ts=8:ft=help:norl: |