When I select some text in Visual mode (either with keyboard or mouse), and I scroll the mouse wheel up/down, the visual mode expands the selection in up/down direction according to the mouse scrolling, and I don't want this behavior (I want to expand it manually).

I tried the following remapping:

:vnoremap <ScrollWheelUp> gv5k
:vnoremap <ScrollWheelDown> gv5j

but works only for the first try (select text and scroll. If I select another text and scroll again, selection will be expanded), in addition, mouse must be disabled for visual mode. So this setting is not what I want.

Is there a way to do not expand the text selection with the mouse wheel, leaving it enabled for Visual mode selection also ?

  • What operating system are you using? Terminal or GUI? The behaviour you want is how it works for me everywhere I've tested. Have you tried :vnoremap <ScrollWheelUp> <C-Y> and :vnoremap <ScrollWheelDown>? – Rich Jan 24 at 10:31
  • Hi. I am using gvim on Linux. The default behavior behaves correctly until the selected text is in the scope of the text you are viewing. Suppose the visible range of the lines in the buffer is [1,28] and the selected text is in range [10,17]. If now I scroll the mouse so the range of visible lines is [40,67], and then I scroll back to view the text in range [10,17], the selection disappears or it's being altered (i.e. other text has been re-selected due to the scroll). The mapping you suggests does not solve the problem. – simo-zz Jan 24 at 15:06
up vote 1 down vote accepted

The behaviour you request is essentially impossible to achieve in Vim, because of two immutable facts:

  1. When in visual mode, the cursor is always at one end of the selection. With the exception of jumping to the other end of the selection by pressing o, if you move the cursor, you are by definition altering the selection,

  2. The cursor is always visible on screen. If you scroll the text so that the cursor would be off-screen, the cursor moves so that it remains on-screen.

Therefore, if you scroll so that the entire original selection is off-screen, you must also be altering the selection.

You could write Vimscript that detects when you are about to scroll off-screen with the mouse, removes the selection, and then also detects when you scroll back and replaces it, but it would be a fair sized chunk of exceedingly hacky code, and there are a large number of edge cases it would have to deal with.

e.g. Would you still expect to be able to press d to delete the conceptual selection while it is offscreen? If so, then even more hacky code is required. You also want to be able to press c to change it? More code. etc.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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