How do I switch to a faster regex engine in Vim when syntax highlighting is causing slowdowns?
Answer
:set regexpengine=1
Explanation
Vim ships with two regex engines and lets you control which one is used. In rare cases — particularly with complex syntax files or unusual patterns — switching engines can eliminate severe performance bottlenecks that make Vim feel sluggish during normal editing.
How it works
:set regexpengine=0— automatic (default): Vim picks the engine per pattern:set regexpengine=1— old NFA engine: deterministic but can be faster for certain patterns involving complex character classes or backtracking-heavy expressions:set regexpengine=2— new NFA engine: generally faster and more feature-rich, but can exhibit pathological slowness with particular syntax patterns (especially in Ruby, Haskell, or heavily-nested regex files)
The symptom of an engine mismatch is noticeable lag while scrolling or typing in large files, even with syntax highlighting otherwise enabled and working correctly.
Example
If you open a large Ruby or TypeScript file and notice Vim hanging for a second on each keypress:
" Try forcing the old engine
:set regexpengine=1
" Or profile which syntax rules are the bottleneck first
:syntime on
" ...scroll around...
:syntime report
A :syntime report will show which regex patterns are consuming the most time, and you can then decide whether switching engines or disabling the offending syntax group is the better fix.
Tips
- Put
set regexpengine=1in yourvimrcif a particular filetype consistently causes slowdowns - Use
:setlocal regexpengine=1inside aFileTypeautocommand to scope the change to only the problem filetype - This option has no effect on Neovim, which uses its own regex engine from the
libvimfork and does not exposeregexpengine - If switching engines does not help, consider
:set syntax=offfor that file type and relying on Tree-sitter (Neovim) or an LSP for semantic highlighting instead