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.

UNIX Text Editors

Many text editors are included with most UNIX operating systems. Some are popular, others are specialized and may not be of much use. If you are already familiar with another text editor, feel free to use that editor to create shell scripts during this course.
Some of the popular UNIX text editors include the following:
  1. emacs, a very powerful text editor with hundreds of configuration options and keyboard commands. A graphical version of emacs, called xemacs, provides menus to access basic editing features.
  2. pico, a simple editor with on-screen keyboard commands to make it easy for new users to create and edit text files.
  3. ed, an old-fashioned editor that works on a single line of a file at a time.
  4. joe, an easy to use editor with basic functionality

Of the editors in the above list, vi and nano are available for Red hat and Ubuntu Linux. If you are working on a UNIX system at your site, you may have one or move of these text editors, in addition to a graphical text editor. Each version of UNIX provides a basic text editor from its graphical menus. Consult your documentation or system administrator for assistance in located a graphical editor on your UNIX system.

What Are Shell Scripts?

A shell script is a text file that contains one or more commands. This seems pretty simple after all this buildup, but that is all a script really is. The power of shell scripts, however, lies not in the simplicity of the concept, but in what you can do with these files. In a shell script, the shell assumes each line of the text file holds a separate command. These commands appear for the most part as if you had typed them in at a shell window. (There are a few differences, covered in the chapters to follow.) For example, this code shows two commands:
df -k

The following Try It Out shows you how to make these two commands into a script and run it. You will see a number of very short examples that should provide a quick overview to scripting.

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.

SEMrush Software