Vim

Vim (Vi IMproved) is a contemporary version of the classic vi editor, with additional capabilites.

Introduction
All UNIX machines typically come with the vi text editor. However, vi lacks some more contemporary features (for example, syntax highlighting). This is where Vim comes in. If you know how to use vi, you'll find Vim to be the same except it has a number of useful features added to make editing easier. If you don't yet know how to use vi or Vim, and you didn't come from using ed, you'll find the learning curve quite steep (which means, it might take you a while to learn to achieve maximum proficiency).

Vim was originally written by (and is still primarily maintained by) Bram Moolenaar. It started as a personal desire to have a useful vi-clone for the Amiga, but eventually grew as more features were added. It now runs on a variety of platforms (including Unix work-a-likes, e.g. GNU/Linux, Mac OS X, and *BSD; Amiga; Windows; Mac "Classic"; and OS/2) and supports a variety of graphical toolkits (including GTK, QT, and Carbon) in addition to the command-line interface. VIM is also the latin word for "power" or "force".

Vim has a compatibility mode which produces an almost vi-compatible experience (the version of vi used seems to be one released with Sun OS 4.x, from Version 3.7 dated on 6/7/85). This is enabled by using the ":set compatible" ex-command or by running Vim with the -C option.

Vim modes
Vim is a modal editor. This means that how your keyboard input is interpreted depends on the mode the program is currently in. There are five modes for vim (that I know of right now).
 * 1) Command mode. This is the mode that vim usually starts in. All keystrokes are interpreted as commands. If they are valid commands, they will be obeyed. If they are invalid, you get an error bleep.
 * 2) Insert mode. All keystrokes are treated as input text. The text is inserted to the left of the current cursor position. You can enter insert mode with i or I and return to command mode with ESC.
 * 3) Replace mode. Similar to insert but new text overwrites old. You can enter replace mode with R and toggle between insert and replace with the INS key.
 * 4) Ex mode. You enter this with :. A prompt appears at the bottom of the page and you can enter variable-length commands and send them through with ENTER. The commands come from an earlier editor called Ex.
 * 5) Visual mode. This allows you to use your keyboard to select text for cutting and pasting.

A tutorial
While Vim is not as easy to learn initially as it is to learn nano, there are plenty advantages to using it. Some great features of Vim are copy, cut, and paste. Also there is code styling for programmers and there's also an undo/redo feature which is awesome.

Before I start breaking down the different modes let me say this: every time you delete or copy text in the command or visual mode, the deleted text is loaded into a special buffer. This buffer continues to hold the text until you delete more text/characters/lines in which case the buffer will be replaced with the newly deleted text. I say that because the buffer is also used for copying and pasting. So if you copy text and then delete some text, the next time you paste you'll be pasting the last text you deleted instead of what you copied.

When you first open a file with command vim myfile you start out in the command mode. If the file myfile exists then it will load myfile into the editor. If the file myfile does not exist then it will create a blank file with myfile as its save path which can be modified but myfile will not be created on your hard drive until you save the document.

Pressing the i key will put the editor into insert mode. Go ahead and put the editor into insert mode and type some random text you can play with. In fact you should type a couple of lines.

Pressing the Esc while in insert mode will take the editor out of insert mode and back into command mode. Now each letter on your keyboard has turned into a command which manipulates the document instead of keys which insert text into the document.

Pressing the v key puts the editor into visual mode. Visual mode is used to highlight text in the document so you can apply different commands just to that text. If you accidentally go into visual mode or you've highlighted text and don't want to do anything to it then press the v key to go back into command mode.

While in command mode, pressing the colon key opens the editors command line where you can type a command for the editor to manipulate the whole document and not just some text or a few lines. For example in the editors command line you can save the document, quit the document, quit without saving, undo change, and redo change to name a few. You can even combine commands to do them at the same time such as save and quit at the same time.

That's most of the explanation behind it. Now remember these three modes: command, insert, and visual. Everything is centralized around the command mode. Also remember the editor command line which is accessed when in the command mode. Now that I've explained all that here is what happens when pressing different keys in the different modes.

When in command mode press the colon to enter the editor command line. Here are different editor commands. command - explanation If you accidentally enter the editor command line pressing Esc twice will put you back into command mode.

When you're in the command mode different keys have different functions. The commands are case sensitive.

When you're in insert mode every key on the keyboard types text into the document like a normal text editor. When you wish to leave insert mode press the Esc key.

When you're in the visual mode you can highlight text and apply different commands to it. The commands are similar to command mode and are also case sensitive.

The following command opens myfile and automatically goes to line 23. vim +23 myfile

It's a bit of a task remembering all those commands. The best way to learn them is to try to use Vim regularly referring to those commands for the different modes. There's a lot more commands and tricks you can do in Vim. Those are just the basic ones.

Interactive tutorial
If you installed Vim on your GNU/Linux machine then vimtutor was also installed. vimtutor is an interactive tutorial which can be run from the terminal. vimtutor It's easy to learn that way.

~/.vimrc
To make a configuration file for Vim, create a file in your home directory called .vimrc, and fill it with stuff that you'd normally type into Vim's "ex" mode. For example, if you regularly do ":syntax on" from inside Vim, put "syntax on" (no colon or quotes) on a line by itself in your ~/.vimrc file.

Here's the contents of a useful .vimrc file: syntax on set number set expandtab set tabstop=4 set shiftwidth=4 set autoindent

Where:
 * "syntax on" turns on syntax highlighting
 * "set number" turns on line numbers
 * "set expandtab" makes Vim insert spaces (instead of tabs) whenever you hit the tab key
 * "set tabstop=4" sets tabs to equal 4 spaces
 * "set shiftwidth=4" makes it so when you use the text shifting command, it shifts over using 4-space indents.
 * "set autoindent" has Vim use "smart indenting", ie. when you are tabbed out to, say, the 8th column, and you type something then hit enter, the cursor is helpfully placed at the 8th column again for you.

Additionally you can turn any single argument command off by adding an exclamation point (!). So "set number" can be turned off by "set number!"

(You can shut off line numbering from command mode by typing :set nonumber.)

Syntax highlighting
You can turn syntax highlighting on/off with: :syntax off :syntax on

If you have syntax highlighting on, but are using a dark background and the colors don't show up well, you can use this command: :set background=dark

= External links =
 * Vim Homepage
 * #vim wiki