GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS

Written by thoughtbot

Viiiiiiiiiiiiiiiiiim

In light of our recently announced Vim Trail, here are more vim tips from those in the trenches all day! For prior tips from us see part one and part two.

Viiiiiiiiiiiiiiiiiim

From Josh:

How does vim help me kick ass at life? It's simple. I type less.

Vim allows you to map key combinations together to do wonderful things.

Do you absolutely hate trailing whitespace or tabs in your files?

nnoremap <silent> <F5> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR>:retab<CR>

Hitting F5 will clean out all that junk for you.

A lot of times, I'll be working on feature files and want to change the first word in a line (change a Given to a When, or whatever).

map <silent> <C-h> ^cw

Hitting <Control>+h will move the cursor to the beginning of the text on that line and change the first word.

Here's a gem I stole from my coworker Joe:

vmap D y'>p

When you're in visual mode (that is, you have text selected), hitting D will duplicate whatever's selected directly below. This really comes in handy when you're writing tests.

I use rails.vim, but there are a couple of other commands I wish it contained. Here are two that I added:

" Edit routes
command! Rroutes :Redit config/routes.rb
command! RTroutes :RTedit config/routes.rb

" Edit factories
command! Rfactories :Redit spec/factories.rb
command! RTfactories :RTedit spec/factories.rb

I open my routes and factories files a fair amount during the day - who wants to write out that path (even tab-completion is slow, in my opinion.) Write a command, set it and forget it!

The trick to writing code faster is realizing what you do over and over… and making it a couple of keystrokes away.

From Jon:

One of the simple things from cucumber.vim that makes me happy is <Control>-]. Usually reserved for jumping to a tag, in a cucumber feature it jumps to the step definition! No more trying to remember where each step is defined. Just hit <Control>-] and you're there.

Getting back to the cucumber feature is different but similar. vim has the concept of the "alternate" file, which is what you were just editing. So when you switch to the step definitions, your feature becomes the alternate file. You can switch back to it with <Control>-^ (or <Control>-6). You can do this as much as you want and you'll keep swapping between the feature and the step definition you'd just opened. Also handy for a class and its tests.

From Mike:

Vim can handle multiple files admirably using buffers, splits, and tabs.

Every file you open using :e or :tabe (or :E or gf or any other way of opening a new file) is added to your buffer list. See your buffer list with :buffers. You can switch to any buffer from that list with :buffer by specifying either the number or the file name (the file name will tab-complete). Close the current buffer and remove it from the list with :bd.

Go to the prior buffer with <Control>-^. In :buffers this file has a '#' in front of it.

For example:

:e ~/.vimrc      " edit the file ~/.vimrc
:e ~/.zshrc      " edit the file ~/.zshrc
<Control>-6      " switch back to ~/.vimrc
:buffers         " view all open buffers
:buffer .z  " switch to the ~/.zshrc buffer
:bd              " close the ~/.zshrc buffer and remove it from the list

Well what if we want both buffers visible at once? For this we want a split!

Horizontally split your screen using :split (or just :sp); vertically split it using :vsplit. Rails.vim gives you extra love here: all those :Rcontroller, :Rview, etc. commands can instead be :RScontroller or :RVview (S = :split, V = :vsplit).

Jump between splits using ^w followed by a motion. ^wj will jump to the split below the current one; ^wl to the split to the right; and so on. ^w^w will jump to the prior split (hold down ^w to bounce between things). Move them with ^wJ, ^wL, and so on.

I have much more to say about tabs but I'm running low on space so here's a fun prank:

noremap <Up> <Down>
noremap <Down> <Up>
noremap <Left> <Right>
noremap <Right> <Left>

Learn vim at your own pace with our free Vim trail.