Skip to content

Instantly share code, notes, and snippets.

@rgm
Last active August 29, 2015 14:04
Show Gist options
  • Save rgm/e5d526a4fc2d53fba8cd to your computer and use it in GitHub Desktop.
Save rgm/e5d526a4fc2d53fba8cd to your computer and use it in GitHub Desktop.
Alternate file finder for Meteor
function! MeteorOpenAlternate(opener, type)
let alternate = system("meteor_find_alternate " . expand("%:p") . " " . a:type)
execute a:opener . " " . alternate
endfunction
nnoremap <leader>mU :call MeteorOpenAlternate("e", "test")<CR>
nnoremap <leader>mu :call MeteorOpenAlternate("vsplit", "test")<CR>
nnoremap <leader>mT :call MeteorOpenAlternate("e", "template")<CR>
nnoremap <leader>mt :call MeteorOpenAlternate("vsplit", "template")<CR>

Quick swapping of related buffers for Meteor, assuming you've laid the source tree out as Meteor air-quotes best practice suggests.

To install:

  1. Put meteor_find_alternate on your $PATH and chmod +x
  2. Put vim snippet in ~/.vimrc

Assuming your leader is \, then the following commands will work:

\mt -> open a vsplit to show the template js when in the template html, and vice-versa, creates if doesn't exist
\mT -> same as above, except uses the current window instead
\mu -> open a vsplit to show the unit test when in any js file, creates if doesn't exist
\mU -> same as above, except uses the current window instead

#!/usr/bin/env ruby
fn = ARGV.first
alternate_type = ARGV.last
path, base = File.split(fn)
def test_alternate(path, base)
new_path = case path
when /\/app\//
path.gsub("/app", "/test/unit")
when /\/test\/unit\//
path.gsub("/test/unit", "/app")
end
File.join(new_path, base)
end
def template_alternate(path, base)
new_base = case base
when /\.html$/
base.gsub(/\.html$/, ".js")
when /\.js$/
base.gsub(/\.js$/, ".html")
end
File.join(path, new_base)
end
case alternate_type
when 'test'
puts test_alternate(path, base)
when 'template'
puts template_alternate(path, base)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment