Reading — step 1 of 5
Read
~1 min readProduction
Search & Replace
Find next match:
python
For huge buffers, scan in chunks (don't materialize whole string).
Regex search:
- Use
remodule. - For interactive: compile pattern, scan buffer.
- Multi-line:
re.MULTILINEflag.
Live search:
- As user types, find first match starting from current cursor.
- Highlight matches.
- Common: vim's
/pattern.
Replace:
- Find + insert+delete.
- Optional: confirm each, replace all, replace in selection.
- Undo: each replacement is one entry, OR group all into one undoable.
Find in files (project-wide):
- Walk file tree.
- For each file, search.
- Parallelize across files.
- Modern editors index workspaces (LSP, Tree-sitter).
Fuzzy find:
- Match approximate sub-sequences.
- Used for file picker, command palette.
- Algorithms: FZF (Floyd-Warshall-like), proximity scoring.
Incremental search:
- Vim's /pattern: go to first match as user types.
- VS Code Ctrl+F: live highlight.
- Ctrl+G or F3: next.
Search history:
- Recent searches accessible via up arrow.
- Persistent across sessions.
Selection-based search:
- Highlight selected text → Ctrl+F places it in find input.
- Or Ctrl+D to add next occurrence to multi-cursor (Sublime).
Code-aware search:
- Symbol search via LSP.
- Definition / references / implementations.
- Faster than text search, semantically aware.
Performance:
- Naive O(n*m) substring search slow for huge files.
- Use Boyer-Moore, KMP, or std string find (often optimized).
- For regex on huge files: streaming line-by-line.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…