Tuesday, December 07, 2004

Basic Vim

Vim (Vi improved) is a very popular, full featured text editor for Linux/Unix systems. It is practically guaranteed to be installed on most systems. If you are using a Linux/Unix shell account, you will appreciate Vim's ability to open, edit, create, and save files. Unfortunately, Vim is not very intuitive. Most will remember the helpless feeling they had on their first encounter.

There are an astounding number of commands for Vim. Even advanced users only use a subset of the available commands. This tutorial will endeavor to teach you enough to be productive and point you in the right direction should you desire to learn more.

Vim operates from within a shell. All commands are made from the keyboard. There is no mouse support, no fancy GUI, and no hints appear on the screen.

To start Vim, simply type vi at the command line.
This will open Vim with a new document and give you some basic version information and a hint or two. As soon as you begin inserting text, the information will disappear.
A variation on this is to type vi filename which will open Vim with that file loaded.
This is where most people will get stuck. Right at the beginning with no idea of how to insert text, save a file, or even exit the program! It seems to be some sort of a cruel joke. Windows users may even resort to rebooting the system or crashing the shell. (Linux systems can run for years without rebooting, this is never a good option.)

Here are the most important commands that you need to know. Commands for saving, writing and exiting are available when you are not in insert mode and are in the form ":command". That is a colon followed by the command. To toggle from command mode to insert mode, press i to enter insert mode. Press the "Esc key" to get back to command mode..

To change to INSERT mode press i or "insert".
To change out of INSERT mode press "Esc".
To exit the program type: :q.
To exit without saving type: :q!.
To write your file type: :w.
To write a new file and name it, type: :w filename.
OK, that will get you started. You can now do the most important things that you would want to do with a text editor. You can now open, create, edit, and save files as well as start and exit the program. Whew...

Moving around within a document:

You will quickly notice that the arrow keys, and the "home" and "end" keys do just about what you would expect them to do. Some of the behavior, however, is not really what you are looking for. Notice that when you arrow up or down the cursor jumps to the carriage returns rather than the screen lines.

There are an over abundance of ways to move around a document. Most will require that you are not in insert mode. Get use to moving in and out of insert mode! Note that you can add a number before most commands to modify the command.

*Screen lines are different than lines terminated by a carriage return!
To move to the left type: h, Ctrl-H, "back space", or the left arrow key.
To move to the right type: l, "space key", or the right arrow key.
To move to the end of a line type: $ or "End".
To move to the beginning of a line type: 0 or ^.
To move to the first character in a screen line type: g0, or 0.
To move to the last character in a screen line type: g$, or $.
To move to the middle of a screen line type: gm.
To move to a character number (position) in a line type: num |. (pipe symbol)
To move to the n-th occurrence of a character from the right type: num t char.
To move to the n-th occurrence of a character from the left type: num T char.
To move to the right a number of words type: num W.
To move to the left a number of words type: num B.
To move forward a number of sentences type: num ).
To move backward a number of sentences type: num (.
To move forward a number of paragraphs type: num }.
To move backward a number of paragraphs type: num {.
There are many more... This is a good start though.


To move down a line type: j.
To move up a line type: k.
To move down a screen line type: gj, or g[down_arrow].
To move up a screen line type: gk, or g[up_arrow].
To goto a specified line type: num G.
To goto a percentage of the document type: num %.
There are many more...


To scroll the screen down type: Ctrl-E.
To scroll the screen up type: Ctrl-Y.

This is enough to get you started with Vim. This is, however, not even the tip of the iceberg. Vim includes a complete scripting language and is highly configurable. You can work with multiple files, make command line commands, incorporate other programs, and much more.

No comments:

Post a Comment