How to Edit Files with vi - dummies

How to Edit Files with vi

By Peter Pollock

Linux has a couple of very useful built-in file editors. vi is a very useful one. vi originated as a mode of a file editor called ex, which was a line editor that grew with the changes in technology until it had a visual mode that users activated with the command vi. vi then became an editor itself, and you can call it directly from the command line.

In Normal mode, a typed letter i is regarded as a command that puts vi into Insert mode. After vi is in Insert mode, though, a typed letter i (or any letter, for that matter) is not seen as a command; it is a request to make i the next letter in the file.

There are dozens of different commands you can use in Normal mode, so the following is a quick example of how to edit a file. You can edit the file, move to the offending text, go into Insert mode, delete the bad text, type the correct letters, and then save and quit the file.

Here are the steps:

1Select the file by typing vi index.php at the command line.

This allows you to choose which file you need to edit.

2Use the arrow keys to move the cursor to the part of the file you want to change.

One there, you need to enter Insert mode.

3Use the i command to enter Insert mode.

Insert mode is where anything you type becomes a part of the file.

4Use the Delete key and the letters on the keyboard to make the correction.

Once you have finished, you need to return to Normal mode.

5Press the Esc key to get back to Normal mode.

You have to press the Esc key to switch from Insert mode to Normal mode.

6Type a colon (:) to move the cursor to the bottom of the screen.

This is where final commands can be made.

7Type wq.

wq is two individual commands: w for Write (or Save) and q for Quit. This command combination returns you to the command line.