Shell Editors

 < Day Day Up > 

When we discussed shells scripts in the preceding chapter, we wrote all the scripts in one of the text editors you had been previously introduced to. However, this is not the only way to write a shell script. Since some machines are running in shell mode only, how would you write a script on those machines? There are text editors that actually work entirely from the shell. The three most common would probably be vi, Pico, and Emacs. Of these, vi is probably the most common, although programmers frequently use Emacs to write program code.


The vi editor works entirely from the shell. To invoke the vi editor and begin an editing session, enter vi, followed by the name of the file you want to edit, and then press Return.

   % vi filename [Return]

For example, if you want to edit the file that we created in the previous chapter, you would enter:


You would then see something like what is shown in Figure 21.6.

click to expand
Figure 21.6: Opening files with the vi editor.

vi automatically sets aside a temporary buffer as a working space for use during this editing session. If the file you specify when calling vi does not already exist in your current working directory, a new file will be created with the name you specified. This means that you create a new file with the vi editor the same way you open an existing file; just make certain that the name you use is not already being used by another file in that directory.

There are several points you should note about the vi editor. Perhaps the most important thing is that vi only operates in one of two modes. These are the Insert mode and the Command mode. In Insert mode, whatever you type will be inserted into the file you are editing, up to the point when you exit Insert mode by pressing the Escape key. This is obviously the mode used to actually type in the text of a document. In command mode, you can execute a variety of commands. These commands include cut and paste, move the cursor, search, delete characters, delete entire lines, and so on.

In command mode you need to be very aware that almost every key on the keyboard executes a command, so be careful what keys you press. Since it is possible for you to change your document accidentally, you should be aware of how to undo such changes. Provided your cursor has not left a particular line of text, typing in an uppercase U will undo all the changes that were applied to that line of text, restoring the line to its original state. You also can press a lowercase u to undo the last command, but only the last command.

The vi editor has a long list of commands that can be executed. You should be aware of the more commonly used commands. When the vi editor first starts, it begins in Command mode. You should be aware of this because you cannot begin typing and inserting text. One of your first commands normally will be to move the cursor and start inserting. Table 21.3 summarizes some basic vi editor commands.

Table 21.3: Basic vi Commands




This command inserts text at the current cursor position. All characters typed after i will be inserted. To exit this mode, press the Escape key.


This command will append text after the current cursor position. All characters typed after a will be inserted. To exit this mode, press the Escape key.

h, j, k, l

These keys move the cursor in various directions. They move the cursor left, down, up, and right, respectively.


This key moves the cursor to the end of the current line.


This key moves the cursor to the first line.


This key moves the cursor to the last line.


Pressing the d key twice deletes the line the cursor is on.


Pressing the x key deletes the character the cursor is on.


The lowercase u will undo the previous command.


This command will undo changes to the current line.


This command will paste the last deleted text at the cursor position. This is much like the Ctrl key and the v key being pressed simultaneously in most Windows programs.


This command will discard all of your changes and exit the vi editor.


This command will save all of your changes and exit the vi editor.

You will need to keep in mind that while you are inserting text, you will need to type a carriage return at the end of each line. Otherwise, your text will be one very, very, very long line. Unfortunately the vi editor does support automatic word wrapping.

You might think the vi editor seems a bit complex and has a number of archaic commands. You are correct. It might seem curious then that this editor is so popular. However, it is popular among Linux aficionados because it has been around for a long time and has been in widespread use. Of course, proponents of the vi editor would argue that, with its plethora of commands, it is also quite a powerful text editor.


If you thought the vi editor was a bit too complex, it is quite possible that you will find Pico more to your liking. Pico is a simple text editor. The first thing you will notice when you launch Pico is that the main commands are displayed at the bottom of the screen and that context-sensitive help is provided. This means that you don’t have to memorize any commands. This may be the single most important advantage of Pico over vi. The commands you need are shown at the bottom of the screen. When you open a file with Pico, you should see something much like Figure 21.7, where one of the scripts from the preceding chapter is shown open for editing.

click to expand
Figure 21.7: Opening a file in Pico.

There is no command mode or insert mode in Pico. As you type characters on your keyboard, they are immediately inserted into the text. Any commands you might need are entered using control-key combinations. The Pico editor is an excellent choice for those new to Linux. It is also quite easy for the Windows user to become accustomed to. You launch Pico, and in fact most text editors, the same way you launch vi, by typing in pico, followed by the file you want to open.

 < Day Day Up > 

Moving From Windows to Linux
Moving From Windows To Linux (Charles River Media Networking/Security)
ISBN: 1584502800
EAN: 2147483647
Year: 2004
Pages: 247
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: