vimtricks.wiki Concise Vim tricks, one at a time.

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

  • :snomagic behaves like :substitute but 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 g and c still 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 :smagic when 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/n to preview match counts

Next

How do I trim trailing whitespace in a file without polluting jumplist or search history?