6. Creating and Altering Files

Many UNIX users will probably create files on a microcomputer and then transfer those files to a UNIX system. But you may find it useful to create or edit files on the UNIX system itself. One way to create new files in your directory, or to alter files you already have, is to use a text editor, a program written specifically to perform these tasks. The most commonly used UNIX text editor is vi (pronounced ``vee-eye''). The GNU EMACS screen editor is also popular. For more information, see man emacs. The easiest editor to use, but one that has limited features, is Pico. This simple display editor is also used with the Pine mailing program. See man pico for more information.

Ex is a line-oriented editor that is related to vi because it has an ``open'' or ``visual'' mode. When you go into visual mode using ex, you are actually using the display editor, vi. If you initiate editing with vi, you can likewise move from visual editing mode into ex.

The following sections offer an elementary introduction to vi. There are many more commands and features than this chapter presents, but these should be adequate for you to create and modify files and to learn how this editor works.

Starting vi

Before using vi, be sure that you have correctly identified your terminal type to the UNIX system. Vi uses information associated with your terminal type when it moves the cursor and scrolls lines of text up and down. If you have not yet identified the terminal type, as described in section 2.3, do so before calling vi. If you use vi without identifying your terminal type, vi responds ``[using open mode]", and displays only one line at a time.

To call the vi editor, use the command

     vi name
where name is the name of the file you want to create or modify. If the file already exists in your working directory, vi displays the first screenful and, at the bottom line of the display, tells you how big the file is. If vi can't find a file by that name, it tells you the file is new.

Unlike some display editors, vi does not automatically insert into the file everything that you type. Instead, it has a command mode and an insert mode. When you start up vi, you are in command mode. If you give a command that puts vi into insert mode, you remain in insert mode until you press the ESC key to go back to command mode. Most commands you type to vi will not show on the screen, and they do not need the Return key to terminate them. One exception to this is commands that begin with a colon (:). After you type such a command, it is displayed on the bottom line of the screen, and you must press the Return key to issue it. A command that begins with a colon is actually passed to the ex editor, which performs the command and returns you to vi.

Controlling the Cursor and Window

The portion of the text that you see displayed on the screen appears to be in a window that moves forward and back (downward and upward) over the file. Within that window, you move the cursor to the position where you want to insert, delete, or replace text. When you begin editing a file that already exists, the window is the first screenful, or page, of the file. The following control characters move the window up and down:
      ^d    Moves the window down half a screenful.
      ^u    Moves the window up half a screenful.
      ^f    Moves the window forward one full screen (with a few
            lines of overlap).
      ^b    Moves the window backward one full screen.
To redraw the current window, use ^l (lowercase ``L'').

To move the cursor within the window, use any of the following:

     h,j,k,l  Moves the cursor one character position left, down, up,
              or right, respectively.  On some terminals, the arrow
              keys serve the same function.
     +        Moves the cursor to the beginning of the next line.
     -        Moves it to the beginning of the previous line.
     Return   Same as +.
     H        Moves the cursor to the top (highest line) of the screen.
     L        Moves it to the bottom (lowest line) of the screen.
     M        Moves it to the middle line on the screen.
     w        Moves the cursor right, to the beginning of the next word.
     b        Moves it left, to the beginning of the previous word.
     0 (zero) Moves it to the beginning of the current line.
     $        Moves it to the end of the current line.
To move the cursor to a position that may be outside the current window, use one of the following commands:
     /stringReturn
                Searches forward for string, centering the line
                containing it in the new window.  If the search reaches
                the end of the file without finding the string, it
                ``wraps around'' to the beginning of the file and
                continues searching.

     ?stringReturn
                Searches backward for string, also wrapping
                around if string is not between the starting
                point and the beginning of the file.

     nG         Moves to line n.

     G          Moves to the last line of the file.
The value of string is ``remembered'', and so /Return searches forward for the most recently named string, and ?Return searches backward for it.

Inserting Text

Several commands put vi into insert mode. In insert mode, everything you type is inserted into the file until you press the ESC (escape) key. Here are some of the most useful commands to initiate insert mode:
     i  Begins inserting text immediately before the cursor.
     a  Begins inserting text immediately after the cursor.
     o  Opens a new line below the current one and enters insert
        mode.  You may type any number of new lines.
     O  (uppercase o) Opens a new line above the current one and
        enters insert mode.

Deleting Text

In general, use the d command to delete material. The d command takes an argument to describe what kind of entity to delete, and it can be prefixed with a number to indicate the number of repetitions for this deletion. Typing d and then striking the space bar deletes the character under the cursor. If you type 5d, then space, you delete five characters--the current one plus the next four. Other forms are:
     dw Deletes from the cursor position through the end of the
        word. 4dw deletes four words: the current one and the next three.

     dd Deletes the entire current line; 2dd deletes two lines.
