Editing files

Starting vi

To edit a file, type vi filename and if the file already exists, vi will read it in. If it does not exist, vi will create it. (See ``Filenaming conventions'' for information about naming files.) In this example, the command vi soliloquy loads the file soliloquy in the current directory. You may notice that this version of Hamlet's soliloquy contains some misquotations; these will be removed in later sections:

   To be, or not to be: that is the question:
   Whether 'tis nobler in the mind to suffer
   The slings and arrows of outrageous fortune,
   Or to take arms against a sea of troubles,
   And by opposing end them. To die, to sleep -
   No more - and by a sleep to say we end
   The heartache, and the thousand natural shocks
   Which flesh is heir to! 'Tis a consummation
   Devoutly to be wished. To die, to sleep -
   To sleep - perchance to dream: ay, there's the rub,
   For in that sleep of death what dreams may come
   When we have shuffled off this mortal coil,
   Must give us pause. There's the respect
   Which makes calamity of so long life:
   For who would bear the whips and scorns of time,
   Th' oppressor's wrong, the proud man's contumely,
   The pangs of despised love, the law's delay,
   The insolence of office, and the spurns
   Which patient merit of th' unworthy takes,
   When he himself might his quietus make
   With a bare needle? Who would needles bear,
   To grunt and sweat under a weary life,
   The fair Ophelia! - Nymph, in thy orisons
   Be all my sins remembered.
   "soliloquy" 35 lines, 1502 characters
On the bottom line of the screen, vi reports soliloquy as having 1502 characters on 35 lines of text:

"soliloquy" [Read only] 35 lines, 1502 characters

This line indicates that the file permissions on soliloquy are set so that you may not write to it. If you own this file, and you wish to make changes to it, change its permissions from within vi and reload it for editing using the following commands:

:!chmod u+w soliloquy

When you start vi you are in command mode. vi has two modes; command mode and insertion mode. In command mode you can issue commands to vi and move around your document. In insertion mode, you can only enter text.

If you want to start editing a file at a point part of the way through it, rather than at the beginning, you can start vi with the following command:

vi +line filename

where line is the line number to position the cursor at. To start editing at the end of the file (for example, to append information to a list), start vi with the following command:

vi + filename

Next topic: Entering text
Previous topic: A quick tour of vi

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003