Next: Copying and Moving Up: Advanced Vi Tutorial Previous: Moving around

Modifing Text

The aim is to change the contents of the file and vi offers a very large set of commands to help in this process.

This section will focus on adding text, changing the existing text and deleting the text. At the end of this section you will have the knowledge to create any text file desired. The remaining sections focus on more desireable and convenient commands.

When entering text, multiple lines can be entered by using the [ return ] key. If a typing mistake needs to be corrected and you are on the entering text on the line in question. You can use the [ backspace ] key to move the cursor over the text. The different implementations of vi behave differently. Some just move the cursor back and the text can still be viewed and accepted. Others will remove the text as you backspace. Some clones even allow the arrow keys to be used to move the cursor when in input mode. This is not normal vi behaviour. If the text is visable and you use the [ ESC ] key when on the line you have backspaced on the text after the cursor will be cleared. Use your editor to become accustomed to its' behaviour.


        a           Append some text from the current cursor postion
        A           Append at the end of the line
        i           Insert text to the Left of the cursor
        I           Inserts text to the Left of the first non-white character
                        on current line
        o           Open a new line and adds text Below current line
        O           Open a new line and adds text Above the current line

We give it and we take it away. vi has a small set of delete commands which can be enhanced with the use of command modifiers.


        x           Delete one character from under the cursor
        dw          Delete from the current position to the end of the word
        dd          Delete the current line.
        D           Delete from the current position to the end of the line

The modifiers can be used to add greater power to the commands. The following examples are a subset of the posibilities.


        nx          Delete n characters from under the cursor
        ndd         Delete n lines
        dnw         Deletes n words. (Same as ndw)
        dG          Delete from the current position to the end of the file
        d1G         Delete from the current postion to the start of the file
        d$          Delete from current postion to the end of the line
                    (This is the same as D)
        dn$         Delete from current line the end of the nth line

The above command list shows the delete operating can be very powerfull. This is evident when applied in combination with the cursor movement commands. One command to note is [ D ] since it ignores the modifier directives.

On occasions you may need to undo the changes. The following commands restore the text after changes.


        u           Undo the last command
        U           Undo the current line from all changes on that line
        :e!         Edit again. Restores to the state of the last save
not only allows you to undo changes, it can reverse the undo. Using the command [ 5dd ] delete 5 lines then restore the lines with [ u ] . The changes can be restored by the [ u ] again.

vi offers commands which allow changes to the text to be made without first deleting then typing in the new version.


        rc          Replace the character under the cursor with c
                    (Moves cursor right if repeat modifier used eg 2rc)
        R           Overwrites the text with the new text
        cw          Changes the text of the current word
        c$          Changes the text from current position to end of the line
        cnw         Changes next n words.(same as ncw)
        cn$         Changes to the end of the nth line
        C           Changes to the end of the line (same as c$)
        cc          Changes the current line
        s           Substitutes text you type for the current character
        ns          Substitutes text you type for the next n characters

The series of change commands which allow a string of characters to be entered are exited with the [ ESC ] key.

The [ cw ] command started from the current location in the word to the end of the word. When using a change command that specifies a distance the change will apply. vi will place a $ at the last character position. The new text can overflow or underflow the original text length.



Next: Copying and Moving Up: Advanced Vi Tutorial Previous: Moving around