[vim/vim] Vim is slow when using the CursorMoved event for C++ files only (#8981)

118 views
Skip to first unread message

Andres Rodriguez

unread,
Oct 8, 2021, 1:33:18 PM10/8/21
to vim/vim, Subscribed

Describe the bug
When I use the CursorMoved or CursorMovedI event, Vim becomes slow when moving the cursor only for cpp or hpp files. Not sure if the file size matters. For small files sometimes Vim will be slow but it seems Vim is always slow for large files.

This behavior does not affect the following file types:

  • css
  • html
  • js
  • json
  • makefile
  • md
  • py
  • scss
  • sh
  • svg
  • ts
  • txt
  • vim

To Reproduce
Can't use vim --clean to reproduce the bug. But here is how to reproduce the bug just add the following to your vimrc and open a C++ file.

fun! g:CenterCursor()
    let i = line('$') - line('.')    
    if i < 30
        execute "set scrolloff=" . i
    else
        set scrolloff=999
    endif
endfun

augroup Start
    "other autocmds ...
    autocmd CursorMoved * call CenterCursor()
    autocmd CursorMovedI * call CenterCursor()
augroup END 

Expected behavior
For Vim not to be slow when moving the cursor for any file type.

Environment (please complete the following information):

  • Vim version: 8.1.2269
  • OS: Windows 10 21H1
  • Terminal: Windows Terminal WSL 2


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

bfrg

unread,
Oct 8, 2021, 1:48:57 PM10/8/21
to vim/vim, Subscribed

Do you use any 3rd party plugins for C++?

Andres Rodriguez

unread,
Oct 8, 2021, 2:07:41 PM10/8/21
to vim/vim, Subscribed

@bfrg nope

bfrg

unread,
Oct 8, 2021, 2:18:53 PM10/8/21
to vim/vim, Subscribed

Why are you resetting scrolloff on every CursorMoved /CursorMovedI? Just set it once in your vimrc.

Andres Rodriguez

unread,
Oct 8, 2021, 2:27:08 PM10/8/21
to vim/vim, Subscribed

I like having scrolloff set to 999 and when I am at the bottom of a file I like to use shortcuts zz and Ctrl+E to center the line I'm on. But when I move the cursor to a different line this causes nasty behavior. So I created CenterCursor() and it gives me a much smoother experience when editing files at the bottom of them.

Andres Rodriguez

unread,
Oct 8, 2021, 2:32:32 PM10/8/21
to vim/vim, Subscribed

if I make CenterCursor() to

fun! g:CenterCursor()
    set showcmd
    set noshowmode
endfun

There is no slowness, the moment I set it to

fun! g:CenterCursor()
    set scrolloff=999 "or any number
endfun

The slowness behavior starts for cpp files only.

Christian Brabandt

unread,
Oct 8, 2021, 3:29:31 PM10/8/21
to vim/vim, Subscribed

how exactly do you scroll?

Christian Brabandt

unread,
Oct 8, 2021, 3:32:59 PM10/8/21
to vim/vim, Subscribed

Does it help, if you only set the scrolloff value, if it actually differs from the old version?

Andres Rodriguez

unread,
Oct 8, 2021, 5:47:41 PM10/8/21
to vim/vim, Subscribed

@chrisbra I scroll with hjkl and at the bottom of a file, I use Ctrl+E and zz.

So I did what you suggested. When I scroll it's a little laggy with C++ files, not as smooth when scrolling on other file types. But this won't solve slowness when scrolling at the bottom of the file though.

set scrolloff=999
let g:const = 999
fun! g:CenterCursor()
    let i = line('$') - line('.')
    if i < 30
        execute "set scrolloff=" . i
        let g:const = i
    else
        if g:const != 999
            set scrolloff=999
            let g:const = 999
        endif
    endif
endfun

Andres Rodriguez

unread,
Oct 8, 2021, 5:54:19 PM10/8/21
to vim/vim, Subscribed

hmmm if I make CenterCursor() like this.

fun! g:CenterCursor()
    let i = line('$') - line('.')
endfun

Scrolling inside cpp file is also laggy. But like I said above the set scrolloff is what causing the most lag.

Andres Rodriguez

unread,
Oct 8, 2021, 6:25:54 PM10/8/21
to vim/vim, Subscribed

I figured it out! This plugin https://github.com/frazrepo/vim-rainbow is somehow fighting against CenterCursor(). Not sure how though since the plugin only have one auto command au syntax,colorscheme * call rainbow#load().

Bram Moolenaar

unread,
Oct 9, 2021, 5:43:32 AM10/9/21
to vim/vim, Subscribed

So it is not a problem with Vim, closing.

Bram Moolenaar

unread,
Oct 9, 2021, 5:43:32 AM10/9/21
to vim/vim, Subscribed

Closed #8981.

Reply all
Reply to author
Forward
0 new messages