<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: vim code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 06:35:41 GMT</pubDate>
    <description>DZone Snippets: vim code</description>
    <item>
      <title>Rails and Vim</title>
      <link>http://snippets.dzone.com/posts/show/2338</link>
      <description>A simple command for opening related Rails project files in Vim:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;if ARGV.empty?&lt;br /&gt;  puts "usage: #{File.basename($0)} string" &lt;br /&gt;  puts "  Scans related Rails directories for " + &lt;br /&gt;         "files begining with string " &lt;br /&gt;  puts "  and opens them in vi." &lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;files = []&lt;br /&gt;ignore = [/CVS$/]&lt;br /&gt;&lt;br /&gt;# Find models or controllers that match args&lt;br /&gt;ARGV.each do |arg|&lt;br /&gt;  models = Dir["app/models/#{arg}*"]&lt;br /&gt;  controllers = Dir["app/controllers/#{arg}*"]&lt;br /&gt;  files += models + controllers&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Remove duplicates&lt;br /&gt;files.sort!.uniq!&lt;br /&gt;&lt;br /&gt;# Add unit tests for models&lt;br /&gt;files.grep(%r{app/models/(.*?).rb}) do&lt;br /&gt;  tests = Dir["test/unit/#{$1}_test.rb"]&lt;br /&gt;  files += tests&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Add views and functional tests for controllers&lt;br /&gt;files.grep(%r{app/controllers/(.*?)_controller.rb}) do&lt;br /&gt;  views = Dir["app/views/#{$1}/*"]&lt;br /&gt;  tests = Dir["test/functional/#{$1}_controller_test.rb"]&lt;br /&gt;  files += views + tests&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Add views and fixtures for mailers&lt;br /&gt;files.grep(%r{app/models/(.*?_mailer).rb}) do&lt;br /&gt;  views = Dir["app/views/#{$1}/*"]&lt;br /&gt;  fixtures = Dir["test/fixtures/#{$1}/*"]&lt;br /&gt;  files += views + fixtures&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Again remove duplicates&lt;br /&gt;files.sort!.uniq!&lt;br /&gt;&lt;br /&gt;# Remove files that match ignore list&lt;br /&gt;files.delete_if do |filename|&lt;br /&gt;  result = false&lt;br /&gt;  ignore.each do |i|&lt;br /&gt;    if filename =~ i&lt;br /&gt;      result = true&lt;br /&gt;      break&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  result&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;system "vi -o #{files.join(' ')}" &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Drop it in a file called "edit" in your path and you can open related rails files with a few key strokes:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# open AccountController and related tests and views:&lt;br /&gt;edit account_c&lt;br /&gt;&lt;br /&gt;# open AccountMailer and related tests, views, and fixtures:&lt;br /&gt;edit account_m&lt;br /&gt;&lt;br /&gt;# open everything related to accounts:&lt;br /&gt;edit account&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;A detailed explanation here: &lt;a href="http://wiseheartdesign.com/articles/2006/07/27/rails-and-vim/"&gt;http://wiseheartdesign.com/articles/2006/07/27/rails-and-vim/&lt;/a&gt;.&lt;br /&gt;</description>
      <pubDate>Sat, 29 Jul 2006 03:00:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2338</guid>
      <author>jlong (John W. Long)</author>
    </item>
  </channel>
</rss>
