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.
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>
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
map <silent> <C-h> ^cw
<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
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.
One of the simple things from cucumber.vim that makes me happy is
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
<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>-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.
Vim can handle multiple files admirably using buffers, splits, and tabs.
Every file you open using
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
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
Go to the prior buffer with
:buffers this file has a ‘
in front of it.
: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
:vsplit. Rails.vim gives you extra love here: all those
:Rview, etc. commands can instead be
:RVview (S =
:split, V =
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
^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.