To simply delete the character under the cursor, you can also type x. There is also a D command (capital D). It deletes the rest of the current line, beginning with the character under the cursor.

Altering Existing Text

To replace existing text with new text, you could simply use an appropriate d command, then insert new material at the same point. Here are some other commands that replace existing text with new text:
     r         Followed by a single character, this replaces the character
               under the cursor with the new character.  For example, rs
               replaces whatever is under the cursor with an ``s''.

     Rtext     Followed by the ESC key, this acts as if you had given
               a sequence of r commands, one for each character of the
               new text.  That is, it is a one-for-one character
               replacement of any length you choose.  For example, suppose
               the cursor is on the h of the line
               ``This is a funny example.''
                  ^
               and you give the command
                    Rry a non-<ESC>
          
               where <ESC> is the ESC key.  The line would now read
          
               ``Try a non-funny example.''
                          ^
     nstext    Followed by <ESC>, this substitutes text for the next n
               characters, starting with the one under the cursor.  A $
               covers the nth character when you begin the substitution.

     ncwtext   Followed by <ESC>, this changes the next n words, beginning at
               the cursor position, to text. When you start, a $ covers
               the last character of the nth word to be replaced.

     nStext    Followed by <ESC>, this replaces n lines, beginning
               with the current one, with text. If you don't give
               a number, the entire current line is replaced.

     Ctext     Followed by <ESC>, this replaces the rest of the current
               line (starting with the cursor position) with text.
Notice that, except for the r and R commands, all these commands delete a fixed amount of material and then put vi into insert mode. The new text you supply can contain any number of characters or lines--whatever you type until you press the ESC key.

Two additional commands are useful here. The first is the u (undo) command. It undoes the most recent command that changed the file you are editing. This is useful for correcting an editing mistake--if you give the u command immediately after you make that mistake. You cannot ``back up'' to an earlier change because a second u would only undo the previous u. The other, the xp (transpose) command, is actually the x (delete-character) followed by the p (put) command. The result of xp is to interchange the character under the cursor with the one to the right of it.

Moving and Replicating Text

To rearrange lines or groups of lines, use ``yank'' and ``put'' commands:
     nyy  ``yanks'' (actually duplicates) n complete lines,
           beginning with the current one, and places them in an unnamed
           buffer (a temporary storage area).

     nyw   Yanks n words, starting at the current cursor
           position, and places them in the unnamed buffer.

     p     Puts text from the unnamed buffer after the current line
           if the buffer contains full lines, or after the cursor
           position if the buffer contains a partial line.

     P     Like p, but puts text from the unnamed buffer BEFORE
           the current line or cursor position.
After you have used p or P, the yanked material exists in both its original and new location in the file. You can now delete it from its original location. If you need several copies of some original text, you can yank it once, then use p or P to put it in as many new places in the file as you need.
Important Note: There is only one unnamed buffer. Every time you place text into this buffer, the buffer's previous contents are destroyed. This unnamed buffer is also used for ANY piece of text you delete with an x, d, D (or other) command. This is why the xp (transpose) command works: the x command places a character into the buffer, then the p inserts it after the cursor.

Saving Your Work and Leaving the Editor

When you are working on a file using vi, you are actually making changes to a working copy of that file. To make the changes permanent, you must write out the working copy, either replacing the previous file or making a new one. When you write out the file, you can exit from or remain in vi. You can also leave vi without writing out any file, thus discarding your editing changes and preserving the previous version of the file. Some ways to do these tasks are:
     ZZ   Exits from vi, saving any changes you made.

     :wq  Writes the file unconditionally (even if it was
          unchanged) and quits.

     :w   Writes out a new version of the file, replacing the
          previous one, but keeps you in vi.

     :w name
          Writes the new version as file name, leaving the
          original file untouched, and keeps you in vi.

     :q!  Quits, discarding all your editing changes.

     :e!  Discards your editing changes and starts editing the
          previous version of the file all over again.
All the forms beginning with a colon (:) must be terminated by the Return key.

Note: If you cannot write out your file because you have reached your ``hard'' quota, you may be able to remove unwanted files without leaving vi, then write out your work. To do this, use the following commands:
     :sh
This gets you to the shell, temporarily setting aside your vi editing session. Now use
     ls -l 
to get a long-form directory listing and
     rm files
to delete unneeded files and get below quota. Then use ^d or exit to exit from the shell and come back to vi. Now write out your edited file.

More Information about vi

For detailed information about vi, see Learning the vi Editor, by Linda Lamb (O'Reilly & Associates, 1990; ISBN 0937175676).

Go to next chapter

Go back to table of contents