Tips for Ruby development in Vim

I’m huge vim fan. I’ve tried many modern editors, but, really, I wasn’t feeling at home like it was with vim. That being said, vanilla vim is nice, but for any kind of serious software development one wants to enhance it [quite] a bit.

There is one more thing I suppose worth mentioning: actually, I don’t use VIM, rather neovim, which is basically the same thing, but it works a bit faster. It’s perfectly stable and vim-compliant at the moment.

Obvious part

There is a ton of My-Vim-Setup-Like posts, so I see no point in creating another one.

This is a list of vim plugins: NOTE: dein.vim is a plugin manager for NeoVim.

call dein#add('L9')
call dein#add('WebAPI.vim')
call dein#add('Gist.vim', {'on_cmd': 'Gist'})
call dein#add('filetype.vim')
call dein#add('taskpaper.vim')
call dein#add('tpope/vim-fugitive')
call dein#add('tpope/vim-dispatch')
call dein#add('kassio/neoterm')

""  UI
call dein#add('flazz/vim-colorschemes')
call dein#add('The-NERD-tree', {'on_cmd': 'NERDTreeToggle'})
call dein#add('mru.vim')
call dein#add('xterm-color-table.vim')
call dein#add('vim-airline/vim-airline')
call dein#add('vim-airline/vim-airline-themes')
call dein#add('c0r73x/neotags.nvim')
call dein#add('Tagbar')
call dein#add('ryanoasis/vim-devicons', {'depends': ['The-NERD-tree', 'vim-airline/vim-airline', 'vim-airline/vim-airline-themes']})

call dein#add('Lokaltog/vim-easymotion')
call dein#add('surround.vim')
call dein#add('repeat.vim')
call dein#add('FuzzyFinder' )
call dein#add('unimpaired.vim')
call dein#add('delimitMate.vim')
call dein#add('Tabular')
call dein#add('')

call dein#add('mattn/emmet-vim')
call dein#add('Yggdroot/indentLine')
call dein#add('cpp.vim')
call dein#add('SirVer/ultisnips')
call dein#add('The-NERD-Commenter')
call dein#add('ruby.vim', {'on_ft': ['ruby']})
call dein#add('ruby-matchit', {'on_ft': ['ruby']})
call dein#add('tpope/vim-rake')
call dein#add('vim-coffee-script')
call dein#add('hail2u/vim-css3-syntax')
call dein#add('groenewege/vim-less')
call dein#add('Markdown', {'on_ft': ['markdown', 'gmarkdown']})
call dein#add('jtratner/vim-flavored-markdown', {'on_ft': ['markdown', 'gmarkdown']})
call dein#add('Enhanced-Javascript-syntax')
call dein#add('vim-jsbeautify')
call dein#add('rake.vim')
call dein#add('thoughtbot/vim-rspec')
call dein#add('tpope/vim-Bundler')
call dein#add('othree/html5.vim')
call dein#add('tpope/vim-haml', {'on_ft': 'haml'})
call dein#add('slim-template/vim-slim.git', {'on_ft': 'slim'})
call dein#add('benekastah/neomake')
call dein#add('tpope/vim-rails.git')
call dein#add('lucapette/vim-ruby-doc', {'on_ft': ['ruby', 'rails', 'haml', 'eruby']})

Plugins, that are essential for Ruby development are:

  • ruby.vim for basic ruby support in vim
  • vim-rake—-|
  • vim-rails—| these play well together for overall experience
  • vim-bundler-|
  • ultisnips great snippet plugin with smart filetype system and easily customizable snippets
  • vim-rspec depending on your vim-terminal(-tmux, -neoterm) setup, this may come handy
  • indentLine regardless of 2 spaces this comes as a ~~life~~ eyesaver
  • neomake integrates linters like Rubocop
  • neotags/ctags userful for every kind of language

Not-so-obvious part

Don’t use cursorline, as it causes constant redrawing, which, when combined with syntax highlighting, drops performance:

set nocursorline

Set colors for Rubocop warnings:

hi NeomakeWarningSign ctermfg=255 ctermbg=9 guifg=Red guibg=Grey
hi NeomakeErrorSign ctermfg=255 ctermbg=9 guifg=Red guibg=Grey

Setup XO1 completion.

:au FileType ruby,eruby set omnifunc=rubycomplete#Complete
:au FileType ruby,eruby let g:rubycomplete_buffer_loading = 1 
:au FileType ruby,eruby let g:rubycomplete_classes_in_global = 1
:au FileType ruby,eruby let g:rubycomplete_rails = 1

Rip tags from current directory:

:au FileType ruby nnoremap <F12> :!ripper-tags -R --exclude=vendor<CR>

If you use tmux or neoterm with vim, you’ll find stuff this useful:

:au FileType ruby nnoremap <leader><leader>r :Rake 
:au FileType ruby noremap <silent> <Leader>tf :call neoterm#test#run('file')<CR>
:au FileType ruby noremap <silent> <Leader>tc :call neoterm#test#run('current')<CR>
:au FileType ruby noremap <silent> <Leader>tr :call neoterm#test#rerun()<CR>
:au FileType ruby noremap <silent> <Leader>ro :T rake routes<CR>

Setup Rubocop2 linting:

let g:neomake_ruby_enabled_makers = ["rubocop"]
let g:neomake_coffeescript_enabled_makers = ["coffeelint"]
let g:neomake_json_enabled_makers = ["jsonlint"]
let g:ruby_doc_command='open'
:au BufWritePost * Neomake

Tune rails-vim, use projections. This awesome plugin, among other things, provides you with handy commands like :Emodel, or :Emo for models, controller helpers etc. But there are also non-default things like services, components, provides, decorators, whatever. To set things up use:

 let g:rails_projections = {
 \ "app/components/*.rb": {
 \   "command": "component",
 \   "template": ""}}

Tune ultisnips:

:au BufNewFile,BufRead *_spec.rb :UltiSnipsAddFiletypes rspec.rails.ruby
:au BufNewFile,BufRead */app/controllers/\*.rb :UltiSnipsAddFiletypes rails.ruby

  1. TLDR: omni completion — completion that takes semantics into account 

  2. Rubocop gem – A Ruby static code analyzer, based on the community Ruby style guide.