Is there a way to find all uses of a method in vim? I'm using vim as an IDE for Rails with rails.vim. ctags helps to jump to a method definition from usage but not the other way round AFAIK. I'd like to be able to find everywhere (controllers, views etc) that a method has been used.
There aren't any perfect solutions for this in Vim, but you can get close with cscope and grep or ack.
cscope will help you find all references to a symbol. It's made for C and C-like languages, but it does a decent job of matching symbols in Ruby code as well. It's not going to get the context right all the time.
Here's a vim cscope tutorial and a blog post about Ruby/Vim/cscope, and another blog post, both of which include additional tips about navigating Ruby/Rails code in Vim.
Using grep or ack from Vim with quickfix integration is another great way to find symbols. They've got no notion of scope/context, but often a simple search is enough. Using just the built-in :grep command, you can do:
:grep some_method app/controllers
:cwindow
And get the results of the search in the quickfix window, which will allow you to quickly navigate to the matching files and line numbers.
A much better option is the ack.vim plugin, which integrates ack with Vim and makes use of the quickfix window.
If you're not already using a plugin for navigating CTAGS, I recommend Tagbar.
I wrote a gem to do just that: https://rubygems.org/gems/starscope
It parses ruby code properly and exports to ctags and cscope file formats.
Related
I am new to ruby and rails. I am used to working in IDEs (Xcode, Visual Studio, etc.) where I can perform project-wide/workspace-wide "build/compile" operations.
Let's say that I modify a number of ruby files in my rails project. I haven't yet written tests that will exercise all of my changes.
Is there a way to ensure that all of my *.rb files compile without directly exercising them at runtime? I'd really just like to perform a "compile all of my ruby/erb files" operation so that I know that I don't currently have any syntax errors.
UPDATE
I probably should have mentioned that I've been writing code professionally for 20 years. I realize that Ruby isn't compiled like, say, C++, but that doesn't mean that its syntax can't be checked. In my case, I've decided to use ruby-lint to catch basic syntax errors without having to exercise the code at runtime.
If you're on Linux or Mac or some funny windows box with the gnu find command, you can do something like:
find /your/application_directory -name=*.rb -exec ruby -c {} \;
This will find all ruby scripts in the app directory and run ruby -c on them, which will run a syntax check and respond with Syntax OK or an error message if there is an error in the script.
You can also create a macro of this to your editor, so that when you save or press a key combination it will run ruby -c on the file and give you the results. In vim you can use this in your .vimrc :
map <Leader>c :w !ruby -c<cr>
Which maps Leader-c to write the file and check it's syntax using ruby -c.
Verifying your rake-tasks and views for syntax errors might be a little trickier, but many of the templating engines like haml have similiar -c syntax check parameter.
This a one-line answer from #Zac comment, I have included it as an answer as not all the time we do read the comments:
find . | grep ".*\.rb$" | xargs -L 1 ruby -c
Why can't you continue to use an IDE like RubyMine (or its father, IntelliJ)?
There are also plugins for text editors that will highlight syntax issues, but who knows what you're using. I use Sublime Text 2 for non-IDE development and it supports several varieties of automatic linting. As does Emacs, but I'm guessing you won't go there.
Whether you use "real" TDD or not, you need to tighten your code-writing cycles and work more incrementally so you don't have a chance to accumulate such high-level (low-level?) errors in the first place, though. That's true regardless of the language or environment.
You can even do
find . -type f -name '*.rb' -or -name '*.ru' -or -name '*.rake' > /tmp/list.txt
while read file ; do
ruby -c $file
done < /tmp/list.txt
to check your entire project
Ruby is an interpreted language. There is no compilation step. Please follow TDD and write your tests before you write your code.
UPDATE:
Since this answer was accepted, I can't delete it unless the OP accepts another answer but people landing here through Google search, please see the other answers. This was more of a tongue in cheek remark than an answer. Ruby community has wonderful tools for doing all kinds of static code analysis like Rubocop and friends. At the bare minimum, ruby -c will accomplish what the OP wanted.
I just began learning RoR using the materials from railstutorial.org. When the author uses the command line, the resulting text are always nicely organized and colored. When I do the same thing, I often get "weird" characters and unorganized text as result.
http://i.imgur.com/2Q0kzwf.jpg
Example
http://i.imgur.com/mZP4SI9.jpg
My attempt to do the same
I'm not quite sure what to do to make my command line more organized like the one shown in the tutorial. Any help would be appreciated. In case you need to know, I'm using Windows 7.
It is because by default Windows command prompt doesn't know about ANSI color Sequences. You can try https://github.com/adoxa/ansicon which is supposed to make it aware. The reason the tutorial authors look like that is they are using a *nix shell that understands the color codes (probably bash or zsh on either linux or osx if I had to guess)
As Doon pointed out, the Windows console doesn't recognize ANSI sequences. As suggested your best bet right now is to use external tools.
However, since there are some pain points using most of the external tools, Ruby 2.0 will provide support for ANSI escape codes in Ruby out-of-the-box without having to depend on external gems or tools.
As a Ruby/Rails non-pro, I often want to check out the code for a rails method to see how it's implemented...
For example, I was using "form_for", and I wanted to check out the code to see how it works. The slightly lame way I did this was to just google "rails form_for" which takes me to http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html, where I can view the source for form_for.
How do rails/ruby pro's accomplish a similar task? Is there a simple way (without using IDEs) that you can quickly dig this out? Or is it a case of over time learning where stuff is located and find/grep-ing it?
Cheers
I clone the rails repository
git clone git://github.com/rails/rails.git
after I grep on it
git grep 'form_for'
After my Vim editor help my with Ctags to navigate on source code.
If you want some example of code using you can see test unit.
I prefer http://railsapi.com/ both local and remote. Quick search, nice design and magic Show on Github link
I'm learning ruby on rails on a linux box and dusting off my VIM skills (skillz?).
When I got started on VIM way back in my c++ days, I had a friend with a great vimfiles folder that had tons of stuff to get started. Starting from scratch, vim is great, but it feels like it could be a lot better.
I currently have:
vim-ruby
buffer explorer
xml-edit (though I don't have it currently working with erb files)
I know that barely scratches the surface of what some more experienced vim/ruby devs have (including the one offs in the vim.rc file).
Is there a list somewhere (or could we create one) of a bunch of the standard vim configurations needed to make programming ruby (and rails) more fun? Is there a zip/tarball somewhere with a good base setup?
take a look at tim pope's repos on git hub. Many, many awesome vim plugins and extensions for working with ruby and rails
http://github.com/tpope
snipMate (GitHub repo) is highly recommended. It enables you to use TextMate-style snippets in Vim.
snipMate is not Ruby-specific: by default, it comes with one file containing Ruby-specific snippets. If you are going to work a lot on Ruby-based code (Rails, RSpec, Shoulda, and the like), it's probably better to use one of the available snippets' collections and customize it as you see fit rather than writing your own snippets from scratch.
There is a pretty nice setup for your VIM environment.
http://github.com/akitaonrails/vimfiles
Just follow the instructions and in a minute or two you will have everything ready for Ruby(on Rails) development.
As Jed has mentioned above - tpope plugin is a "must have" and it is part of the akita vimfiles.
I have this in my ~/.vimrc to quickly see the output of a file I'm working on:
map <Leader>r :w! <bar> !ruby %<CR>
For rails, two essential plugins are rails.vim and the NERD tree, for navigating the directory of the rails project you're working on.
I'm one of those developers who isn't using TextMate with any of his Ruby/Ruby on Rails work. My particular loyalty in this arena lies with vim. What are your favorite tips/tricks for using vim with Ruby and/or Ruby on Rails to make you as efficient as possible when working?
Most important
Get a copy of rails.vim it is awesome on millions of levels. Read the doc. There are way too many tips, :Rview customer, :RSmodel foo, :Rinvert, gf, :Rextract, :Rake and the list goes on and on. You will probably want NERDTree as well for easy navigation (which you can access using :Rtree)
Second most important
Follow tpope on twitter (the author of fugative, rails.vim, haml.vim, vividchalk theme, cucumber.vim and so on), he seems to be posting new related to Rails vim plugins quite regularly (be it syntax highlighting or git integration).
You might want to checkout my ruby/rails specific vimfiles.
Its a useful starting point and has many useful Ruby/Rails plugins bundled and configured.
The one thing that really sucks about Textmate is that it doesn't run on Linux. My vim/gvim config is the same on Mac, Windows and Linux. Same fonts, same themes, same plugins and same customizations.
I mostly use Textmate for snippets and quick evaluations for posting here.
I wrote an in depth guide on using Textmate features (especially Rails related features) in VIM. It's very relevant to this question.
http://www.jackkinsella.ie/2011/09/05/textmate-to-vim.html
I don't use vim, instead, I'm like those millions of developers using Textmate. Nevertheless, a colleague does use vim/gvim.
By looking at him work, one of the things I wish I could do in Textmate is the ease of working on multiple files at the same time. Basically, you can easily manipulate multiple windows, which is quite handy.