1. Why Search and Replace Are Vital in Vi
When working with HDL files, SDC constraints, or simulation logs, you often need to:
- Quickly find specific signals or parameters
- Replace old naming conventions
- Correct log patterns (like FAIL → PASS) across thousands of lines
Vi provides fast and flexible commands for searching and replacing text without leaving the terminal.
2. Searching for Text
| Command | Description |
| /pattern | Search forward for the given pattern |
| ?pattern | Search backward for the given pattern |
| n | Move to the next occurrence |
| N | Move to the previous occurrence |
Example:
/error
This searches forward for the word “error” in your log file.
Press n to find the next occurrence or N to go back.
💡 Use case: Quickly scan for multiple simulation errors in sim.log.
3. Case Sensitivity in Search
By default, Vi searches case-sensitive.
To ignore case while searching:
:set ic
(Use :set noic to disable it later.)
Example:
Search for “fail” or “FAIL” both at once:
/fail
4. Substitution Command Syntax
The general syntax for substitution is:
:[range]s/old/new/[options]
Part Meaning range Lines to apply substitution (e.g., 1,10, %, or .) old Text/pattern to search for new Replacement text options Control behavior (global, confirm, etc.)
5. Common Substitution Examples
| Command | Description |
| :s/old/new/ | Replace first occurrence on current line |
| :s/old/new/g | Replace all occurrences on current line |
| :1,10s/old/new/g | Replace all occurrences between lines 1–10 |
| :%s/old/new/g | Replace throughout the entire file |
| :%s/old/new/gc | Replace with confirmation (asks before each change) |
6. Real-World Substitution Examples
Example 1: Rename signal in RTL file
:%s/data_in/data_bus/g
Replaces all instances of data_in with data_bus in the entire file.
Example 2: Update clock name in constraint file
:100,200s/clk_ref/clk_main/g
Replaces signal name only between lines 100 and 200 — perfect for scoped changes in .sdc or .tcl files.
Example 3: Fix error logs
:%s/FAIL/PASS/gc
Replaces “FAIL” with “PASS” and confirms each change — handy for verification logs.
7. Using Regular Expressions (Regex) in Vi
Vi supports powerful regex-based patterns for advanced search/replace operations.
| Pattern | Meaning | Example |
| ^ | Beginning of line | :^module → lines starting with “module” |
| $ | End of line | /endmodule$ |
| . | Any single character | /b.t → matches bat, bit, bot |
| * | Zero or more of the previous char | /err* → matches er, err, errr… |
| [abc] | Matches a, b, or c | /[ABC]_done |
| [^abc] | Matches any char except a, b, or c | /[^abc]data |
| | | Logical OR | /fail|error → find “fail” or “error” |
8. Complex Search Examples
| Task | Command | Description |
| Find lines starting with “set” | /^set | Useful for constraint scripts |
| Find lines ending with “;” | /;$ | HDL syntax check |
| Find any of “fail”, “warn”, or “down” | /fail|warn|down | Multi-pattern log search |
| Find “fail” followed by “warn” and “down” | /^fail.*warn.*down/ | Multi-match on same line |
9. Replace Using Patterns
You can also use regex in substitution:
:%s/^#.*//
Removes all commented lines (starting with #).
Or:
:%s/\s\+$//
Removes trailing spaces — great for cleaning up formatted code.
10. Practical Use for VLSI Engineers
Scenario:
You want to replace all reset signal names from rst_n to reset_n across 5000 lines of RTL.
:%s/\<rst_n\>/reset_n/g
The \< \> ensures exact word match — avoiding partial replacements like arst_n.
Summary
Search and substitution = ultimate productivity combo.
Once mastered, these commands help you:
- Debug faster in logs
- Edit massive RTL or constraint files efficiently
- Automate replacements with regex power
