Barbarian Meets Coding
barbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

3 minutes readfive-minutes-vim

5 Minutes Vim: CtrlP considered harmful

5 minutes vim. Learn cool stuff about vim in under 5 minutes
In this 5 minutes of Vim goodness you learn how to find a superior mapping to ctrlP for moving within your project

In this 5 minutes of Vim goodness we’ll break the shackles of convention and become slightly more productive when using Vim. Here we go!

Of all the activities you’ll perform when coding with Vim (or any other editor), one of the most common ones is the simple act of moving between files. Since this is such a common thing we do in our daily coding using a plugin that allows us to move between files seamlessly is a must-have for all Vim users.

These plugins fall in the category of fuzzy search file finders and they let you go from a workflow that in Vim can resemble:

  • The slow typing of a file path using the :edit command
  • The slow navigating of the file tree of your project in netrw (:h netrw).

and in other editors a manual search for a file with the mouse, to something that is so straightforward and instantenous as thinking of the name of a file, typing it and jumping right into it.

In VSCode it’s called quick open and can be reached with CTRL-P or CMD-P. In other editors it’s called Go To File or something similar. In Vim there’s a slew of plugins that provide a similar (and often more powerful) functionality: fzf.vim, ctrlP and denite are some of the ones which come to my mind as I type these words.

I personally use fzf and I use it all the time. The first mapping that I added to my vim fzf configuration was this:

nnoremap <C-p> :<C-u>FZF<CR> 

I probably found it somewhere in the docs and it has served me really well over the years. I just type CTRL-P, the name of my destination appears in my head, my fingers materialize it into text, and BOOM! I’m in that file.

All seems fine but when you stop to thing about it… Why use CTRL-P at all for such a common coding task? It forces me to move both of my pinky fingers away from the home row. That doesn’t feel really great, does it? The answer is probably tradition and inertia. I can’t quite remember which was the first editor to provide this sort of functionality (textmate?), but we’ve just continued the tradition and used this key binding when there are far better choices.

So instead, in order to perform this super frequent task, we can use the premium space in our keyboard, the home row. And we can rely on the <leader> key so that jumping into fuzzy search is super smooth:

nnoremap <leader>s :<C-u>FZF<CR> 

In my vim config <leader>s is <space>s. Both of these keys are under my hand in resting position so jumping into fuzzy search mode is completely friction free.

But this is just an example. The really important thing is the principle behind it:

  • Question everything: When using plugins or copying mappings from other plugin configurations or vimrcs from other users question the mappings and commands, think about how often you use them, and adapt them before you incorporate them in your own config.
  • Put frequent tasks in the home row: Take advantage of the <leader> key in combination with the home row keys for frequent coding tasks

Happy fuzzy finding!


Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García