Exploring Your Linux System

Basic File Editing Using the Visual Editor (VI)

In the hands of an expert vi is a very efficient tool, in the hands of a beginner, it is a pain in the #$$. All applications and services on Linux store their configuration in text files, making text file manipulation one of the most important skills an administrator can posses. Although not the only tool, vi is the most common text editor available, and by far the oldest. One reason for vi’s versatility is that it relies on standard keys for all its operations. Special key combinations such as [alt+f], commonly used to activate a File menu do not work over telnet sessions, making remote use of many tools difficult.

vi has two modes. Command and Edit mode. In command mode any keys you press represent file manipulation commands, not text characters. Once in Edit mode, those same keys mean something different. Linux ships with a newer version of vi, called vim (activated by typing [vi]) which includes a third option, called the colon (:) prompt. The colon prompt can accept more complex commands, and support command editing.

When you run vi you will be in Command mode. To return to command mode press [esc] at any time. Use your cursor keys to place the cursor where you want to edit a file, and press either [a] or [i]; append or insert. There is very little difference between these two commands, so it is only necessary to learn one. You will notice on the bottom of the screen that you have been switched to insert mode (if you press [a], append is displayed on some systems). You may now edit you file. When you are finished editing your file, press the [esc] key and you will be returned to Command mode. To save your file and exit press [shift+zz].

If you require a more complex operation, such as a save without exiting, or a save as, you can activate the colon prompt. Simply enter [:] and you will see it in the lower left of the screen. Enter [!qw] to quit and save, [q!] to quit without saving, and [w] to save. To save the file under a new name enter [w filename]