Post 4: Search and Substitution in Vi Editor


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

CommandDescription
/patternSearch forward for the given pattern
?patternSearch backward for the given pattern
nMove to the next occurrence
NMove 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]

PartMeaning
rangeLines to apply substitution (e.g., 1,10, %, or .)
oldText/pattern to search for
newReplacement text
optionsControl behavior (global, confirm, etc.)

5. Common Substitution Examples

CommandDescription
:s/old/new/Replace first occurrence on current line
:s/old/new/gReplace all occurrences on current line
:1,10s/old/new/gReplace all occurrences between lines 1–10
:%s/old/new/gReplace throughout the entire file
:%s/old/new/gcReplace 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.

PatternMeaningExample
^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

TaskCommandDescription
Find lines starting with “set”/^setUseful for constraint scripts
Find lines ending with “;”/;$HDL syntax check
Find any of “fail”, “warn”, or “down”/fail|warn|downMulti-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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top