DistributedNetworks DistributedNetworks


Unix Shell Scripts   «Prev  Next»
Lesson 4 Using a text editor
Objective Use the vi text editor.

Using vi Text Editor

Shell scripts are text files and cannot contain any special formatting or font information. Shell scripts can be created in any text editor like Notepad++ , as long as they are saved as plain text files.
Some text editors can color code shell scripts, highlighting comments, commands, and variables. This information is to help you understand the script. The formatting and colors are not saved as part of the text file.

Why use vi?

The most well-known text editor on modern UNIX systems is the vi editor. UNIX systems have other editors, including graphical editors that are easy to use. But because vi is available on all UNIX systems, we will use it as the basis for writing our shell scripts. The information presented in this lesson should be a review for you. It is not easy to use vi until you have mastered a few of its fundamentals.
The vi text editor has several modes. The two main modes you will use are command mode and insert mode. In command mode, characters that you enter are interpreted as commands; in insert mode, characters that you type are entered as part of your document. After inserting text (or if you are not sure what mode you are in), you can always change to command mode by pressing the Esc key, which toggles between the two modes.

VI Editor Modes

The first thing most users learn about the VI editor is that it has two modes: command and insert. The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyboard into the current file. VI starts out in command mode. There are several commands that put the VI editor into insert mode. The most commonly used commands to get into insert mode are a and i.
For example, hit i key and type "This is EASY.", then hit the escape key. Once you are in insert mode, you get out of it by hitting the escape key.
You can hit escape two times in a row and VI would definitely be in command mode. Hitting escape while you are already in command mode doesn't take the editor out of command mode. It may beep to tell you that you are already in that mode.

Basic vi commands


The following table describes some basic commands in vi.

Unix Text Editor - Exercise

Click on the Exercise link below to practice using vi in the UNIX Lab.
Unix Text Editor - Exercise
The next lesson introduces a command to write information to the screen from a script.