Using rails.vim
Installing rails.vim
Editing a Rails application is a challenge for any text editor, since Rails applications are spread across multiple files of different types and have different rules for highlighting and indenting. A good editor for Rails applications will have the ability to highlight and indent all files used by Rails, switch easily between files, search for files quickly and run scripts and rake tasks without leaving the editor.
Editing Rails Applications in Vim
Installing rails.vim is about the same as installing any other Vim script.
Jumping Between Files
Though it is possible to edit Rails applications in Vim without the help of any specialized scripts, the rails.vim script makes many tasks simpler. A good example is editing a controller file. Assuming your current directory is the application's base directory and you want to edit the Posts controller, a typical vim command might be :e app/controllers/posts_controller.rb. Though vim helps you out with tab completion, the rails.vim scripts makes it even easier.
Simply run the command :Rcontroller posts.
The rails.vim script also gives easy access to scripts, generators and rake tasks. Some commands are very difficult to run in Vim, especially script/server. Since script/server never exits until you exit the server, running !script/server will never allow you to take control of Vim until you end the script. The rails.vim script gives you an :Rserver command that solves this problem.
The biggest problem to solve when editing Rails applications is jumping between many files. Without any scripts, this is a difficult task in Vim. The following commands will help switch between files.
In addition to the commands above, rails.vim also integrates with the project.vim script. Issuing the :Rproject command while in the base directory of a Rails application will open a project window organized by models, views, controllers, etc. This can be used for graphical navigation instead of command-based navigation.
The features outlined here are only the tip of the iceberg. The remaining features can be read about in the rails.vim help file. The rails.vim help file can be accessed with the :help rails command.
The rails.vim script can interact with several other scripts. Vim scripts in general can be downloaded from the Vim scripts archive.
Another important, but often overlooked, thing setting up Vim is setting the color scheme and fonts.
Using the graphical Vim frontends instead of the console versions gives you better control over this. Choose a font that's readable, unambiguous (a capital O shouldn't be mistaken for a zero) and with which you're comfortable. Color schemes are also very subjective, but Vim comes with a variety of color schemes for experimenting. There are also other color schemes available in the Vim scripts archive.
Editing a Rails application is a challenge for any text editor, since Rails applications are spread across multiple files of different types and have different rules for highlighting and indenting. A good editor for Rails applications will have the ability to highlight and indent all files used by Rails, switch easily between files, search for files quickly and run scripts and rake tasks without leaving the editor.
Editing Rails Applications in Vim
Installing rails.vim is about the same as installing any other Vim script.
- Locate your vimfiles directory. On Windows, this is C:\Documents and Settings\your username\vimfiles or on Linux or OS X ~/.vim.
- Extract the zip file downloaded from the rails.vim download page into this directory. Be sure to preserve the directory structure.
- Start Vim in your Rails project directory.
- To load rails.vim's help files, run the command :helptags ~/.vim/doc on Linux or OS X, or :helptags ~/vimfiles/doc on Windows.
- To access the rails.vim help file, run the command :help rails.
Jumping Between Files
Though it is possible to edit Rails applications in Vim without the help of any specialized scripts, the rails.vim script makes many tasks simpler. A good example is editing a controller file. Assuming your current directory is the application's base directory and you want to edit the Posts controller, a typical vim command might be :e app/controllers/posts_controller.rb. Though vim helps you out with tab completion, the rails.vim scripts makes it even easier.
Simply run the command :Rcontroller posts.
The rails.vim script also gives easy access to scripts, generators and rake tasks. Some commands are very difficult to run in Vim, especially script/server. Since script/server never exits until you exit the server, running !script/server will never allow you to take control of Vim until you end the script. The rails.vim script gives you an :Rserver command that solves this problem.
The biggest problem to solve when editing Rails applications is jumping between many files. Without any scripts, this is a difficult task in Vim. The following commands will help switch between files.
- :Rmodel, :Rmigration, :Robserver, :Rfixtures and :Runittest
These commands will quickly switch to their respective model-related files based on any arguments given. For example, to edit the Post model, you can simply type the command :Rmodel post and rails.vim will find the file for you.
- :Rcontroller, :Rhelper, :Rview, :Rlayout, :Rfunctionaltest
These commands will switch quickly to their respective controller-related files. For example, to edit the Posts controller simply type the command :Rcontroller posts. Some commands, such as the Rview command must have both a controller name and view name. To edit the edit.html.erb template for the posts controller, type the command :Rview posts/edit. - Jumping to Other Files
The :Rfind command can be used to jump to various other files. For instance, to switch to routes.rb you can use the command :Rfind routes or to switch to database.yml you can use :Rfind database.
In addition to the commands above, rails.vim also integrates with the project.vim script. Issuing the :Rproject command while in the base directory of a Rails application will open a project window organized by models, views, controllers, etc. This can be used for graphical navigation instead of command-based navigation.
- Running Generators
The :Rgenerate command can be used to run generators. To generate a new Post model, for example, run the :Rgenerate model post. - Running the Server
The :Rserver command will run any servers configured to run. The server will run in the background, returning control of Vim to you. - Running Rake Tasks
Rake tasks can be run with the :Rake command. So, to run the db:create:all task, run the :Rake db:create:all command.
- Running Scripts
Any of the scripts in the scripts/ directory can be run with the :Rscript command. This can be used to run any scripts that don't have specific commands associated with them. If, for example, you wanted to destroy the Post model, you can run the command :Rscript destroy model post.
The features outlined here are only the tip of the iceberg. The remaining features can be read about in the rails.vim help file. The rails.vim help file can be accessed with the :help rails command.
The rails.vim script can interact with several other scripts. Vim scripts in general can be downloaded from the Vim scripts archive.
Another important, but often overlooked, thing setting up Vim is setting the color scheme and fonts.
Using the graphical Vim frontends instead of the console versions gives you better control over this. Choose a font that's readable, unambiguous (a capital O shouldn't be mistaken for a zero) and with which you're comfortable. Color schemes are also very subjective, but Vim comes with a variety of color schemes for experimenting. There are also other color schemes available in the Vim scripts archive.