Development 2021-12-27

Ruby on Rails and Vim

Grzegorz Smajdor

Software Engineer & CTO

I've been VIMming for more than 10 years, the reason for that is I just don't like to move my hands away from the keyboard and I try to avoid mouse as much as possible. I simply consider this to be more productive and allows me to focus one one thing at a time.

In this article I will not go to the details of how to use and what VIM is, as you can read about it yourself but instead I will show you the plugins, flow that I use in my daily Ruby on Rails development. For details of my vim config checkout my repo.

So… let's begin…

FZF

.vimrc:

Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'

FZF best to describe as Fuzzy Finder — functionality that is useful to me by this plugin is to find files in project, opened buffers, buffer tags and search…

:Files

FZF Files

As you see above, we have a project files and a preview of the file. Additionally, I use it for browsing my opened buffers (:Buffers)

:Buffers

FZF Buffers

as well as list all the functions in the buffer

:BTags

FZF BTags

However, thats not the end of its powers.

Search in the project is one of the powers combined with power of rg

:Rg

FZF Ripgrep

Vim-Rails

.vimrc:

Plug 'tpope/vim-rails'

This is the mostly juicy plugin I use. Its build to focus and gets most out of the Ruby on Rails structure. Focused on keyboard and performance remove a lot of time of finding the resources that I need. It improves navigation, by simply jumping from the controller, model, view, partial, routes, migrations, unittest and more.

Simple trigger in :E<tab> then you will get a list of possible choices (Topes-Rails-Plugin)

:E

Rails E command

The :A command

Another interesting feature of the Rails.vim plugin is to switch between the production code and a test. This is simple trigger by ":A" command, but if you work with vertical splits or with horizontal splits you can just trigger **:AV** or **:AS** to do that easily.

:A :AV :AS

Rails A, AV, AS commands

:Extract partial

When working with views, you can simply move code into partials by highlighting the code and executing the command **:Extract 'patial_name'**. This will generate the file with _, move the code into it, and also replace the code to render :partial_name in the original file.

:Generators

They are super powerful, executing them by command :Generate <tab><tab> will list all possible generators

(this action is more less the same as rails generate but still inside of the vim and after generation will open the main resource (Generate controller)

:Destroy

Sometimes, we just want to destroy the resource we already have generated. To do that use Destroy 'resource' (Destroy controller)

:Server / :Console / :Logs

Any of the above commands are executing the commands async and the output will be kept in the Vim buffer. To have it work you have to install a dispatch plugin

.vimrc:

Plug "vim-dispatch"

gf

gf is a magic command

hitting gf on the Post.firstmodels/post.rb hitting gf on the has_many :commentsmodels/comment.rb

This works, for all type of relations (view -> controller, controller-> model)- best read about it here.

Vim-test

.vimrc:

Plug 'vim-test/vim-test'

As I am a huge fun of TDD, I love to quickly run the tests from VIM by hitting <CR> (which is enter) The vim-test plugin is all is needed to execute any type of a test (ruby, python..) as well as execute in scope - project, class, single method. Whats more, it will find a matching test file from a production code!

I've mapped the <CR> (which is an enterrepo) and whenever I hit enter in View mode, I will execute the tests for current file. For running single test, I use TestNearest command.

CoC.NVIM

.vimrc:

Plug 'neoclide/coc.nvim', {'branch': 'release'}

This provides much more power into completion. You can see it action here. As CoC allows to install extensions. Few that I found super useful are:

.vimrc:

let g:coc_global_extensions = ['coc-snippets', 'coc-css', 'coc-json', 'coc-yaml', 'coc-tabnine', 'coc-html']

Conclusion

To wrap it up, today, we have plenty of different tools to use that will improve our workflow, will help us to deliver higher quality of the software. Finding a perfect one is hard, but it's really important to focus on tools as we use them constantly.

Thank you,

Grzegorz