Find rails partial references in vim

Every so often when I’m about to change a partial I need to know how this change may affect other parts of the application by seeing where and how the partial is called and used. I’ve written a Vim command to do this (put in your .vimrc):

function! GrepPartial(partial)
  if (a:partial)
    let pattern = "partial.*" . partial
    let path = substitute(expand("%:h"), "app/views/", "", "")
    let action = substitute(expand("%:t:r:r"), "^_", "", "")
    let pattern = "partial.*" . path . "/" . action

  if (exists('g:loaded_fugitive'))
    execute "Ggrep -P " . '"' . pattern . '\b" app/views'
  elseif (exists('g:ackprg'))
    execute "Ack " . '"' . pattern . '\b" app/views'
    " seriously? use git grep or ack!
    execute "vimgrep " . '"' . pattern . '\>" app/views/*/**'
command! -nargs=? GrepPartial
      \ call GrepPartial(<q-args>)

Now while editing a partial just use :GrepPartial.

You can also pass in a partial:

:GrepPartial accounts/show

Note that if you have Fugitive installed then you’ll want to compile git with the PCRE library, easy for Homebrew users:

brew install git --with-pcre

If you don’t install PCRE then you’ll need to get rid of the \b, though your results may be less accurate.


Right now the function is good-enough for how I use it, though at some point I may automatically scope partial references when given a non-scoped partial pattern.

Follow me on Twitter to keep up with what I’ve learned building my personal finance tool, Stash.

Read more posts …
Written: February 23rd, 2012
Categories: rails, grep, vim