0

I use (neo)vim with mouse=a and scrolloff=5. Clicking on one of the top or bottom five lines with the mouse causes the screen to jump (so that the new cursor position is more centered), which distracts me. Is there any way to disable "so=5" for mouse-clicking only?

5
  • I'm not sure how that could work: scrolloff set a minimal number of lines below or above the cursor line. Disabling the option "only for mouse click" would mean that you don't have this minimal number of lines between the cursor line and the edge of the screen but how would vim know when to set this minimal number of lines again? – statox Apr 7 '19 at 17:25
  • @statox preferably at the next vertical movement – 0x539 Apr 7 '19 at 17:25
  • 1
    I'm pretty sure it's not possible to do what you want. You could try to use a mapping like nnoremap <leftmouse> :set scrolloff=0<CR><leftmouse> to disable scrolloff when you click and then remap the motion you want to reenable the option but that seems very impractical. – statox Apr 7 '19 at 17:29
  • Do you need the answer to be a setting in vimrc or you need it to be a mouse-and-keyboard combination? – tivn Jun 22 '19 at 12:50
  • @tivn preferably a vimrc setting, but a mouse-and-keyboard combination would help too. – 0x539 Jun 22 '19 at 13:18
0

Try the following: hold down shift key before you press mouse button.

6
  • Since I use neovim in the terminal this results in my terminal trying to select text. – 0x539 Jun 22 '19 at 14:23
  • what’s the purpose you need to click there? – tivn Jun 22 '19 at 14:29
  • What’s the purpose of “Clicking on one of the top or bottom five lines” ? – tivn Jun 22 '19 at 14:33
  • usually the purpose is to move the cursor to that line. – 0x539 Jun 22 '19 at 14:34
  • That’s of course would not possible :-) – tivn Jun 22 '19 at 14:35
0

If you want to avoid test scrolling when you mouse-click at edges of the window, you can simply set scrolloff to 0, see comments in /usr/share/vim/vim80/defaults.vim(ubuntu 1804):

" Show a few lines of context around the cursor.  Note that this makes the
" text scroll if you mouse-click near the start or end of the window.
set scrolloff=5

Note that you have to set scrolloff=0 after sourcing the above defaults.vim if you are using .vimrc copied from vimrc_example.vim, on Ubuntu for example, set it like:

" Get the defaults that most users want.
source $VIMRUNTIME/defaults.vim
set scrolloff=0
1
  • Hi, welcome! The OP wants to set scrolloff to a non-zero value. Thus, I'm not quite sure your answer is relevant. – Biggybi Feb 23 at 12:23
0

This occurs to me:

nnoremap <leftmouse> :let oldso = &scrolloff <bar> let &scrolloff = 0<CR><leftmouse>:let &scrolloff = oldso <bar> unlet oldso<CR>

Save the old value, set to 0; do the mouse click; restore the value.

2
  • This seems to produce identical results to regular <leftmouse>. Am I missing something? – 0x539 Mar 25 at 19:50
  • I see the problem @0x539 : because scrolloff is restored immediately afterwards, there is still a jump. You could leave off the restore and define another mapping/command to restore (e.g., command RestoreScroll let &scrolloff = oldso | unlet oldso), but you would have to call it yourself when you wanted it. – D. Ben Knoble Mar 25 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.