Home Technical Support vi Editor: Searching Text, Visual Selection, and Deleting in Labs

vi Editor: Searching Text, Visual Selection, and Deleting in Labs

Last updated on Sep 08, 2026

vi Editor: Searching Text, Visual Selection, and Deleting in Labs

The Search text in vi Editor and Text handling in vi Editor exercises teach searching, selecting, and manipulating text in vi. These exercises appear across 8 courses, making them among the most commonly encountered exercises in the platform. This article builds on the vi Editor basics article and covers the advanced operations.


Table of Contents

  1. Searching for Text with /
  2. Searching Backward with ?
  3. Navigating Multiple Occurrences
  4. Visual Character Mode (v)
  5. Visual Line Mode (V)
  6. Deleting Selected Text
  7. Deleting Entire Lines with dd
  8. Undo and Redo
  9. Common Questions

Searching for Text with /

Forward Search

Open a file in vi, ensure you're in Command Mode (press Esc), then type / followed by the search term:

/High

Press Enter. The cursor jumps to the first occurrence of "High" in the file.

Common mistake: Trying to search in Insert Mode. You must be in Command Mode for / to work as a search command.

Fix: Press Esc first, then type /search_term.


Searching Backward with ?

Backward Search

The ? command searches in the opposite direction (upward through the file):

?High

Press Enter. The cursor jumps to the previous occurrence of "High" (above the current position).


Navigating Multiple Occurrences

Jump Between Matches

After a search with / or ?, use these commands to navigate between occurrences:

Key Action
n Jump to the next occurrence (same direction as the last search)
N (Shift + n) Jump to the previous occurrence (opposite direction)

Example workflow:

  1. /High → finds first "High"
  2. n → jumps to second "High"
  3. n → jumps to third "High"
  4. N → goes back to second "High"

Visual Character Mode (v)

Selecting Individual Characters

Press v in Command Mode to enter Visual Character Mode. Move the cursor with arrow keys or h/j/k/l to highlight individual characters or phrases:

1. Press v
2. Move cursor to select the text you want
3. Text is highlighted

Use case: Selecting a word, sentence, or partial line for copying, deleting, or other operations.


Visual Line Mode (V)

Selecting Entire Lines

Press Shift + V (capital V) in Command Mode to enter Visual Line Mode. This selects entire lines:

1. Press Shift + V
2. Move cursor up/down to select multiple lines
3. Entire lines are highlighted

Use case: Selecting multiple complete lines for deletion or copying.


Deleting Selected Text

Delete with d After Selection

Once text is selected in Visual Mode (character or line), press d to delete it:

1. Open file: vi output.json
2. Enter line mode: Shift + V
3. Navigate to select lines
4. Press d to delete

The deleted text is yanked (copied to the vi buffer) and can be pasted with p.


Deleting Entire Lines with dd <a name="delete-line>

Quick Line Deletion Without Selection

Place the cursor on any line and press dd to delete the entire line:

1. Navigate cursor to the line you want to delete
2. Press dd (two d's)
3. The line is removed

Press dd multiple times to delete consecutive lines. For example, 5dd deletes 5 lines starting from the current line.


Undo and Redo

Recovering Accidental Deletions

If you delete text by mistake:

Key Action
u Undo the last change
Ctrl + R Redo (reverse the undo)

Common failure: Deleting text and realizing it was needed. Press u immediately to restore.


Common Questions

Question Answer
My / doesn't search — it just types a slash. You're in Insert Mode. Press Esc to return to Command Mode, then try / again.
How do I search for multiple words? Use space-separated terms: /error code or use regex patterns.
What's the difference between v and V? v selects character by character. V (Shift+v) selects entire lines.
I deleted too much text. Press u to undo. Press multiple times to undo multiple changes.
How do I copy text without deleting it? Select in Visual Mode, then press y (yank), then p (paste) at the destination.
Can I search and replace in vi? Yes: :s/old/new/g replaces all occurrences on the current line. :%s/old/new/g replaces in the entire file.

Wrap-Up

vi text operations extend beyond basic editing. Key takeaways:

  • /term searches forward, ?term searches backward.
  • n and N navigate between search matches.
  • v selects characters, V selects entire lines.
  • d deletes selected text. dd deletes the current line.
  • u undoes changes. Ctrl + R redoes.

Remember: you must be in Command Mode for all these operations. Press Esc if you're unsure which mode you're in.

The DevSecOps Box is stateless — any files you create or edit are lost on page refresh.