Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enable mouse scrolling but not mouse visual selection in vim?

Tags:

vim

iterm2

I recently enabled mouse mode in vim:

set mouse=a

Which I love for scrolling but is awful for highlighting text. I am using vim in iTerm2 on a Mac. Now when I select text it's very slow to highlight (only highlighting after the highlight dragging is complete) and the highlighted text is not automatically copied into my clipboard, as it used to be when using iTerm2 to select.

Is there a way to allow mouse/trackpad-based scrolling in vim without using the mouse for visual-mode text selection?

I've tried:

set mouse=n

Which prevents the text from getting highlighted when I try and select, but it also prevents iTerm2 from highlighting text.

Update:

Thanks to this post I realized I can get almost the behavior I want by holding down the option key, but this is rather tedious. I would really like not to need to hold down the option key every time I select. Thoughts?

Update2:

Thanks to the suggestions in the comments I was able to get a bit closer. I needed to recompile vim so that +clipboard is enabled:

brew install vim

Now if I'm willing to put up with not seeing my highlight in progress as a select text then I can at least still get it in the clipboard by yanking it after visual-mode selection. And if I really want better selection ergonomics I can hold down alt/option and get the original iTerm2 selection behavior.

I wish I could reverse these modes. For example, default to the behavior that is accomplished by holding down alt/option and enable the visual mode behavior when I want it with a key...but oh well, this is good enough.

like image 636
Kevin Bullaughey Avatar asked Mar 04 '15 20:03

Kevin Bullaughey


People also ask

How do I enable scrolling in Vim?

":set mouse=a" is used to enable mouse scrolling/selection.

How do I scroll in Vim?

You can make Vim scroll the text using the shifted up/down arrows by mapping Shift-Up to Ctrl-Y and Shift-Down to Ctrl-E. Shift-Down will then scroll down (like moving a scroll-bar down, or like moving a cursor at the bottom of a window down), and Shift-Up will then scroll up (like moving a scroll-bar up, etc).

Can you use mouse in Vim?

Mouse reporting is enabled in an app, such as Vim, that runs in a Terminal window. In general, mouse reporting is not enabled in apps by default. For example, to enable mouse reporting in Vim, you need to add a set mouse=a command to your ~/. vimrc file.


2 Answers

Best I've found is to add a toggle for the mouse mode.

Adding the following to .vimrc will switch from mouse=a to mouse= and back again when the , key is pressed:

noremap , :if &mouse == "" \| set mouse=a \| else \| set mouse=  \| endif<CR><CR>
like image 95
Armand Avatar answered Oct 27 '22 05:10

Armand


With your update you say you can almost get what you want, but that the selection is not copied automatically.

For that, you just need set clipboard+=autoselect guioptions+=a

See :help clipboard-autoselect for details.

like image 34
Ben Avatar answered Oct 27 '22 04:10

Ben