Note: I have not tested this with gvim, only with normal vim
This piece of code allows me to scroll instead of selecting text when I drag using my mouse.
function! MouseScroll()
"mark b is the current cursor position
"mark a is the previous cursor position
norm mb
let currPos=line('.')
norm `a
let prevPos=line('.')
if currPos>prevPos
norm `bma
" norm ^E (This is hard to typeset)
" Adopting the following exe command to achieve the same motion.
exe "normal! \<C-e>"
elseif currPos<prevPos
norm `bma
" norm ^Y
exe "normal! \<C-y>"
endif
" Need this additional line to "scroll" continuously.
" Otherwise, I was prompted "marker not set" warnings when dragging
" for more than 1 line.
norm ma
endfunction
map <LeftDrag> ma<LeftMouse>:call MouseScroll()<cr>
and add this for insert mode:
imap <LeftDrag> <ESC>ma<LeftMouse>:call MouseScroll()<cr>:norm gi<cr>
Note: ^E
and ^Y
are not 2 characters each, they are each one character literals
I am using marks to check if I'm dragging up or down. Then accordingly I either scroll up or down.
Explanation
map <LeftDrag> ma<LeftMouse>:call MouseScroll()<cr>
The first thing we do when we map LeftDrag
is that we set the cursor's mark (this will be our previous mark) to mark a
. Mark a
contains the previous position of the cursor to compare with the current position and scroll accordingly. Then we click. This is because we want to get the current position of the mouse in the buffer (because mouse drags don't usually move the mouse to the updated position). Then we call the MouseScroll
function.
Inside the function
norm mb
let currPos=line('.')
norm `a
let prevPos=line('.')
Now we get the current position of the cursor using norm mb
. Mark b
will contain the mark of the current cursor position.
We store the positions of the position of these marks in variables currPos
and prevPos
Then inside the if-elseif statement, we compare the currPos
with the prevPos
. If the current position is more than the previous position, ie the mouse is dragged downwards, we change mark b
into mark a
and then scroll down using norm ^E
. We do a similar thing for the other case when the current position is below the previous position (the mouse is dragged upwards), except that we scroll up.
And that's it :)
Additional Configurations
Sometimes dragging the mouse still enters visual mode (at least for me it does), so I have a set mouse=nic
. It's your choice on what you want to set the mouse to. Also, you can swap the ^E
and the ^Y
depending on your scroll preferences.