A First Vim Tutorial

An Introduction to Vim.

I've started using vim again. I thought I would post some of my vim notes.

I generally use vim 50% in the terminal, and now 50% as Cream (UPDATE: After learning more Vim commands I switched from Cream to regular Gvim).

Cream is a version of gvim that makes using vim really easy. If you want an easy introduction to vim, try Cream first.

Vim has a reputation as being difficult to use. That is because when you first start it up you are presented with nothing but the following screen. It won't let you type any text, and depending on what keys you push, the editor may perform strange behavior.

vim

Basically there are two modes in Vim: Normal mode and Insert mode. It starts up in normal mode which means that you cannot type text into your text file until you enter insert mode.

Because just about every Unix-based system (including Linux and Mac OSX) has vim by default, knowing the basics of vim can be good for emergencies. On some Linux distros there is no emacs but there is vim. If your computer becomes unbootable but you can still boot into a terminal, just knowing the basics of Vim might be able to help you edit the required files to fix your system.

Also, despite their somewhat cryptic operation, editors like Vim and emacs are very powerful and very fast to use once you learn them. There are entire books written about Vim, and the links at the bottom of this page can give you an idea about how complex and powerful Vim is.

How to use Vim

To start Vim in Linux, just type vim in the terminal. You can optionally specify a file to open or create — if the file exists it will open the file, and if the file does not exist, it will be created. For example:

$ vim file.txt

Pressing either the letter i key or the INS key will put you into Insert mode where you can enter text. Push the ESC key to switch to Normal mode where you can enter Vim commands.

The following commands will be enough to start. There is also a tutorial built into vim that you can study to learn more. To start the Vim tutor, just type vimtutor in a terminal. The following summarizes most of what the vimtutor teaches as well as additional commands found in the user manual. After you go through the vimtutor, the following makes a good review that you can bookmark in case your memory of vim gets a little rusty down the road. The vimtutor also gives instructions on how to set up a configuration file that will automatically provide syntax highlighting when you start vim.

Getting help

F1 — this key will get you into the help pages of Vim from within the program, although if you are running vim inside a something like GNOME terminal, you might get the help pages for GNOME terminal when you use F1. The following help commands will always be available:

Closing Vim

Saving a file in Vim

Moving around the document

USEFUL TIP: If you want to get used to navigating through a document in vim, start playing a free game called Nethack. If you are using Ubuntu or another Debian-based version of GNU/Linux, you can install Nethack in the terminal by typing: sudo apt-get install nethack-console. Once Nethack is installed, start it by typing nethack in a terminal.

Like most Vim commands, prefix the command with a number to repeat it. So typing 3w will move the cursor forward by 3 words.

Adding text

Vim generally starts in Normal mode. To insert text into your file, press the letter i. To return to Normal mode, press the ESC key, or more conveniently Ctrl-c or Ctrl-[.

Deleting text

You can tell Vim to repeat commands by giving it a number. For example, typing 4dw will delete from the cursor to the end of the current word, plus 3 more words including the space after the word. It can also be typed with the number in the middle like this d4w and it will do the exact same thing as the previous example.

How to undo commands in Vim

This is one of the most important things to know:

"Copy / paste" in Vim, or "putting" text

When you delete text it is stored by Vim and you replace it by putting it back with the p key.

Retreiving and inserting a file into another file

You can insert another text file into the current text file.

Replacing text

Changing text

Searching for text

There are many other nice tricks you can employ. While in the search mode, try the following options:

Substitute, or find and replace text

Matching parentheses

Executing commands

You can send commands to the terminal from within Vim, for example to list the contents of a directory.

Here are a couple of examples in Linux, or in Windows under Cygwin:

You can even run external programs from within Vim by prefixing the commands with :!.

More vim Tutorials

Vim Cheatsheets