mkaz.com home photography web dev personal about

VIM Guide

Marcus Kazmierczak
Created On: August 28, 2002
Last Updated: July 26, 2005
Introduction

This VIM Guide document is a quick reference for using the Vim text editor, an improved version of vi, common on most platforms. Includes the basics on how to do many common tasks and some tips and tricks that hopefully are useful time savers.

Why vi? Why not emacs? Who cares? Try them both see what your personal preference is. I like vi because it is installed on every Unix by default. It is small, fast and starts quickly. Your mileage may vary.

Basics

Modes Explained

There are a three modes in vim: NORMAL, INSERT and VISUAL.

  • Normal Mode is for issuing most of the commands, searching and moving around. Use ESC to go to Normal mode.
  • Insert Mode is to insert/edit text. This is the mode most people think of as editor mode. You type, it shows up. Use i, a and o as a few ways to enter Insert mode. Try them out to see the difference.
  • Visual Mode is for marking text, especially useful for copying and pasting. Use v to enter visual mode.

The bottom left corner of your editor will tell you what mode your in, it will be blank for NORMAL mode and will say INSERT and VISUAL in those modes.


Undo. Undo. Undo.

The single most important key to remember is u which is undo in NORMAL mode. Vi has an unlimited amount of undo back to your last save. Just hit it u over and over again.

If you hit undo too many times, you can use ^R for redo.


Exiting and Saving

In Normal mode, typing : (that's a colon) gets you to the vi command-line. Your cursor will be placed at the bottom of the screen after a colon. The following commands are used on the command-line to save the file and quit the program.
w Write (Save)
q Quit (Close)
wqWrite and Quit. (Save and Close)
q! Force Quit (Close without saving changes)

NOTE: Hit ESC several times to exit command-line mode without issuing a command.


Moving Around

There are many many ways of moving around in VIM. Besides the standard arrow keys the following are very useful (NORMAL MODE):

$Move to End of line
^Move to Beginning of line
 
:0Move to Beginning of File (Line 0)
:NNNMove to Line NNN
:$Move to End of File (Last Line)
 
fAJump to A character in line
^BScroll Backwards a Page
^FScroll Forwards a Page

Editing Multiple File

You can open multiple files on the command-line to edit at once.
  vim file1 file2

To move between the files use:

:nextNext File
:prevPrevious File


Delete, Copy and Paste

Use x to delete a single character.

In vi, d is cut (or delete), y is copy (stands for yank) and p is paste. Keep these generalizations in mind. Using just the single key, d or y, will simply cut or copy a single character, the one beneath your cursor. The following is a little more useful:

ddDelete a complete line (NORMAL MODE)
yyCopy a complete line (NORMAL MODE)

Use p to paste the cut or copy lines.


You can also use VISUAL mode to copy and paste.
To use visual mode, type v to enter visual mode. You can then use the arrows to highlight the text you want. Then use d or y to cut or copy the highlighted text.


Time Savers

Use the Numbers

The number keys are extremely helpful when combined with almost any command. The number keys are used to perform the same operation numerous times. For example if you want to delete the next five lines:

5dd

Indenting a Block of Code

To indent or unindent a line of code based upon your tab size use the >> and << keys. Combine with the numbers keys to indent blocks of code.


Modifying Text

guuLower-case complete line
gUUUpper-case complete line
My .vimrc File

Custom configurations are stored in the file ".vimrc" in your home directory.
The following is my Vim configuration file:

" MacOS X .vimrc

" Marcus Kazmierczak, marcus@mkaz.com
" Created On: March 24th, 2001
" Last Modified: August 29th, 2002

" mouse support
set mouse=a

set ruler
set laststatus=2
set backspace=2
set noerrorbells nowrap nocompatible

" all tab stuff to 4
set tabstop=4
set softtabstop=4
set shiftwidth=4
set cinoptions=>4
set expandtab

"set autoindent
"set smartindent

syntax on
colorscheme karkle 

if has("gui_running")
    "set gfn=Monaco:h14
    set gfn=Courier:h14
    colorscheme zellner
endif
    

Other Resources

Vim Home page
Vim Home page, everything you could want about Vim, the vi iMproved editor. Downloads, Documentation, News, Community, etc... Be sure to check out the Tip archive for helpful hints.


Learning Vi (BOOK) Learning the vi Editor
Quickly learn the basics of editing, cursor movement, and global search and replacement. Then take advantage of the more subtle power of vi. Extend your editing skills by learning to use ex, a powerful line editor, from within vi. For easy reference, this new edition also includes a command summary at the end of each appropriate chapter. Buy Now at Amazon.com


Vi Quick Reference Guide (Quick Reference Guide) Vi Editor Pocket Reference
A companion volume to the newly updated sixth edition of "Learning the vi Editor", this "Pocket Reference" presents movement and editing commands, the command-line options, and other elements of the "vi" editor in an easy-to-use tabular format. Buy Now at Amazon.com