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

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=0automatic (default): Vim picks the engine per pattern
  • :set regexpengine=1old NFA engine: deterministic but can be faster for certain patterns involving complex character classes or backtracking-heavy expressions
  • :set regexpengine=2new 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=1 in your vimrc if a particular filetype consistently causes slowdowns
  • Use :setlocal regexpengine=1 inside a FileType autocommand to scope the change to only the problem filetype
  • This option has no effect on Neovim, which uses its own regex engine from the libvim fork and does not expose regexpengine
  • If switching engines does not help, consider :set syntax=off for that file type and relying on Tree-sitter (Neovim) or an LSP for semantic highlighting instead

Next

How do I highlight only the line number of the current line without highlighting the entire line?