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.

网友答案:

short answer: I believe it's not possible!

But it is usually hard to prove the absence of an answer. So I'll try to give my thought on that, and I hope for you that I'm wrong. So here goes the long answer:

Your terminal on "normal" conditions, handles the mouse and the command line application doesn't know anything about that. But sometimes, an application requests control of the terminal capabilities to be able to bind mouse events to do "stuff".

It's what happens when you set mouse=a: you change your terminal capabilities to tell that all mouse events are directed to the running application, so then it's up to vim to bind or not the actions (such as scrolling, selecting, pasting etc..).

So when you're using the option key to be able to select even though the running app has control of the cursor, this is a hack on the terminal emulator (iTerm2 in your case) to get the behaviour you're actually expecting.

My advice to you would be to switch to macvim (brew install macvim) which installs a gui frontend for vim. And then you'll have selection that works just fine, though it's just out of the terminal.

网友答案:

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.

网友答案:

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: