if_ruby.txt 8.3 KB

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