Tuesday, September 25, 2012

Nifty VIM stuff

I've been building some of these techniques over time as I move from TextMate to VIM. Today, I came across the venerable "Seven habits of effective text editing" by Bram Moolenaar, who is the primary author of VIM and wrote this post, ah, 12 years ago now.

Please feel free to help yourself to my dot_vim repository on github, which has my working vimrc and plugins

Here are some of the wicked good things I've come across:

  • Windows.  One of the biggest drawbacks to TextMate was the one window thang.  
  • To create a new window in VIM do:
    • Ctrl-W S (for Split): creates a horizontal split
    • Crtl-W V (for Vertical): creates a vertical split
  • To navigate between windows, use Ctrl-W plus the navigation keys h,j,k,l
  • To close a window, use Ctrl-W C
  • Tabs.  Tabs are collections of windows.  All tabs have access to all the buffers, so you have access to any open file from any tab
  • To create a new tab do :tabnew
  • To close a tab to :tabclose
  • To move to a tab use:
    • :tabnext
    • :tabprevious
    • :tabfirst
    • :tablast
    • :tabmove
    • My .vimrc has some mappings for these
  • To reposition the buffer in the middle of the window, simply press 'zz'
  • Here's the one that got me really, really excited.  For searching, I've been using the standard '/' thing.  Well, check this out; you can simply put the cursor on the word for which you are looking and press '*'.  Yep, pretty nifty.
  • Block Comment / Uncomment.  This is the shizz:
    • To block comment, use Ctrl-V to enter Visual Block mode.  Navigate across all the lines you want to comment out, then use I  ESC to insert comment characters.  I just did this in SQL to insert "-- '
    • To uncomment a block, use Ctrl-V to enter Visual Block mode.  Navigate across all the comment characters, then use d to delete.  Voila!
  • To combine two lines: position the cursor at the end of the top line and press "J" to delete the newline
  • From Vim, you can run the current buffer with :!ruby %
  • Special for Rails: I added the command-t plugin for quickly finding your favorite file.  Sweet.