Vim - The powerful simple editor

September 01, 2005
You select any Linux/Unix OS distribution; from the spartan linux which fits on a floppy to the industry heavy weights like redhat and suse; you are guaranteed to find the vi editor. So it is really worth ones time to learn how to use this powerful but simple editor. Vim is the modern version of the vi editor. Learning to use vim (vi) contains an initial learning curve. But the power it gives the user to accomplish complex text manipulation with just a few keystrokes makes the trouble taken to learn worth it.

Here I will explain a few commonly used commands in Vim. Vim (vi) is an editor with modes of operation. There are three modes of operation in vim. They are as follows :

  1. Command mode - This is the default when you start vi. In command mode, the keys entered are treated as commands, rather than as text input. Most commands are single letters. Case (upper or lower) is significant. Some commands can be combined, or prefixed with qualifiers.
  2. Input Mode - Input mode means that what ever you type (except the Escape key) is understood by vi to be the text that you want to enter into the file, and is not a command. There are many ways of entering input mode. But only one way of exiting Input Mode - Which is by typing Escape, which puts you back into the Command mode.
  3. Last Line Mode - In this mode, you are actually entering commands to the ex editor, on which vi is built. You get into the last line mode from the command mode by typing a colon (:) character. The colon, and whatever you type after it, is displayed on the last line of the screen (which is how this mode gets its name). All characters you type after the colon, until the carriage return, are treated as ex commands.

Command Mode


Cursor Movement Commands


h - j - k - l - These commands move the cursor left - down - up - right in that order.
n| - Moves the cursor to the n'th character on the line. If the character position is invalid, vi beeps. Just "|" by itself moves the cursor to the begining of the line.
0 - Moves the cursor to the begining of the line.
^ - Moves the cursor to the first non blank character in the line.
$ - Moves the cursor to the end of the line.
fx - (where x is any character) Searches forward for the next occurence of that character from the cursor position, on the current line only.
Fx - Works the same as fx but in reverse direction.
[wW],[bB] - Move one word forward and backward respectively.
H - Moves the cursor to the top (highest) line on the screen.
M - Moves the cursor to the middle line on screen
L - Moves the cursor to the lowest line on screen
Ctrl-d - Scrolls down by half page.
Ctrl-u - Scrolls up by half page
Ctrl-f, Ctrl-b - Scroll forward and backward a page.
nG - Where "n" is a number. The cursor goes to that line number in the file. Example, 1G goes to the top of the file. 157G to the 157th line and so on.
Mx - Where "x" is any single letter, marks the current line with a marker called x. Later you can move back to that line with the command 'x. Note : To go to the exact cursor position of the mark, use ` instead of the forward single quote.
`` - (This is a single quote typed twice, not a double quote) acts as a toggle and takes you to the position you were at before. So you can repeatedly switch between two positions in the file.


Text Modification Commands


x - Deletes the character under the cursor.
rn - (where "n" is any character) replaces the character under the cursor with the character n.
cw - (change word) puts you into input mode by replacing the word that the current cursor is on.
dw - (delete word) deletes the current word you are on.
dd - (delete line) deletes the current line you are on.
yy - yanks (copies) a line into the buffer.
p - pastes the yanked line at the position of the cursor.

Input Mode


i - Which means insert text before the cursor. This command puts you in Input Mode.
I - Same as above but it starts at the begining of the line.
a - Which means apend text after the cursor. This command puts you in Input Mode.
A - Same as above but starts appending at the end of the current line.
R - Replace characters.Puts you in another kind of input mode (called replace mode) in which what ever you type overwrites whatever is under the cursor. This goes on till you type Esc key.
o - Which means open mode. Opens a new line below the current line.
O - Opens a new line above the current line.

Last Line Mode Commands


:w - Saves the file in the same name that you opened it with.
:w filename - Similar to "save as" command. In vim, you can also use the :sav filename command to accomplish the same function.
:n,m w filename - This saves the contents of line numbers n to m to the given filename. Use $ instead of m to refer to the last line number without knowing what its value is.
:e filename - This is to start editing another file instead of the current one. Once you have used the e command, you can toggle back and forth between the two files by the command :e#. If you have made a lot of changes (and not saved), and now want to discard all of them, type :e!.
:r filename - The read command, reads in the file and inserts its contents below the current line.

What I have explained above is only a tiny sample of commands which a beginner in vi (vim) can start using. You can rest assured that there are many many more . If you want to learn more advanced commands, the best way to do is to read the Vim manual which gets installed along with the editor. Just type :help in the command mode and start reading the manual.

For Further Reading

1 comments:

  • Andrew

    Another way to go from Input Mode to Command Mode is with the key combination: ctrl+[