I've been using vim for the past couple of years. Vim is a cross-platform programmer's editor that is highly customizable and has a ton of well known and not-so well known features. As time goes by, I always come across new or better ways of doing things that increase productivity. What's most important is to force yourself to start using them so that they become part of your workflow, becoming muscle memory.
Here's a small collection of tips that I've picked up along the way:
Moving around files
I never bought into NERDTree or any of the other explorer plugins. I usually jump around using gf, using Rails.vim's
:R,:A, etc, or you can open up vim's built in explorer using:Ex.:Sexopens it up in a split window.Use vim marks to jump around any number of files. Here's a quick reference
gfwill open the filename under the cursor.Rails.vimextends this by being smart about view partials. You can also use<C-W>gfto open the file in a new tab.Use ctags.
C-]on any method name or attribute to jump to the declaration point (or<C-W>-]to open on a new window. Use<C-T>to come back. The tag stack is maintained. Then,:tato jump to the tag. I use F5 to rebuild my ctags database:map <silent> <F5>:!ctags -R --exclude=.svn --exclude=.git --exclude=log *<CR>Learn how to use windows and tabs: it can really improve your work flow. I usually have multiple tabs open with different areas of the project. On each tab, I may have up to four windows open. I use tabs when it's a different context, for example, a CSS and a view file on one tab, and a controller, cucumber feature and step definitions on another tab, each on its own windows. When working with many windows, it's useful to quickly resize them:
<C-W>_maximizes the viewing area of the active window (for vertical windows,<C-W>|does the same). You can come back to equally sized windows with<C-W>=. The easiest way for me to move around tabs is usinggtandgT. To move around windows, I typically use<C-W>along with one of h, j, k, l.
More efficient editing
Automate simple tasks with macros.
q{reg}<your-macro>q. Then invoke it n times withn@{reg}. Learn about recursive macros to run it until EOF.Use your registers. Yanking (and putting) and recording macros to different registers is most useful. Use
:regto view contents of your registers.Quickly change text surrounded by something using
ci. For example, to change a string in double quotes useci", or to change parenthesized parameters, useci(.When working with large number of files, you can run the same command on all buffers/windows/tabs by using
:bufdo/:windo/:tabdo. Such a time saver.Learn how to use vim's substitute command with regular expressions and the grouping operators \( and \). You can morph a file at will using the matched part of the pattern and \1, \2. Throw
iat the end of the command for interactive substitution. Here's a good vim regex guideVim's built in autocompletion rocks. Use
<C-n>or<C-p>in insert mode to activate and move through the options.When moving within a file,
f<char>moves the cursor forward until the first<char>.;repeats the action. You can move to the second occurrence of<char>by using2f<char>.F<char>does the same, but backwards.Learn the different ways to enter insert mode. The ones I use the most, other than
iareato enter insert mode at the next character (as opposed to i, which enters insert mode before the current character).Ato enter insert mode at the end of the line.Ito enter insert mode at the beginning of the line.Oto create a new line above the current line and enter insert mode.oto create a new line below the current line and enter insert mode.
Block selection is magical. Use it when you want to select a vertical block or column to yank it, delete it, etc. To enter visual block select, do
<C-q>. You can also insert arbitrary text before the block with<C-I><type your chars>Esc. For example, this is very useful for inserting the same text on a set of aligned<li>s(like adding a class="foo" attribute) or after hash rockets or equal signs on your ruby code - yet another reason to align these properly.C-eandC-y, scrolls the buffer without moving the cursor. Make it scroll three lines at a time with:nnoremap <C-e> 3<C-e> nnoremap <C-y> 3<C-y>Use
%to jump to the matching open or closing bracket/parenthesis. Use the matchit plugin to have it match more than single characters, for exampledefmatchesendin Ruby code, or<ul>matches</ul>in HTML.
Other miscellanea
For bash style command tab completion, put
set wildmode=list:longeston your vimrc. Then tab away when running commands on ex mode.View unprintable characters with
:set list. Cancel out with:set nolistAnalyzing log files with vim is also a win. For example, you may be in a rails app and used
Rails.vim's:Rlogcommand. Now you can use the global command to surface up the specific pattern you're looking for using:g/{pattern}/p, or you may remove any useless lines with:g/{useless}/d.When pasting something, vim will autoindent which is usually not what you want. Use
:set pastebefore the paste to avoid that.Use
gg=Gto apply proper indentation to a messy source file. I define 2 space "tabs" for all file types by usingset tabstop=2 shiftwidth=2 expandtab.Saving and reopening a vim session is easy using
:mksession!and:source. I have the following two maps in my vimrc to make this even easier with F2 and F3:map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2 map <F3> :source ~/vim_session <cr> " And load session with F3Ctrl-Lto redraw the screen (to fix broken syntax highlighting). The following mapping will also remove highlighting after a search:nnoremap <C-L> :nohls<CR><C-L>.About those darn backup files infecting your project: Keep them, but on a different location - you may come to a crash, and you'll need them. I created a directory called ~/.vim_backups, and vim puts them there. The relevant lines are:
silent execute '!mkdir -p ~/.vim_backups' set backupdir=~/.vim_backups// set directory=~/.vim_backups//I usually write blog posts in Vim using markdown. I use vim's integrated spell checking by setting
:set spellang=en_uson my vimrc. To invoke it, use:set spellwhich will underline misspelled words, andz=brings up suggestions for the misspelled words under the cursor.Word wrapping is sluggish by default. I added these lines to my .vimrc which do a few of things:
<leader>wtoggles between wrap and no wrap. It adds a>character to new lines, making it obvious that you're on a wrapping line. It tries to wrap on word boundaries, and finally, it remaps k and j so that you're moving up/down on visual lines, not actual lines.Use
set showmatchandset mat=5on your .vimrc to blink matching parenthesis or brackets. Unobstrusive and useful.
I am sure I'm missing many, but this is what I use most often. Got any tips worth sharing or even better, improvements to the above?
Resources
The following are books, tutorials or articles that I've found useful in learning and improving my vim workflow.
