Home > Articles > Home & Office Computing > Microsoft Windows Server

  • Print
  • + Share This
From the author of

From the author of

Editing for the First Time

The next few sections show you how to edit your first file. During this process, you learn the basic commands that you have to know to use Vim:

Inserting Text

To enter text, you need to be in insert mode. Type i, and notice that the lower left of the screen changes to --INSERT-- (meaning that you are in insert mode).

Now type some text. It will be inserted into the file. Do not worry if you make mistakes; you can correct them later. Enter the following programmer's limerick:

A very intelligent turtle
Found programming UNIX a hurdle
	The system, you see,
	Ran as slow as did he,
And that's not saying much for the turtle.

After you have finished inserting, press the <Esc> key. The --INSERT-- indicator goes away and you return to command mode.

Your screen should now look something like Figure 3.

Figure 3 Screen after the text has been inserted.

Getting Out of Trouble

One of the problems for Vim novices is mode confusion, which is caused by forgetting which mode you are in or by accidentally typing a command that switches modes. To get back to normal mode, no matter what mode you are in, press the <Esc> key.

Moving Around

After you return to command mode, you can move around by using these keys: h (left), j (down), k (up), and l (right). At first, it may appear that these commands were chosen at random. After all, who ever heard of using l for right? But actually, there is a very good reason for these choices: Moving the cursor is the most common thing you do in an editor, and these keys are on the home row of your right hand. In other words, these commands are placed where you can type them the fastest.

One way to remember these commands is that h is on the left, l is on the right, j is a hook down, and k points up. Another good way to remember the commands is to copy this information on a Post-It Note and put it on the edge of your monitor until you get used to these commands.

Deleting Characters

To delete a character, move the cursor over it and type x. (This is a throwback to the old days of the typewriter, when you deleted things by typing xxxx over them.)

Move the cursor to the beginning of the first line, for example, and type xxxxxxx (eight x's) to delete the first eight characters on the line. Figure 4 shows the result.

Figure 4 Screen after delete (xxxxxxxx)

To enter a correction, type iA young <Esc>. This begins an insert (the i), inserts the words A young, and then exits insert mode (the final <Esc>). Figure 5 shows the results.

Figure 5 Result of the insert.

Undo and Redo

Suppose you delete too much. Well, you could type it in again, but an easier way exists. The u command undoes the last edit.

Take a look at this in action. Move the cursor to the A in the first line. Now type xxxxxxx to delete A young. The result is as follows:

intelligent turtle

Type u to undo the last delete. That delete removed the g, so the undo restores the character.

g intelligent turtle

The next u command restores the next-to-last character deleted:

ng intelligent turtle

The next u command gives you the u, and so on:

ung intelligent turtle
oung intelligent turtle
young intelligent turtle
young intelligent turtle
A young intelligent turtle

If you undo too many times, you can press CTRL-R (redo) to reverse the preceding command. In other words, it undoes the undo.

To see this in action, press CTRL-R twice. The character A and the space after it disappear.

young intelligent turtle

There's a special version of the undo command, the U (undo line) command. The undo line command undoes all the changes made on the last line that was edited. Typing this command twice cancels the preceding U.

A very intelligent turtle	
   xxxx                Delete very
A intelligent turtle
         xxxxxx         Delete turtle
A intelligent
                     Restore line with U
A very intelligent turtle	
A intelligent              Second U undoes the preceding U

Getting Out

To exit, use the ZZ command. This command writes the file and exits.

Unlike many other editors, Vim does not automatically make a backup file. If you type ZZ, your changes are committed and there's no turning back.

Discarding Changes

Sometimes you will make a set of changes and suddenly realize you were better off before you started. Don't worry; Vim has a "quit-and-throw-things-away" command. It is :q!.

For those of you interested in the details, the three parts of this command are the colon (:), which enters command mode; the q command, which tells the editor to quit; and the override command modifier (!). The override command modifier is needed because Vim is reluctant to throw away changes. Because this is a command mode command, you need to type <Enter> to finish it. (All command mode commands have <Enter> at the end. This is not shown in the text.)

If you were to just type :q, Vim would display an error message and refuse to exit:

No write since last change (use ! to override)
  • + Share This
  • 🔖 Save To Your Account