Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1 | *if_ruby.txt* For Vim version 7.0aa. Last change: 2005 Mar 29 |
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| |
| 10 | 2. The VIM module |ruby-vim| |
| 11 | 3. VIM::Buffer objects |ruby-buffer| |
| 12 | 4. VIM::Window objects |ruby-window| |
| 13 | 5. Global variables |ruby-globals| |
| 14 | |
| 15 | {Vi does not have any of these commands} |
| 16 | *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273* |
| 17 | |
| 18 | The Ruby interface only works when Vim was compiled with the |+ruby| feature. |
| 19 | |
| 20 | The home page for ruby is http://www.ruby-lang.org/. You can find links for |
| 21 | downloading Ruby there. |
| 22 | |
| 23 | ============================================================================== |
| 24 | 1. 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 Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 35 | like for the |:append| and |:insert| commands. This |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | 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 | |
| 42 | Example 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 Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 63 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | 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 | |
| 72 | Executing Ruby commands is not possible in the |sandbox|. |
| 73 | |
| 74 | ============================================================================== |
| 75 | 2. The VIM module *ruby-vim* |
| 76 | |
| 77 | Ruby code gets all of its access to vim via the "VIM" module. |
| 78 | |
| 79 | Overview > |
| 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 | |
| 99 | Module Functions: |
| 100 | |
| 101 | *ruby-message* |
| 102 | VIM::message({msg}) |
| 103 | Displays the message {msg}. |
| 104 | |
| 105 | *ruby-set_option* |
| 106 | VIM::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* |
| 112 | VIM::command({cmd}) |
| 113 | Executes Ex command {cmd}. |
| 114 | |
| 115 | *ruby-evaluate* |
| 116 | VIM::evaluate({expr}) |
| 117 | Evaluates {expr} using the vim internal expression evaluator (see |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 118 | |expression|). Returns the expression result as a string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | |
| 120 | ============================================================================== |
| 121 | 3. VIM::Buffer objects *ruby-buffer* |
| 122 | |
| 123 | VIM::Buffer objects represent vim buffers. |
| 124 | |
| 125 | Class Methods: |
| 126 | |
| 127 | current Returns the current buffer object. |
| 128 | count Returns the number of buffers. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 129 | 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] | 130 | is 0. |
| 131 | |
| 132 | Methods: |
| 133 | |
| 134 | name Returns the name of the buffer. |
| 135 | number Returns the number of the buffer. |
| 136 | count Returns the number of lines. |
| 137 | length Returns the number of lines. |
| 138 | self[{n}] Returns a line from the buffer. {n} is the line number. |
| 139 | self[{n}] = {str} |
| 140 | Sets a line in the buffer. {n} is the line number. |
| 141 | delete({n}) Deletes a line from the buffer. {n} is the line number. |
| 142 | append({n}, {str}) |
| 143 | Appends a line after the line {n}. |
| 144 | |
| 145 | ============================================================================== |
| 146 | 4. VIM::Window objects *ruby-window* |
| 147 | |
| 148 | VIM::Window objects represent vim windows. |
| 149 | |
| 150 | Class Methods: |
| 151 | |
| 152 | current Returns the current window object. |
| 153 | count Returns the number of windows. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 154 | 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] | 155 | is 0. |
| 156 | |
| 157 | Methods: |
| 158 | |
| 159 | buffer Returns the buffer displayed in the window. |
| 160 | height Returns the height of the window. |
| 161 | height = {n} Sets the window height to {n}. |
| 162 | cursor Returns a [row, col] array for the cursor position. |
| 163 | cursor = [{row}, {col}] |
| 164 | Sets the cursor position to {row} and {col}. |
| 165 | |
| 166 | ============================================================================== |
| 167 | 4. Global variables *ruby-globals* |
| 168 | |
| 169 | There 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: |