简体   繁体   中英

Vim - make click exit insert mode

There is nothing I hate more than phantom touchpad clicks causing my text to get entered somewhere random. I would almost rather have the typing be interpreted as random Vim commands. At any rate, I do have configuration that makes leaving insert mode highly conspicuous visually, so that shall be a non-issue.

How should I do this? I don't think there's a way to map or intercept mouse events in Vim. I am hoping maybe there is an autocmd of some sort that fires on clicks?

I am only talking about command line Vim here. Not MacVim or some such.

You can also map the mouse to do nothing at all:

inoremap <LeftMouse> <Nop>

Edit by OP: This little bit of insight was the perfect solution to the problem.

I prefer to make damn sure the left click is ignored when in insert mode. The reason that I like this answer so much is that it delivers the one-two punch of not only preventing the movement of the cursor (to cause my text to appear where i randomly phantom-clicked), but it also prevents Vim from causing those keys i was in the middle of typing to be interpreted as Vim commands, which is what would happen when left click is bound to <ESC> .

However I found sometimes I would stubbornly keep hammering on the mouse to change window while still in insert mode even though I set my entire status bar to change color in insert mode , mostly because I'm stupid, and the default bindings of double, triple, and quad left clicks can still trigger despite the single click map, because in this sort of situation I just mash the button I was hitting (in this case left mouse) instinctively, a behavior ingrained by flaky/laggy network connections (which actually still doesn't make any sense considering how TCP functions...)

So, to address that issue and configure Vim so that it will force me to realize that I'm in insert mode, here is an even more bulletproof set of binds. It clears out all the default functionality of left clicks causing various visual mode selections to go into effect (which if I go on to cancel will still drop me back in insert mode somewhere else, the original behavior I intended to correct):

inoremap <LeftMouse> <Nop>   "normally causes visual selection mode
inoremap <2-LeftMouse> <Nop> "normally causes visual word selection mode
inoremap <3-LeftMouse> <Nop> "normally causes visual line selection mode
inoremap <4-LeftMouse> <Nop> "normally causes visual block selection mode

With :set mouse=a (or at least :set mouse=i ), you can use the following mapping:

inoremap <LeftMouse> <Esc>

See :help mouse and :help mouse-using for more information.

I used Nikita Kouevda's version but with a small tweak to avoid clicking more than once:

inoremap <LeftMouse> <Esc><LeftMouse>

This takes you out and then performs the default action. As with his the mouse mode needs to be:

:set mouse=a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM