Friday, April 5, 2013

Vim as a Ruby on Rails IDE


December 29th, 2008 By: Daniel

In the words of Tim Pope “TextMate may be the latest craze for developing Ruby on Rails applications, but Vim is forever”
Here’s how I use vim (and gnome-terminal) to program in Rails on Ubuntu.
I have a ruby script that opens two gnome-terminals each with 9 tabs each. That may seem like a lot, but each one is assigned a specific purpose and by now my fingers know just where to go depending on the task at hand. For example, models, views and controllers each get their own tab. So if I need to edit a model my fingers will do an vim model_name.rb (or :e model_name.rb if I’m already in vim). And of course bash aliases and tab_completion minimizes the actual number of keystrokes required. In the same way, I know exactly where to go to watch the log, play in the console, use the SCM or anything else.
My Vim Rails IDE
So that’s what it actually looks like. Notice how I have Gmail in the background so I can know immediately when I get a new email or IM (and ignore it at my leisure). Not shown is a separate Firefox window running on my other monitor so that I can see both the code and the running application at the same time.
Now, I have some doubts as to how well my solution would work for other people as it requires a bit of discipline to make sure everything has a place and to train yourself to always do things in the right place. It seems most people prefer to have only a few windows (or tabs or buffers or whatever) open and use some sort of fuzzy finder or file explorer to find the file they want to work on next. But that’s what I love about vim. I can come up with a custom workflow that works for me instead of conforming to the whims of some dictatorial IDE. True, the learning curve was quite steep at first, and I still occasionally invest some of my time in learning how to use additional features and customizations, but now contemplating using another text editor is like going back to riding tricycles after learning to fly jet planes.
Here’s the current version of the ruby script in case anyone’s interested. I call it zvim_rails_ide so I can use it with a quick zv<tab>.
#!/usr/bin/env ruby
 
if ARGV.size.zero?
  puts "Usage: #{File.basename($0)} name_of_project"
  exit
end
 
RAILS_ROOT = File.join(Dir.pwd, ARGV[0])
RAILS_ROOT << '/' unless RAILS_ROOT[0] =~ %r{/$}
 
unless File.exists?(RAILS_ROOT)
  puts "#{RAILS_ROOT}: No such file or directory"
  exit
end
 
ls = "ls --color"
scm = File.exists?('.git') ? 'git status' : 'svn status'
test_dir = File.exists?("#{RAILS_ROOT}spec") ? 'spec' : 'test'
 
[
  {
    :geometry => "140x38+90+270",
    :tabs => [
      {:path => "app/models", :command => ls},
      {:path => "app/views", :command => ls},
      {:path => "app/controllers", :command => ls},
      {:path => "app/helpers", :command => ls},
      {:path => "public", :command => ls},
      {:path => "vendor", :command => ls},
      {:path => test_dir, :command => ls},
      {:path => test_dir, :command => ls},
      {:path => "", :command => ls}
    ]
  },
  {
    :geometry => "141x32+185+110",
    :tabs => [
      {:path => "", :command => scm},
      {:path => "", :command => ls},
      {:path => "lib", :command => ls},
      {:path => "", :command => "ruby script/console"},
      {:path => "", :command => ls},
      {:path => "config/locales", :command => ls},
      {:path => "", :command => ls},
      {:path => "", :command => ls},
      {:path => "", :command => ls}
    ]
  }
].each do |window|
  tabs = window[:tabs].map {|tab| "--tab-with-profile=DEFAULT --working-directory='#{RAILS_ROOT}#{tab[:path]}' -e \"bash -c '#{tab[:command]}; bash'\""}.join(' ')
  system "gnome-terminal --geometry #{window[:geometry]} #{tabs}"
end

No comments:

Post a Comment