5.2 Starting and Stopping

   

5.2 Starting and Stopping vi

The editor is started when you use a vi command and give a file name as its argument. For example, if you want to edit a file with the name myfile , you will use:

 $  vi myfile  

This command starts vi and allocates a memory buffer for file myfile . If the file already exists, text in the file will be displayed on the screen. The bottom line, which is used as a status line, will display a file name, line number, and number of characters in the file. Remember that you can't add any text to the file until you go into insert mode because vi started in the command mode. You will see something like Figure 5-1 when you start vi .

Figure 5-1. A sample vi startup window.

graphics/05fig01.gif

The tilde character ( ~ ) you see in the screen shot shows that the file has no text in these lines. To switch to insert mode, you press the i character just after starting vi . After that you can insert any text into the file. One line of text may be up to 256 characters in length. When you press the graphics/enter.gif key, you go to the next line. When you have finished entering text into the file, you press the graphics/esc.gif key to return to command mode. Now you can issue any required command. To save the file, you can use the :w command. You will observe that as soon as you enter the colon character, the cursor goes to the bottommost line (the same line that was used as the status line) and the colon is displayed in the start of that line. This is the line where vi commands are displayed.

After you save the file, you can quit the editor with the :q command. You can also use :x or :wq to save and quit in one step.

Note that you can use the vi command without any file name. In that case the editor will start with an unnamed file buffer in the memory. When you need to save a file, you can use :w filename instead of :w in the command mode. The buffer will be saved with the file name you supplied with the :w command. In the same fashion, you can use the :w filename command to save a file buffer with a new name if you have already opened a named file.

Multiple files can be opened with vi by supplying multiple file names at the command line. If you want to open three files ( file1 , file2 , file3 ) simultaneously , the command will be as follows .

 $  vi file1 file2 file3  

If you made some changes to a file and don't want to save them, you can quit the vi editor with the :q! command. If you try to quit the vi editor after making changes to it with the :q command, it will refuse . The exclamation symbol " ! " forces vi to quit even if the file buffer is changed. You can add the exclamation symbol to any command when you want to force vi to do something.

Note

Most vi beginners make mistakes while using vi , resulting in unexpected changes to the file being edited. It is a good habit to use the :w command every few minutes to save the file being edited. Then if you make a mistake and don't understand what has happened , just quit vi with the :q! command and restart it with the saved file. In this case, you will lose only the data typed after the last save.



   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net