Home > Articles

Basic vi Skills

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

Command Mode Editing

In this section we use a sample text file to perform some editing functions. The system must have been installed with the Kernel Development section if it's a Red Hat distribution, or the equivalent packages from a Debian distribution:

/usr/src/linux-2.4/Documentation/watchdog.txt

NOTE

The following is a suitable file for practicing editing commands:

/usr/src/linux-2.4/Documentation/watchdog.txt

Inserting and Adding Text

One of the most common tasks in a vi session is making changes to the text in a file. This involves using one of the following keys to change from Command mode to Insert mode:

  • i—Inserts text to the left of the cursor

  • I—Inserts text at the beginning of the text in that line, not the beginning column of the vi screen

  • a—Appends to the right of the cursor

  • A—Appends at the end of the current line

  • o—Begins a new line below the current line

  • O—Drops the current line and begins a new one in its place

Changing or Replacing Text

The following are incredibly useful when you're altering an existing file and need to change a character, a line, a sentence, or just a word:

  • cw—Changes a single word from the current cursor position. To change a whole word, you put the cursor on the first character of it.

  • c$—Changes the current line but doesn't extend to change the rest of a wrapped sentence on the screen.

  • r—Replaces the character under the cursor.

  • R—Replaces text on the same line until Esc is pressed, but it doesn't change text on the next line. Instead, it pushes it ahead of the current changes.

NOTE

Remember to use the force multipliers in the appropriate places. You can easily change multiple words by using 5cw or replace 10 characters with 10r.

Deleting Text and Lines

A more advanced use of vi is to remove or delete characters, words, or even lines. Be careful to check your deletions and changes, or press the u key to get things back to normal and try it again.

  • x—Deletes a single character under the cursor

  • X—Deletes a single character before the cursor

  • dw—Deletes a single word that's currently under the cursor, from the cursor position onward

  • dd—Deletes the current line entirely, regardless of the cursor position in the line

  • D—Deletes all text from the current cursor position to the end of the line

  • dL—Deletes all text from the cursor to the end of the screen

  • dG—Deletes all text from the cursor to the EOF

  • d^—Deletes all text from the beginning of the line to the cursor

NOTE

In the lexicon of computer software verbiage, delete in vi stores the cut or deleted string of text in the unnamed buffer for future use. This buffer's contents will be overwritten by any further cuts or deletions.

CAUTION

How you delete is important on the exam. If asked to delete from the current position to the end of the line, don't pick or type the keys that delete the whole line—there are no partial credits on the exam!

  • + Share This
  • 🔖 Save To Your Account