Talk notes from Vim for Ruby and Rails workshop at BostonRb March 2014 meetup.
https://gist.github.com/christoomey/9357451
- Chris Toomey
- @christoomey
- http://ctoomey.com
- github.com/christoomey
https://github.com/tpope/vim-rails
Grand daddy plugin for Rails & Vim happiness. Too much to list, but some high points:
:Rmigration
- open a migration file with partial word tab completion:AV
- One of many ways to interact with 'alternate' files, ie test vs implementation:Rextract
- extract a partial from a larger view
Tons more, read the help docs, have a ball
http://mattn.github.io/emmet-vim/
Emmet is a plugin for rapid generation of HTML sections and documents. It allows you to convert CSS like selector syntax into the equivalent HTML
This:
div.wrapper>ul.people>li.person*5
becomes this:
<div class="wrapper">
<ul class="people">
<li class="person"></li>
<li class="person"></li>
<li class="person"></li>
<li class="person"></li>
<li class="person"></li>
</ul>
</div>
Use the following mappings to convert between hash syntaxes:
" Convert 1.8 hash syntax to 1.9 syntax
nmap <leader>19 :%s/:\([^ ]*\)\(\s*\)=>/\1:/gc<cr>
nmap <leader>18 :%s/\(\w\+\):\s/:\1 => /gc<cr>
I use the following snippet to extract variables while refactoring:
" Borrowed from Gary Bernhardt's vimrc
function! ExtractVariable()
echohl String | let name = input("Variable name: ") | echohl None
if name == '' | return | endif
" Enter visual mode (input() takes us out of it)
normal! gv
" Replace selected text with the variable name
exec "normal c" . name
" Define the variable on the line above
if &ft == "vim"
exec "normal! Olet " . name . " = "
elseif &ft == 'javascript'
exec "normal! Ovar " . name . " = "
else
exec "normal! O" . name . " = "
endif
" Paste the original selected text to be the variable value
normal! $p
endfunction
vnoremap <LEADER>ev :call ExtractVariable()<cr>
Source in my dotfiles: https://github.com/christoomey/dotfiles/blob/master/vim/rcfiles/refactoring
https://github.com/kien/ctrlp.vim
CtrlP is a fuzzy file opening plugin. On its own its great, but I've
configured it with a set of focused mappings to great straight to parts of a
Rails app, ie <leader>gj
goes to app/assets/javascripts
with CtlrP
nnoremap <leader>gc :CtrlP app/controllers<cr>
nnoremap <leader>gj :CtrlP app/assets/javascripts<cr>
nnoremap <leader>gl :CtrlP lib<cr>
nnoremap <leader>gm :CtrlP app/models<cr>
nnoremap <leader>gs :CtrlP spec<cr>
nnoremap <leader>gt :CtrlP app/assets/templates<cr>
nnoremap <leader>gv :CtrlP app/views<cr>
nnoremap <leader>gw :CtrlP app/workers<cr>
nnoremap <leader>gy :CtrlP app/assets/stylesheets<cr>
nnoremap <leader>go :CtrlP config<cr>
https://github.com/thoughtbot/vim-rspec
Vim-rspec will create templated spec commands that allow you to run the spec under the cursor, or the entire file. You need to hook up the commands in some way to a system that can run the commands, but we have suggestions for that: https://github.com/thoughtbot/vim-rspec#custom-command
I use (and recommend!) my Vim-Tmux-Runner plugin for this.
https://github.com/nelstrom/vim-textobj-rubyblock
Drew Neil of Vimcasts fame gives us this tool that provides a text object representing ruby blocks. Now you can comment, delete, or otherwise act on ruby blocks with the same precision as any other Vim text object.
Although I've focused on plugins and snippets above, I think by far the most significant thing that has improved my efficiency with Vim for Ruby and Rails is fully embracing Vim and its approach to editing text.
What to look for:
- Use relative number
- Learn the text objects and motions. Deeply. They apply to most Vim commands, thus every new one is like 20 new commands! (:h motion.txt)
- Avoid visual mode, prefer motions & text objects
- Add new verbs (commentary) and new nouns (TextObj-ruby-block)