vi can be used both when your system is in text mode (the entire screen is devoted to text and there are no images) and when your system is in GUI mode (the screen contains windows, images and menus). When it is in GUI mode (usually KDE or Gnome), vi runs in a terminal window. A terminal window is a text-only window, and it can usually be opened by clicking on an icon (small image) of a computer screen.
(In the case of Red Hat Linux, the terminal window can be opened by clicking on the icon of a red hat in the lower left hand corner of the screen, opening the System Tools menu and then selecting Terminal from that menu. It can be convenient to add the icon for the terminal window to the launcher panel along the bottom of the screen, if it is not already there.)
There are at least two ways to use vi to simultaneously create and open a new file. One is by just typing
This creates an empty file that will not have a name until you save its contents to disk (i.e., transfer the text you typed into it to your hard disk, floppy disk, etc. for long term storage).
You will notice that a (mostly) blank window opens after you type this command. Do not be concerned about trying to type any text into the screen yet (although you will probably want to). This topic will be covered in the next chapter.
(You might initially see something different than the nearly empty screen described above. The reason is most likely that your computer is using a clone of vi instead of the real thing. This is no problem, as the core commands of vi are also used in the clones. If Vim, the most popular clone, is installed in your system, the initial screen will have some text in the center that begins with something like "VIM - Vi IMproved." vi clones will be discussed in more detail in a later section.)
A second way to open a new file is by typing
This creates a new file named "apple" in the current directory (the directory or folder which is currently open on your all-text screen or your terminal window).
If you want, you could create the same file with an extension such as ".txt" added to the end of the file name. In Linux this is merely a matter of convenience (or habit), and it generally makes no real difference for the file because it remains a plain text file in either case. For example:
New files can also be created in directories other than the current directory. For example, to create and open a file named "apple" in the directory /home/jane/, type:
(As you probably already know from your study of Linux or Unix, the above example can be performed regardless of where your current directory is on your system because the path for the command begins with a root directory, i.e. /home. This example assumes that you have the proper permissions to create files in the /jane directory.)
The technique for opening existing files is identical. Just type
When you open an existing or new file, you will notice that nearly the entire screen is either devoted to the text of the file or available for entering text. The only exception is the very bottom line, which provides information about the file. This information initially includes the number of lines and the number of characters in the file. When commands are entered, the bottom line is used instead to show the entry of such commands.
You will also notice a blinking black rectangle in the upper left hand corner of the screen. That is the cursor, and its role is to tell you what character or position in the file is currently ready to be acted upon.
Stretching down the left hand edge of the screen is a column of tildes (~). These are merely row markers and are not part of the text. They will disappear as your typing extends down the screen.
To close a file to which no changes have been made, hit ESC (the Esc key, which is located in the upper left hand corner of the keyboard), then type
To close a file to which changes have been made (such as text having been added or removed) without saving the changes, hit ESC, type
vi works with a buffer (a block of memory in the RAM chips). When you open an existing file, vi copies that file from the hard disk (or floppy, CDROM, etc.) to a buffer. All changes that you make to a file are initially made only to the copy in the buffer, and they are only made to the file itself when you "save" your changes. "Saving" a file means writing (i.e., transferring) the contents of the buffer to the hard disk (or floppy disk).
Likewise when you open a new file. All text you enter (and subsequent edits you make to it) exists only in the buffer until you save the file to disk.
To save the changes that have been made to a file, hit ESC, type
After you have created a new text file and closed it, you might want to confirm that nothing went wrong and that the file actually exists. Probably the simplest way to do this is to use the standard Unix ls command, which displays a list of all of the files in the current directory.