Why you should use vim, or: my favorite vim tips

September 9, 2013

Apart from how vim helps me with my ever-present wrist strain (the subject of another post, perhaps), this post will highlight the most useful or interesting features of vim. If you are still not on one side of the Great Editor War, or if you are on the losing side (cough), this post may convince you to switch to vim.

The command line curve

vim may be the most difficult editor to learn coming from other editors, so why would I advocate that you first learn command-line vim? The answer is speed. The graphical version of vim (known as gVim), while it may come packaged with vim, supports mouse-pointing and menu-clicking. While it could be easier on the eyes and/or spirit for those coming from GUI-land, it suffers from the same syndrome as Emacs. Moving a hand from the keyboard to the mouse, moving the mouse, clicking a mouse button or two, and returning to the home row on the keyboard can eat a couple of precious seconds from your day. The movement may also distract you from your editing task.

So starting vim from the command line, even if vim is the first thing you ever type, will start increasing your time efficiency immediately. But if you choose to start vim now, you should read on first.

The tiny tutor

The creators of vim have made it simple and easy to start working with the editor by including a program called vimtutor that you can invoke. In actuality, the vimtutor program is not a separate program in any sense; it merely starts vim with a buffer full of text designed to be read by a learner. The advantage to this (apart from space efficiency: the tutor text is just a plain text file) is that a learner can save the buffer to disk to review later.

The sweet shortcuts

After you’ve finished your tour through the tutor, try experimenting with the following sequences.

If you’ve gone through all these and are ready to become a true vim-ninja, try taking on some of the Vim Golf challenges.

The cool config

I’ve reproduced my simple .vimrc file here, for anyone looking to start out with some sensible defaults. A .vimrc is pretty simple; just list your preferences, optionally annotating them with line comments that begin with double quotation mark.

syntax on           " turns on syntax highlighting
set tabstop=4       " how many columns a tab spans
set shiftwidth=4    " how many columns text is indented with >>, <<, etc.
set expandtab       " expand tab characters to spaces
set smarttab
set number          " turns on line numbers on the left margin
set ruler           " turns on ruler on bottom right of screen
set showcmd         " shows extra details about mode/command as you type
set hlsearch        " highlights all matches of search command
set magic           " use regular expressions in search patterns
set noerrorbells    " don't beep or flash when encountering an error
set novisualbell    " don't flash when encountering an error
set foldenable      " enable code folding
set fdm=manual      " set fold mode to manual
set smartindent     " turn on intelligent indentation measures
set tw=80           " set automatic line break to column 80
set so=7            " keep cursor from moving >7 lines from edge of window

" highlight characters running past column 80
highlight rightMargin ctermfg=lightred
match rightMargin /.\%>80v/

" choose colors based on dark background
colorscheme koehler
set background=dark
"set background=light

" set language for spellchecking --- disable this with set nospell
set spell spelllang=en_us
"set spell spelllang=de_utf-8

More resources

If you want to learn more tips and learn some vim zen, try Drew Neil’s book Practical Vim. I found it to be a good read after having learned most of vim.

If you are having issues, can’t rememer a vim sequence, or need some pointers about .vimrc files (as I sometimes do), the wiki is a great place to go. Not too sure about the search function, though.