CLI Toolbox: Finding, Searching and Previewing Replacements
On this page
A useful command-line workflow narrows the scope, inspects content and then acts. Run these examples from a project root. Search and preview commands do not modify files; generation steps are identified separately.
Find names, then search content
List TOML files by extension:
Or list searchable paths matching a glob:
Search their content for a literal string:
-n adds line numbers, -F selects literal matching and -g limits paths. Remove -F when you intend a regular expression. File scope and content pattern are separate choices.
Check scope before concluding something is absent
rg normally excludes hidden paths, ignored files and binary content. fd also filters hidden and ignored paths but uses different flags.
| Need | rg | fd |
|---|---|---|
| Hidden paths | --hidden | -H |
| Bypass ignore rules | --no-ignore | -I |
| Extension | -g '*.rs' or -t rust | -e rs |
| Content context | -C 3 | Not a filename-search operation |
Include hidden and ignored content while excluding Git objects:
Broader scope can expose generated files, caches and local configuration. Limit directories or extensions before expanding it. Exit code 1 means no match; 2 indicates an error. ripgrep guide
Inspect context and preview replacements
The src directory must exist. Context helps distinguish identical names in different scopes.
-r changes displayed matches; it does not write files. Before a real replacement, distinguish comments, strings, generated files and unrelated symbols. Prefer a language-aware rename for code identifiers, then inspect Git diff.
Preview files and trees
Explicitly disabling paging, decoration and colour makes output predictable in pipelines; defaults can depend on terminal detection and user configuration.
The depth limits output. --git-ignore explicitly requests Git ignore filtering; tree mode alone is not that guarantee.
For per-file execution, fd passes paths as arguments:
This counts lines without modifying files. Before substituting a destructive command, print and inspect the target arguments. Do not parse formatted ls output into filenames.
Review Git changes with delta
After installing delta, these Git settings improve presentation:
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true
line-numbers = true
[merge]
conflictStyle = zdiff3
[diff]
colorMoved = default
delta changes display, not stored differences. zdiff3 includes common-ancestor context in conflict markers. colorMoved helps identify moved blocks but does not prove their semantics are unchanged.
Use git diff for unstaged work and git diff --cached for staged content. If paging fails, git --no-pager diff separates a display problem from a repository problem. See the Git handbook.
Directory history and project commands
After Zsh initialisation, zoxide's z project jumps by history and keywords; zi allows interactive selection. Confirm with pwd when projects have similar names. zoxide
For an existing Cargo project, save this as justfile:
:
:
just --list lists tasks and just test runs tests. just does not calculate rebuilds from file timestamps; Cargo remains responsible for incremental builds. A shared task entry point keeps terminal, editor and CI commands aligned. just manual
Verify your understanding
In a disposable repository, put the same string in an ordinary, hidden and ignored file. Compare default rg with --hidden and --no-ignore, then preview a replacement and confirm the files remain unchanged. Explain why each result set changed before applying the technique to a large repository.