A Brief History Of Me Using Vim
Like so many others, I started using Vim to edit configuration files on servers or my local workstation. Vim usage became even more usual once I discovered Mutt, a very powerful terminal-based MUA that uses a normal text editor to compose emails.
My first Rails editor was Radrails. I had to switch to Netbeans when Radrails stopped working after an upgrade to my Fedora Linux installation. Netbeans has always been a slow-poke (it got better once I disabled the auto-complete feature) and so I wasn't all that sad when they announced Ruby on Rails support would be discontinued back in January 2011.
It was then that I turned to Vim to give it a bash as a Ruby IDE. I regret nothing.
I Was Ready To Write Vimscript!
It has always annoyed me that I couldn't1 create a missing spec file from a corresponding
model or controller:
:Vfunctionaltest would of course only complain about a nonexistent
file. But Rails.vim already knows how to derive functional test filenames from a given
controller, so it shouldn't be so difficult to just create one with, let's say, a
:Vfunctionaltest! command, right?
1 better: or didn't know how.
So I took a trip to the tpope/vim-rails repo on Github and started digging for a function definition to hook into. I have never really done Vimscript and thought of it as a nice sport, but while I was figuring out how it all works together I found out that my desired feature actually existed already. Eager to learn what else I have missed up to the recent v5.0, I quickly had a look at the :help rails file. Let me share what I have found.
Creating Models, Controller Tests and View Templates
Let's start with the feature I was looking for: Rails.vim is able to create a new unsaved buffer with some boilerplate code to get you started.
The generated code depends on whether Rails.vim detects Test::Unit or RSpec. Here's an example for the generated code in an app that uses RSpec:
Awesome! Room for improvement would be to be able to ommit the
_controller portion as that
can easily be guessed from its functionaltest command. If there's a way to do it, please tell me.
Next up: creating a model.
If you're in a controller source file, let's assume it's
you can create view templates for it:
Jump To Line
This is just a minor one, but might come in handy when you already see a line number of your failing test in the console. You can open a file and jump directly to a line number using this format:
Rails.vim's Builtin Abbreviations
I knew that
ae<TAB> in a test case expands to »assert_equal« but I wasn't aware
of the many, many shortcuts that are readily available in Rails.vim.
AC:: ActionController AD:: ActionDispatch AM:: ActionMailer AO:: ActiveModel AR:: ActiveRecord AS:: ActiveSupport AV:: ActionView bt( belongs_to co( composed_of habtm( has_and_belongs_to_many hm( has_many ho( has_one logd( logger.debug loge( logger.error logf( logger.fatal logi( logger.info logw( logger.warn va( validates_associated vb( validates_acceptance_of vc( validates_confirmation_of ve( validates_exclusion_of vf( validates_format_of vi( validates_inclusion_of vl( validates_length_of vn( validates_numericality_of vp( validates_presence_of vu( validates_uniqueness_of
A pretty exhaustive list. And guess what: you can even create more.
Auto-Completion Based On Syntax Definitions
If you found the above list may prove to be useful, buckle your seatbelts. You're used to
Ctrl-n to autocomplete with a wordlist built from all open buffers.
Ctrl-x Ctrl-u gives you a set of autocompletable words based on Rails.vim's
syntax definition file. Woot?
I think I need to find a more comfortable keyboard mapping for that, though.
Customising Rails.vim With Projections
If your app has a directory structure that expands on the standard Rails layout, Rails.vim
is at a loss determining alternate and related files. But fear not, you can train
your editor to adapt to your custom layout with a
file. Read more about that in the documentation.
Use :Rextract To Move Code Into A Module
Here's another killer: with the hail of
:Rextract (formerly only known to me to extract code from a view
template into a new partial) is even more useful.
:Rextract name from a (visual) selection also works in a model or
controller and will extract the selected code into a module.
:2,4Rextract greeter will yield the following results:
The same goes for controllers, only the module will be created in
I'm sure there are other gems to be found in the mighty-mighty Rails.vim plugin (gems… get it?). I've
:Rpreview which actually hits a running server in your development env. It
can be configured with an
OpenURL command, but that's something for another time.