Heading image for post: Exploring Hashrocket's Vimbundle: Vim Hashrocket

Vim

Exploring Hashrocket's Vimbundle: Vim Hashrocket

Profile picture of Jake Worth

Hashrocket committed to Vim early in our history, and it's been a crucial decision. When we set up a new development machine, we install the Dotmatrix, a library of configurations that includes our favorite, must-have Vim plugins. This blog post is part of a series exploring some of those plugins, focusing on Vim Hashrocket.

Vim Hashrocket

Vim Hashrocket is a collection of settings our team shares. Spend enough time writing code with Vim on a shared machine, and you'll find that the Vim configuration file in the root directory (.vimrc) swells to hundreds of lines, as each developer adds their own favorite settings. Vim Hashrocket codifies that into a plugin. It's a neatly organized junk drawer that reflects many of our evolving goals as a development team.

Here are a few noteworthy settings in the 227-LOC hashrocket.vim plugin.

NotRocket

:NotRocket (line 36) is one of those incantations that makes Vim users feel like magicians. NotRocket substitutes pre-Ruby 1.9 hashrockets (=>) for Ruby 1.9-and-after colons (:). Both are supported in modern versions of Ruby, but many people prefer the colon, and whichever you choose, consistency is a worthy goal. :NotRocket helps us standardize legacy code.

Rails and Gem Projections

Lines 62-144 define a series of projections for use with Projectionist.vim. What are projections? These are command-mode commands like :Econtroller users, which helps you navigate to a UsersController or provides a boilerplate file if it does not exist.

We start by extending the existing projections provided in other libraries, adding a presenter projection to support the Presenter pattern. Next, we define a series of rails_gem_projections– projections that leverage Bundler.vim to add projections based on the contents of your Rails Gemfile. Using the gem Cucumber? You'll get an :Efeature command. Factory Girl? :Efactory. These commonsense mappings save time.

Mappings

hashrocket.vim continues with a series of great copy-and-paste mappings, including mappings to copy to end of line, copy to system clipboard, and copy a whole file to system clipboard.

Lines 162 and 163 add two insert-mode mappings I love: bpry and ipry, which extend to require 'pry'; binding.pry; and require IEx; IEx.pry; respectively. We use these to require and bind leading debuggers for Ruby and Elixir.

Lines 190 to 207 add the command :UnusedSteps, which identifies unused Cucumber step definitions in your integration test suite. This command is useful for cleanup on a large test-driven application.

We also turn spell checking on as a default in Git commit messages, with:

autocmd FileType gitcommit              setlocal spell

One mapping I like a lot was contributed by Josh Branchaud, on line 223:

autocmd FileType help nnoremap q :q<cr>

This lets us quit Vim help pages with q in normal mode (no semicolon), the same way you can quit other terminal programs.

Here are two more excellent mappings from lines 224 and 225:

autocmd FileType ruby nmap <buffer> <leader>bt <Plug>BlockToggle
autocmd BufRead *_spec.rb map <buffer> <leader>l <Plug>ExtractRspecLet

The first mapping gives us easy access to the features of vim-blockle, which swaps Ruby blocks written in the curly braces syntax ({}) to the do-end syntax (do end), and vice versa. When to use one or the other is mostly subjective, and with this mapping, we can rapidly compare each choice.

The second mapping calls the :ExtractRspecLet from the vim-weefactor plugin, swapping local variables defined in an RSpec examples for let blocks:

# spec/application_spec.rb

user = FactoryGirl.create(:user) # => :ExtractRspecLet
let(:user) { FactoryGirl.create(:user)

Again, this choice can be subjective, and with this mapping and the underlying library, we get to compare each option with one command.

The last mapping in the file is a blockbuster. If you're editing an SQL file in a Tmux session, run <leader>t from normal mode, and your file will be read into the session and window of your choice:

autocmd FileType sql nmap <buffer> <leader>t :<C-U>w \| call Send_to_Tmux("\\i ".expand("%")."\n")<CR>

Start a psql session, and you've got a fast way to iterate on an SQL statement from Vim. The command even writes the file for you before sending it to the window.

Conclusion

How does this apply to you? Consider writing your own Vim plugin that captures your favorite settings, install Vim Hashrocket to write code the way we do, or install the Dotmatrix for this and many other settings. Not every person on our our team has all of these plugins on their personal machines, but the shared tooling is vital when we start collaborating.

No exploration of a Hashrocket Vim plugin would be complete without thanking Tim Pope, a Rocketeer who built many of these libraries, as well as all the contributors who have made them better over the years.

If you're in Chicago, stop by Vim Chicago to talk plugins and more. Likewise, the 'Today I Learned' #vim channel contains hundreds of our favorite tips and tricks to leverage this classic editor.

Stay tuned as I continue to explore the Hashrocket Vimbundle in future posts.

Photo Credit: Barn Images, https://unsplash.com/photos/t5YUoHW6zRo. Accessed 15 April 2017.

More posts about Vim