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

How do I mix very-nomagic and magic sections in a single Vim search?

Answer

/\Vsrc/main.c\m:\d\+<CR>

Explanation

Sometimes you need one search pattern that treats a literal path strictly while still using regex power for the suffix. Instead of escaping every punctuation character manually, you can switch regex modes inline: start with \V for a literal-heavy segment, then switch back with \m for a compact regex tail. This keeps the pattern readable and reduces escaping mistakes in large codebases.

How it works

/\Vsrc/main.c\m:\d\+<CR>
  • / starts a forward search
  • \V enables very-nomagic mode, so most characters are treated literally
  • src/main.c is matched as plain text without extra escaping for dots or slashes
  • \m switches back to magic mode for regex atoms
  • :\d\+ matches a colon followed by one or more digits (for line-number style references)
  • <CR> runs the search

Example

Given text like:

see src/main.c:42 for details
see src/main.c:108 for edge case

This pattern finds src/main.c references that include numeric line suffixes, without forcing you to backslash-escape every literal character in the filename.

Tips

  • Inline mode switches are local to the pattern, so you can use multiple transitions in one search
  • Use this style for stack traces, file-path references, or logs with mixed literal and regex fragments

Next

How do I jump to the previous change and reveal it clearly in Vim?