How do I run one substitution in nomagic mode so most regex characters are literal?
Answer
:snomagic /foo.\+/bar/<CR>
Explanation
When a pattern is mostly literal text with just a little regex, default magic mode can force extra escaping and make substitutions harder to read. :snomagic flips the regex flavor for that one command so metacharacters are mostly treated literally unless escaped.
This is useful for quick edits on dotted identifiers, file-like strings, or punctuation-heavy text where you want fewer surprises from regex parsing.
How it works
:snomagicbehaves like:substitutebut with nomagic parsing- Most characters are literal unless you escape them
- In
foo.\+, the dot is literal and\+is an explicit quantifier - Flags such as
gandcstill work the same way
The main advantage is readability: one-off substitutions are easier to reason about because special behavior is opt-in.
Example
Given:
foo....
foo..
foo.
Run:
:snomagic /foo.\+/bar/<CR>
Result:
bar
bar
bar
Tips
- Use
:smagicwhen you want the opposite behavior - For shared scripts, keep escaping explicit so other users can audit the pattern quickly
- If you are uncertain, test first with
:snomagic /pattern/repl/nto preview match counts