开发者

VIM: open ruby/rails file containing the function currently under the cursor

I've started programming Ruby on Rails through VIM. I know this can be don't but I don't know how: How do I open up a new instance of vim to the file that contains the {function|class|constant|whatev开发者_开发问答er} under the cursor? What do I install? How do I use it?


You can use Exuberant Ctags (available as ctags on many systems) to index Ruby classes, methods, modules, and singleton methods (check ctags --list-kinds=ruby). The tag functionality built into Vim can use the generated tags file to jump to the indexed identifiers. The tags functions do not do much source inspection when they lookup tags, so you may need to use the alternate forms (e.g. g Control-], g left click) when you are dealing with a method name that is defined in multiple places. You will need to (re)build the tags file every once in a while (ctags --recurse is usually sufficient) to make sure that new identifiers are indexed (and old identifiers are dropped).

For Rails, there is also rails.vim. It makes some nice enhancements to the gf command (“goto file”), and has some of its own commands (:A… and :R…) that make it fairly easy to navigate Rails projects. Unlike tag navigation with a ctags index, the navigation commands are mostly restricted to files/classes (no “bare” methods; there are some exceptions though: you can extend some jumps with a method name (no :find activate, but you can :Rmodel User#activate), and—based on Rails naming conventions—you can jump from a view to a controller method). The functionality from rails.vim does not require any kind of index, so it is always “up to date”.


I don't think Vim will open the containing file based on a function; I've been using it a long time and don't remember seeing that. That's more like what an IDE would do, and Vim isn't an IDE, it's a very powerful text editor.

Vim can open a file if the path to the file is under the cursor. See Vim's built in help for :h ^Wgf or :h find.

It can also search all the files based on a string, and show you where the string is found and open that file. See the help for :h :vim. That would get you part of the way to what you want.

If the cursor is over a function name, you could use :vim ^rw *. That will copy the word from under the cursor to the command-line so you don't have to type it all in. Vim would search to find all matching files, then set the error list allowing you to move through it, opening the files that matched.

You might want to look at the taglist plugin for Vim as it will provide you a list of all functions and labels in the open files, making it easy to jump around. It requires ctags, but that's usually an easy install.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