Home

My Vim Setup for Rails Development

June 25, 2017

Vim Tmux on Rails

Vim has recently become my editor of choice for code editing, especially for Ruby. I spent half of my time in the Tmux/Vim environment, and the other half is in Xcode.

In this blog post, I am going to share my Rails development workflow, the plugins that I use, and some general useful Vim tips that hopefully, everyone can make use of.

First some key re-mapping

I find the Capslock key useless, and the Control key is kind of hard to reach. So I remap my Caplocks to Control, home row key FTW!

Remap CapsLock to Ctrl

One extra side effects, you can’t even be ANGRY with anyone anymore. Less anger, less carpal tunnel syndrome.

Fuzzy file navigation with ag and ctrlp.vim

The Silver Searcher is a code searching tool just like ack, just “magnitude faster”. And it takes your .gitignore into account. Using it as the default grepping engine of Vim make your search and file navigating operations executed a thousand time faster.

Install it using brew

brew install the_silver_searcher

Adding these configurations to your .vimrc to set ag as the default grepping tool for ctrlp.vim

" Make CTRL-P use ag for listing the files. Way faster
if executable('ag')
  let g:ctrlp_user_command = 'ag %s -l --hidden --nocolor -g ""'
  let g:ctrlp_use_caching = 0
endif

Now you can navigate files using fuzzy searching with the speed of light.

Rails Integration with vim-rails

vim-rails by the legend tpope is the go to plugin for Rails development. It provides functionalities like syntax highlighting, resources navigation, refactoring, running rake and rails commands.

A quick demo before digging in:

Resources Navigation

I mostly use fuzzy searching for navigating between files, but vim-rails does provide another nice way to work with the resources.

For instance, to open the Commit model, you’d do:

:Emodel commit

and to open its controller

:Econtroller commit

You can also do mailer, spec, helper, view, migration and some others. It’s also context-aware, so if your cursor is at the Message#show and you typed Eview, it will open views/messages/open.html.erb.

Rails Commands Integration

Doing :Server! will run a Rails server in a new window using Tmux. You can also do all of the Rails’ commands inside vim, for example:

:Rails g model User email password_digest

Rake commands work as well:

:Rake db:create db:migrate

Jump to Definition with gf

vim-rails extends the “Go To File” behavior by allowing jumping to the corresponding file under the cursor.

Concern and Partial Extraction

Using the :Rextract command for concern and partial extraction. If you’re editing a model, the result will be concern extraction. For example, while editing User model, you can go into visual mode, highlight the methods and run:

:'<,'>Rextract concern_name

Using line number also works:

:19,29Rextract concern_name

The extracted methods will be put in a new file in app/models/concerns and your model will include this file.

The same goes for partials.

HTML Editting with Emmet

Install emmet-vim with Vundle by adding this to your .vimrc

Plugin 'mattn/emmet-vim'

Doing HTML is boring, and Emmet allows me to do ultra-fast HTML typing using CSS-like abbreviation.

Typing the emmet language and then CTRL + Y + , will translate it to the HTML.

Final words

This is not the entire configurations/plugins that I use for Vim. My complete dotfiles can be found on my Github repo. I hope you found this article useful and go Vim-on.