vim 7 ruby omni completion

April 10th, 2006

I've hacked irb/completion into something resembling an omni-completion function for vim 7. It currently works for me. I looked at pycomplete.vim and ccomplete.vim to create it. There is a bit of work ahead as far as variable-type detection and require's are concerned. If you're feeling your oats and want to hack away at it, you'll find it here (rubycomplete.vim (0.7)). If you want to see the old versions check the release history. It was originally called rbcomplete.vim, but a discussion on the vim-dev mailing list led to a rename matching the filetype definitions. There are probably many improvements possible, I'll see how much I can make it do in the next few weeks. I've joined up with the vim-ruby project, so you can find the file in their cvs repository.

features:
  • completion of ruby constants/globals
  • in class completion of class members
  • class method completion
  • [attempted] detection of variable types
  • optional live completion of buffer elements (classes, modules)
  • optional automatic inclusion of requires for completion
  • optional detection of rails and completion of rails methods
  • kernel method completion
  • completion in rails views
n.b.: rubycomplete evaluates code in vim, this could have side effects. As of version 0.6 rubycomplete no longer defaults to evaluating live buffers. This functionality can be toggled back on (I suggest you do so, as it increases the usefullness of the script). As a note loading rails takes a little bit so you will need to be patient when initially completing rails classes.

usage: add something similar to the following to your vimrc and place rbcomplete in your autoload directory

autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
if you want buffer/rails/global completion you must add the following:
autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1
if you want rails support add the following in addition
autocmd FileType ruby,eruby let g:rubycomplete_rails = 1
if you want classes included in global completions add the following
autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1

open a ruby file, add some object call, say Array. and hit CTRL-X CTRL-O.

Obligatory screenshot
rbcomplete.vim screenshot

15 Responses to “vim 7 ruby omni completion”

  1. segfault
segfault Says:
    eiffel, I'm gona need a good bit more to actually be able to help you. The ruby version is only one part. The errors would be a good start. As would the os. Also check here , I'm compiling some troubleshooting tips.
  2. jxde
jxde Says:
    Thanks segfault! I tried to disable rails support and it works fine but I need rails support. Next week I'll move to rails 1.1 and then I'll check it again.
  3. segfault
segfault Says:
    jxde,
    I'll take a crack at making it rails 1.0 compatible. While I'm at it I'll also see about having it optionally connect to the db so we can complete the actual model fields.
  4. segfault
segfault Says:
    Aron,
    Thats on my list. It's a bug with ruby support in vim. If you attempt to access a vim buffer from ruby it crashes.
    --mark
  5. eiffel
eiffel Says:
    does not work with my ruby version 1.8.4, when i press Ctl + X, Ctl + O , it return many errors
  6. segfault
segfault Says:
    kybu,
    I need a little more info. What version of ruby is that compiled against. Also, are you trying to complete in a code block? Feel free to email me also.
  7. kybu
kybu Says:
    Hi,

    I tried auto complete feature in vim 7.0, but I got this error, whenever I press C-X C-O:
    Error detected while processing function rubycomplete#Complete:
    line 22:
    NoMethodError: undefined method `line_number' for 27:Fixnum

    This happens with default vim installation and also with your lates version of rubycomplete.vim.
  8. James
James Says:
    segfault

    How is your progress coming along on making vim + ruby Rails 1.0/1.1+ compatible.

    I'm very interested in your work with regards to having vim connect to the db so that it can auto complete the acutal model fields.

    James
  9. segfault
segfault Says:
    jxde,
    I assume your trying to complete while editing a file thats part of a rails project. In addition I am infering that you have rails support turned on. rubycomplete is trying to load the rails environment and the requires are failing. From the path I take it your doing this on a *nix system. Double check the rails installation, or disable rails support. Send along more info and I will try to replicate the problem on my end.
  10. happy
happy Says:
    works nicely with supertab: http://www.vim.org/scripts/script.php?script_id=182

    thanks alot
  11. Hugh
Hugh Says:
    Thank you!
  12. jxde
jxde Says:
    I tried auto complete and got this error:

    Error detected while processing function rubycomplete#Complete:
    line 22:
    MissingSourceFile: /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__': No such file to load -- console_app

    Please help me
  13. Aron
Aron Says:
    Hello,

    I also get:

    Error detected while processing function rubycomplete#Complete:
    line 22:
    NoMethodError: undefined method `line_number' for 27:Fixnum

    ...when pressing <C-X><C-O>.

    I was trying the "Verifying Installation" process at:

    http://wiki.rubygarden.org/Ruby/page/show/VimRubySupport

    ...when it happened. I.e., I was inside a method definition, after a '.'.

    This is vim 7.0 on Solaris 8 with ruby 1.8.4.

    Looks like a common-enough problem; any wisdom?
  14. Elvin Chapman Elvin Chapman Says:
    professionalize braccianite arsenicism bewitching corycavamine protochordate extricably jacami The Fraud Detectives http://www.housesofmichigan.com/
  15. bjcodcleup bjcodcleup Says:
    Hello my friend, your site is very good! http://nxptuntqtu.com

Leave a Reply