Getting Started With Vim

I have often wished that I could talk to my text editor. "Editor, delete the text up to the next closing HTML tag. Take this list of 15 words and enclose each line in <li> tags."

I've been using Vim since last summer and I am finally able to talk to my editor. It took a while to learn Vim's language, but it began to make sense after learning enough of the commands.

This tutorial shows some of the great things that I like about using Vim.

I'm using Vim 7 on Ubuntu Dapper, and most of what I've written here should work on Vim 7 anywhere. (To my knowledge tabs will only work in Vim 7.)

I usually start Vim in one of three ways:

  1. Open a terminal (mapped to Ctrl-Alt-Shift-t in my GNOME) and type vim
  2. Press Alt-F2 in GNOME and type gvim
  3. Right click on a file in Nautilus and choose "open in vim"

Quick Tips

Cream

Cream is an easy-to-use version on Gvim. It is a good way to get started with Vim because it removes the complexities of the modal editing. You can set Cream up for expert mode which allows you to turn modal edition on and off. As you learn more commands you will probably want to switch to plain Gvim because modal editing is the main benefit of Vim.

Switching Between Vim and Gvim

When running Vim in the terminal you can type :gui to move the current file to Gvim.

The .vimrc File

The .vimrc file allows you to customize Vim. I've attached my .vimrc file which is just the default .vimrc file with a few modifications. I had a more extensive .vimrc file which disappeared on my last OS re-installation.

This is the section that I added to the default .vimrc file:

" ADDED BY ME
set softtabstop=4
set shiftwidth=4
set tabstop=4
" Use spaces instead of tabs
set expandtab
colorscheme desert
set guifont=monospace\ 13
set number
set ignorecase
set vb " turns off visual bell

Editing Markup With surround.vim

If you write any kind of markup (like XML or HTML) then you should try tpope's excellent surround.vim plugin. Surround.vim lets you highlight a section of text and automatically enclose it with markup tags.

For example, you could highlight the current line with a capital V, then type s<li> and the line will automatically be enclosed in <li></li> tags. It will even accept HTML attributes. So you can highlight a section of text and type s<a href="http://www.example.com"> and surround.vim will properly enclose the text with the link code.

You can repeat the surround.vim commands by recording a macro. The letter q starts and stops the macro recording. To enclose many sections of text in <strong> tags, you could highlight the first section of text with something like ve (visual/highlight until the end of the word), then type qss<strong> followed by the enter key. Then q again to stop the recording. The macro will be stored in the s key (the first key that you pushed after starting the macro). To repeat the recorded action, highlight the next set of text and press @s to surround it with the <strong> tags.

Talking to Vim

A first step in learning Vim should be the Vim tutor. Just type vimtutor in a terminal to begin. If running Gvim on Windows, I believe there is an option to start vimtutor from the Start Menu.

I have a Vim tutorial that lists many of the commands found in the vimtutor.

The Vim tutorial on Gentoo.org is great.

The article Efficient Editing With vim is highly recommended because it groups common commands in a logical way.

Consider pasting a Vim cheatsheet like this, this, and/or this next to your computer. Post a blank sheet next to the Vim cheatsheet(s) to take notes on commands that you would like to remember.

Programming Tools

Syntax Highlighting

If you have started a new file in Gvim and tried to save it, the syntax highlighting might not turn on automatically. Just use :e and the syntax highlighting will be applied.

If you haven't saved the file yet, but would like syntax highlighting, use :setf [filetype] to tell Vim what kind of file it is. An example usage would be :setf python or :setf html.

Line Numbers

I set my .vimrc file to always have line numbering on. Sometimes I have to turn it off, for example when copying/pasting multiple lines or exporting code as HTML. To turn off line numbering use :set nonumber. To turn it back on use :set number.

Converting Code to HTML

You can export your file as HTML with :TOhtml. I haven't figured out how to export clean HTML though. The :TOhtml command exports deprecated HTML filled with font tags. If someone knows a way to get Vim to export better HTML, please let me know.

Vim Tabs

Vim 7 has tabs. You can open a new tab with the command :tabnew or :tabnew [filename].

To switch between tabs you can use gt or if in Gvim you can click on a tab to switch to it.

The images below show tabs in both Vim and Gvim.

Tabs in Vim 7

Tabs in Vim (GNOME terminal)

Tabs in Gvim 7

Tabs in Gvim

HTML and XML in Vim

Install the excellent XML Edit for extra features when editing XML, HTML, SGML documents with Vim.

Ruby on Rails and Vim

If you use Ruby on Rails, check out rails.vim — another excellent Vim plugin by tpope. It contains many of the features of Textmate while still allowing you to use your favorite operating system. I previously covered this plugin in an earlier post about editors for Rails.

See also the excellent article Vim for Textmate fans...

Other Vim Plugins

I'm experimenting with other Vim plugins at the moment.

TVO—The Vim Outliners allows you to create outlined documents in Vim. After you install it, type :he tvo for documentation.

Vim Taglist — View some screenshots.

More on Vim coming soon...