Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Adding and Deleting Text in vi

Adding and deleting text in vi is a bit more complicated than doing the same in nano. In nano, you basically just place your cursor where you want to make changes, whereas vi has a whole slew of commands that you use to specify where the changes should occur. (Tables 4.1, 4.2, and 4.3 list only a very few of your options.) Plus, to issue the commands, you have to switch to normal mode.

TABLE 4.1 vi Commands to Add Text

Command

Function

a

Adds text after the cursor

A

Adds text at the end of the current line

i

Inserts text before the cursor

I

Inserts text at the beginning of the current line

o

Inserts a blank line after the current line

O

Inserts a blank line before the current line

TABLE 4.2 vi Commands to Delete Text

Command

Function

x

Deletes one character (under the cursor)

X

Deletes one character (behind the cursor)

dd

Deletes the current line

5dd

Deletes five lines starting with the current line (any number would work here)

dw

Deletes the current word

cw

Changes the current word (deletes it and enters input mode)

r

Replaces the character under the cursor with the next character you type

R

Replaces the existing text with the text you type (like overtype mode in most word processors)

TABLE 4.3 Other Handy vi Editing Commands

Command

Function

yy

Copies the current line

p

Pastes any copied text after the cursor or line

J

Joins the current and following lines

u

Undoes the last change

U

Undoes all changes on the current line

.

Repeats the last command

To add or delete text in vi:

  1. vi

    To begin, type vi at the shell prompt.

  2. i

    Change into insert mode.

  3. There once was a man from Nantucket

    Type some text that you’ll want to add to.

  4. esc.jpg

    Press esc.jpg to enter normal mode before you issue the commands.

  5. Choose a command, based on what you want to do to the text.

    • Table 4.1 lists commands to add text.
    • Table 4.2 lists commands to delete text.
    • Table 4.3 lists miscellaneous editing commands.
  6. dd

    Type the command. Here, we’re deleting the current line of text.

  • + Share This
  • 🔖 Save To Your Account