Until a few months ago, Vim was a program I had never considered using. I had always perceived Vim to be an editor with a steep learning curve that wasn’t worth my time as a web programmer. It was a tool, I thought, used mostly by people who spent their time writing obscure shell scripts or coding the Linux kernel in C.
As it turns out, I was wrong, and I’m glad I was.
Overcoming the Learning Curve
Now, that’s not to say that Vim’s learning curve isn’t steep — the program can be notoriously unforgiving to a new user. But once I learned a few basic commands to move around, and understood the different modes, Vim became much easier to work with.
Encouraged by one of my instructors at Metis, I spent up to an hour at the beginning of each day practicing with vimtutor. (There are also a number of web-based tutorials, and this keyboard layout cheat-sheet.)
After using vimtutor each morning, I then challenged myself to code in Vim throughout the day. At first, I worked much more slowly as I learned different commands and occasionally googled for help. After a few days, though, I could tell that I was beginning to get the hang of it. It was amazing to see the impact of using they keyboard to navigate, without switching over to the mouse every few minutes.
Hard Work’s Rewards
Having now spent a few months using Vim, I’m really beginning to appreciate its power. Using MacVim and a few extensions (for syntax highlighting, file-switching, et al.), I now have most of the key features of a text editor like Sublime and still have the additional power of Vim. Overall, my workflow is much more efficient.
Needless to say, Vim has quickly become an indispensable tool, even (especially?) for a web developer like me.
A Not-Nearly-Exhaustive List of Vim Magic
Below are just a few of the Vim shortcuts that I’ve found especially useful as I’ve been learning Rails.
Vanilla Vim Shortcuts
cis for “change”; it deletes in the given direction then enters insert mode (i.e.,
cwto change to the end of the word, or
c$to change to the end of the line)
ddeletes in the given direction (i.e.,
dwto delete to the end of the word);
Ddeletes to the end of the line
oplaces you in insert mode in a new line below the current line;
Odoes the same above the current line
:!lets you run a terminal command from within Vim, helpful for running Ruby files (like
=automatically indents lines in the given direction.
gg=Gautomatically indents the whole file.
:splitfor a horizontal window split (optionally append a filename to open)
:vsplitfor a vertical window split (optionally append a filename to open)
Ctl-Wcommands allow moving between split windows
Ctl-W Ctl-Wswitches between active split windows
Ctl-W [h,j,k,l]moves to the split window in the given direction
Vim Rails Magic
This magic comes courtesy of the
rails.vim extension, available here.
:Rinitializerjumps to the routes file
:Rmigrationjumps to the most recent migration file
:Rfrom inside a view jumps you to the corresponding action within the appropriate controller and vice-versa
:Rcontroller wombatsjumps to the “wombats” controller
:Rview wombats/index.html.erbdoes the same for the view
:Rmodel wombatdoes the same for the model
!to any of the last three to create the file with a boilerplate class definition
:efollowed by a path/filename switches to a different file
Ctl-p(via this extension) has been a huge time-saver with its fuzzy filename matching.
This is just a short list. Feel fee to tweet me your other favorites @RobertEshleman or comment below. =)