Rails Route Completion with fzf in Vim
My work codebase has a lot of routes. If your Rails app has been around awhile, it likely does too.
You probably know this routine:
- start to type a route and realize you don't remember it precisely
- leave your editor to run
rake routes
(maybe withgrep
) - comb through the output to find the route you wanted
- copy and paste it back into your editor
- promptly forget the route so you can do the same thing next time
I decided to fix this using fzf completion in Vim so I never have to leave the editor.
Now the routine is:
- start to type a route and realize I don't remember it precisely
- hit
<c-x><c-r>
and fuzzy-complete the route - promptly forget the route so I can do the same thing next time
Here's what this looks like.
And here's the code (using fzf#complete
from fzf.vim):
function! s:parse_route(selected)
let l:squished = substitute(join(a:selected), '^\s\+', '', '')
return split(l:squished)[0] . '_path'
endfunction
inoremap <expr> <c-x><c-r> fzf#complete({
\ 'source': 'rake routes',
\ 'reducer': '<sid>parse_route'})
The code is a bit naive but it solves the problem well enough for now.
Note: Example routes are from Thredded ("The best Rails forums engine ever").