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
- Searching for Text with
/ - Searching Backward with
? - Navigating Multiple Occurrences
- Visual Character Mode (
v) - Visual Line Mode (
V) - Deleting Selected Text
- Deleting Entire Lines with
dd - Undo and Redo
- 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:
/High→ finds first "High"n→ jumps to second "High"n→ jumps to third "High"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:
/termsearches forward,?termsearches backward.nandNnavigate between search matches.vselects characters,Vselects entire lines.ddeletes selected text.dddeletes the current line.uundoes changes.Ctrl + Rredoes.
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.