Syntax highlighting causes terrible lag in Vim

Vim

Vim Problem Overview


I love Vim. But its giving me hard times right now.

I use a lot of plugins and during the last 6 months, I've found a lot of awesome ones. But my Vim got really sluggish too. I do constant cleanups, but it doesn't help much.

I'm at the point, where Vim is completely unusable. It feels like it renders at 2-5 frames per second, switching tabs/buffers takes about a second, scrolling with hjkl is awfully terrible, the lag is so bad, even typing a sentence in insert mode is confusing (due to lag).

Edit: Actually, when I open fresh instance of Vim its OK-ish, but than within 15 minutes it becomes unusable.

I've just spent 4 hours trying to figure out which plugin or config is causing the pain. I was unsuccessful.

However, I did find out, that removal of this setting causes all the lag to go away: syntax on

These 3 lines in conjunction with syntax make everything even worse.

set t_Co=256
set background=dark
colorscheme candyman

Interesting. So, syntax highlighting is turning Vim from super snappy to incredibly sluggish?

I tried enabling syntax in "clean" mode: vim -u NONE

And its not an issue there.

So what seems to be the issue is Syntax Highlighting in combination with one or more of my plugins. I tried disabling bunch, no luck.

Is there any way to do profiling? I'm fairly exhausted from manual testing.

Has anyone had similar experience? Maybe take a quick peek into my .vimrc, see if anything rings a bell. https://bitbucket.org/furion/dotfiles

SOLUTION: The plugin causing the mess was:

Bundle "gorodinskiy/vim-coloresque.git"

I recommend reading the answers tho, good insights.

Edit (1 month later): The coloresque plugin has seen some improvements.

Vim Solutions


Solution 1 - Vim

EDIT: Blogged about how this all works, with screenshots and awesome-sauce.

https://eduncan911.com/software/fix-slow-scrolling-in-vim-and-neovim.html

Original answer below...


:syntime on

move around in your ruby file and then

:syntime report

It reported the following slowest matching for me, and you can see that there are not even 1 match.

I disabled rubyPredefinedConstant in ruby.vim file and problem solved. Vim regex engine does not like something in ruby syntax highlight regex. You will have to find the balance between enough syntax highligting and a good performance.

