home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 2.3 Simple Edits Chapter 2
Simple Editing
Next: 2.5 Joining Two Lines with J

2.4 More Ways to Insert Text

You have inserted text before the cursor with the sequence:


text to be inserted


You've also inserted text after the cursor with the a command. There are other insert commands for inserting text at different positions relative to the cursor:

A Append text to end of current line.
I Insert text at beginning of line.
o Open blank line below cursor for text.
O Open blank line above cursor for text.
s Delete character at cursor and substitute text.
S Delete line and substitute text.
R Overstrike existing characters with new characters.
All of these commands leave you in insert mode. After inserting text, remember to press [ESC] to escape back to command mode.

A (append) and I (insert) save you from having to move your cursor to the end or beginning of the line before invoking insert mode. (The A command saves one keystroke over $a . Although one keystroke might not seem like much of a saving, the more adept (and impatient) an editor you become, the more keystrokes you will want to omit.)

o and O (open) save you from having to insert a carriage return. You can type these commands from anywhere within the line.

s and S (substitute) allow you to delete a character or a whole line and replace the deletion with any amount of new text. s is the equivalent of the two-stroke command c [SPACE] and S is the same as cc . One of the best uses for s is to change one character to several characters.

R ("large" replace) is useful when you want to start changing text, but you don't know exactly how much. For example, instead of guessing whether to say 3cw or 4cw , just type R and then enter your replacement text.

2.4.1 Numeric Arguments for Insert Commands

Except for o and O , the above insert commands (plus i and a ) take numeric prefixes. With numeric prefixes, you might use the commands i , I , a , and A to insert a row of underlines or alternating characters. For example, typing 50i* [ESC] inserts 50 asterisks, and typing 25a*- [ESC] appends 50 characters (25 pairs of asterisk and hyphen). It's better to repeat only a small string of characters. vi has difficulty repeating the insertion of more than one line's worth of text.

You can use a numeric prefix with S to substitute several lines. It's quicker and more flexible, though, to use c with a movement command.

A good case for using the s command with a numeric prefix is when you want to change a few characters in the middle of a word. Typing r wouldn't be enough, but typing cw would change too much text. Using s with a numeric prefix is usually the same as typing R .

There are other combinations of commands that work naturally together. For example, ea is useful for appending new text to the end of a word. It helps to train yourself to recognize such frequent combinations so that they become automatic.

Previous: 2.3 Simple Edits Learning the vi Editor Next: 2.5 Joining Two Lines with J
2.3 Simple Edits Book Index 2.5 Joining Two Lines with J

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System