hope that helps, here is the top 3 slowest syntax highlighting regex for ruby reported on my Mac OS 10.8.5, homebrew Vim 7.4 (console vim)

    TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
  3.498505   12494  0       0.008359    0.000280  rubyPredefinedConstant \%(\%(\.\@<!\.\)\@<!\|::\)\_s*\zs\%(STDERR\|STDIN\|STDOUT\|TOPLEVEL_BINDING\|TRUE\)\>\%(\s*(\)\@!
  2.948513   12494  0       0.006798    0.000236  rubyPredefinedConstant \%(\%(\.\@<!\.\)\@<!\|::\)\_s*\zs\%(MatchingData\|ARGF\|ARGV\|ENV\)\>\%(\s*(\)\@!
  2.438253   12494  0       0.005346    0.000195  rubyPredefinedConstant \%(\%(\.\@<!\.\)\@<!\|::\)\_s*\zs\%(DATA\|FALSE\|NIL\)\>\%(\s*(\)\@!

Or you can try vim-ruby as pointed out by Dojosto

Solution 2 - Vim

You have autocmd spam. You should wrap all of your autocmd statements in groups which clear the group before re-adding the autocmds. It looks like your .vimrc has most autocmds commented-out, so maybe there is a plugin that is causing the issue. Check the output of this command:

:au CursorMoved

If there's a bunch of duplicate handlers there, that's your problem.

Here's an example of autocmd discipline from my .vimrc:

augroup vimrc_autocmd
  autocmd!
  "toggle quickfix window
  autocmd BufReadPost quickfix map <buffer> <leader>qq :cclose<cr>|map <buffer> <c-p> <up>|map <buffer> <c-n> <down>

  autocmd FileType unite call s:unite_settings()
  " obliterate unite buffers (marks especially).
  autocmd BufLeave \[unite\]* if "nofile" ==# &buftype | setlocal bufhidden=wipe | endif

  " Jump to the last position when reopening a file
  autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif

  " ...etc...

augroup END

The autocmd! at the beginning of the augroup block clears out the current group (vimrc_autocmd, in this case) before re-adding the autocmds.

Solution 3 - Vim

From another stack overflow question, I get vim fast by adding the following line in .vimrc file:
set re=1
This will force vim to use a older version of regex engine and it is actually FASTER for ruby.

Solution 4 - Vim

I found that "set foldmethod=syntax" makes 7.4 almost unusable slow e.g. for js&ruby files (ubuntu 13.10) whereas "set foldmethod=indent" works fine.

Solution 5 - Vim

I've noticed that vim can slow down to a halt if you're using anything that dynamically changes the background color. Try turning off :set cursorline or :set cursorcolumn (if you have them set).

Solution 6 - Vim

I'd like to thank everyone helping me out on this issue. Good news is, my Vim is snappy again.

I've started with fresh Vim re-install. I've than added plugin by plugin, until I found the root of all evil.

Bundle "gorodinskiy/vim-coloresque.git"

Its a plugin that was causing me all this pain. Since I had it for a while, it wasn't a suspect, thats why I discovered it so late. What this plugin does, is whenever it finds a word for color (eg red, green), or hex value (eg. #FFFFFF), it sets background color of the text to match the color that its describing. Brilliant idea, but seems like poor implementation.

Removing this plugin removed the lags.

But I didn't stop here. I've done a major cleanup of my .vimrc as well. Removed some more plugins I hadn't used. Grouped my autocmds and removed unnecessary ones.

My Vim is very snappy now. I'm happy again.

Solution 7 - Vim

Syntax highlighting can be slow, but that should be limited to some (somewhat pathological) files, and particular syntax(es). The latest Vim 7.4 has a new command :syntime to troubleshoot syntax highlighting slowness.

Apart from that, often, a binary search where you disable half of your plugins, then only one half of that (when the problem is still there), or the other half (when the problem vanished) lets you get to the problematic script quickly.

Solution 8 - Vim

I'm pretty sure that

set t_Co=256
set background=dark
colorscheme candyman

have nothing to do with that lag. The two first lines are useless (the number of usable colors is defined according to your $TERM and your colorscheme already does set background=dark) but not really harmful.

Common "Vim is slowing to a crawl" causes include poorly written autocmds, too many autocmds, reloading one's ~/.vimrc too often, poorly written plugins…

Please post your setup so that we can help you find out why you experience that lag.

Solution 9 - Vim

I have had this problem for a long time, and it was driving me crazy. I tried installing vim-ruby. Not sure if that helped but at least now I have the most up-to-date version of ruby syntax highlighting (with any performance improvements since the last version of Vim was released).

But then I looked further, and discovered that vim-ruby has a mode that skips all the expensive highlighting. Try adding this line to your vimrc and see if it helps (it did for me, finally!):

let ruby_no_expensive=1

Solution 10 - Vim

For me that was set relativenumber feature which was slowing it down. Try to disable it with set norelativenumber and test.

Solution 11 - Vim

Just in case it helps anyone out there:

I had accidentally created an extremely large tag file for my project; so my syntax highlighter was searching the large file to highlight function names.

So check your tag file! (In my setup I was using easy-tags so mine was placed at ~/.vimtags

Solution 12 - Vim

Very late, but I figured this may help someone in a similar boat as me.

The plugin vim-nerdtree-syntax-highlight turned out to be lagging my entire editor. They do mention on their Github that people have reported lag issues, and offer some solutions.

So if anyone happens to be using that plugin, try using some of the fixes listed; this one works, but you need to be selective about which languages you want to keep:

let g:NERDTreeSyntaxDisableDefaultExtensions = 1
let g:NERDTreeDisableExactMatchHighlight = 1
let g:NERDTreeDisablePatternMatchHighlight = 1
let g:NERDTreeSyntaxEnabledExtensions = ['c', 'h', 'c++', 'php', 'rb', 'js', 'css']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionif __name__ is NoneView Question on Stackoverflow
Solution 1 - VimAsk and LearnView Answer on Stackoverflow
Solution 2 - VimJustin M. KeyesView Answer on Stackoverflow
Solution 3 - VimChun YangView Answer on Stackoverflow
Solution 4 - VimfwoeckView Answer on Stackoverflow
Solution 5 - VimJohn PenaView Answer on Stackoverflow
Solution 6 - Vimif __name__ is NoneView Answer on Stackoverflow
Solution 7 - VimIngo KarkatView Answer on Stackoverflow
Solution 8 - VimromainlView Answer on Stackoverflow
Solution 9 - VimDerek KraanView Answer on Stackoverflow
Solution 10 - VimAndrey ChernihView Answer on Stackoverflow
Solution 11 - VimNahin KhanView Answer on Stackoverflow
Solution 12 - VimShayan RashidView Answer on Stackoverflow