Vim: How can I disable the mouse in Operator-Pending Mode? - vim

I have a problem: When I initiate Operator-Pending Mode with d or c and I click on the active window, the mouse click acts as a motion. For example, I can position my cursor at the start of a paragraph, press d, then click on a word at the end of the paragraph and Vim will delete from the cursor position to the end of the paragraph. I would like to disable this feature to prevent accidental mouse clicks.
I have added the following in my .vimrc, which tells Vim to enable the mouse only in Normal Mode and Visual Mode.
set mouse=nv
However, this does not work. I can enter Operator-Pending Mode and still use a mouse click as the motion. Is it possible to tell Vim not to accept mouse clicks for motions in Operator-Pending Mode?

:help 'mouse' won't help, here, so you can set it to a or any desired value.
You could disable the left mouse in operator-pending mode, though:
onoremap <LeftMouse> <Nop>
which prevents mouse clicks motions in operator-pending mode. Note that, with that mapping, a mouse click is not considered as a motion anymore but it also sort of "breaks out" of operator-pending mode, which may or may not be a problem.

Related

iTerm2 cursor scrolling with mouse enabled in vim

iTerm2 has a very nice feature that allows, when scrolling the mouse, to move the cursor up and down.
However, if I enable the mouse in vim with :set mouse=a, the scrolling behavior changes: it now scrolls the file but does not move the cursor until the cursor gets out of view.
Is there a way to keep the iTerm2 scrolling along with mouse enabled in vim?
Does this help?
map <ScrollWheelDown> j
map <ScrollWheelUp> k
Have a look at :help scroll-mouse-wheel.

vim 'a' key meaning in normal mode

In vim, when you type a in normal mode, you go into insert mode after cursor. But daw will delete word under cursor and caw will change word under cursor. I use these combinations very often, it very helpful. But what does a mean in this context and can it be useful in other cases?
Vim being a (the?) modal editor, the keys on your keyboard have different meanings depending on which mode you are in.
In normal mode, a means "append". It enters insert mode and whatever key you press after that is inserted in your buffer.
In normal mode, c, d and y are called "operators". When you press one of those keys, you quit normal mode and enter another mode called "operator-pending mode" where Vim waits for you to feed it a motion or text-object.
In operator-pending mode, a single a means nothing but aw is one of those text-objects and could be translated as "around word". There's also iw for "inner word" or at for "around tag" and many others…
Reference:
:help vim-modes
:help navigation
and more specifically
:help operator
:help text-objects
:help word-motions and following

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

How do I scroll up/down without leaving insert mode?

In Vim normal mode, you can press ctrl+e and ctrl+y to scroll down and up, respectively. I'm trying to make a key-bind that lets me do this from insert mode as well. This is what I've got:
" Scroll up and down while in insert mode.
inoremap <C-e> <C-o><C-e>
inoremap <C-y> <C-o><C-y>
This works like expected, but it has a big flaw. It leaves insert mode, scrolls, then re-enters insert mode. This is relevant when it comes to undo, repeat command etc. and I would like to be able to scroll up and down without leaving insert mode. Thoughts?
You could take a look at :h i_CTRL-X_CTRL-E, which is a built-in insert-mode mapping to scroll:
*i_CTRL-X_CTRL-E*
CTRL-X CTRL-E scroll window one line up.
When doing completion look here: |complete_CTRL-E|
*i_CTRL-X_CTRL-Y*
CTRL-X CTRL-Y scroll window one line down.
When doing completion look here: |complete_CTRL-Y|
So in your case, this would probably do the trick:
inoremap <C-e> <C-x><C-e>
inoremap <C-y> <C-x><C-y>
undojoin fixes the undo part of it:
ino <C-E> <Space><BS><ESC><C-E>:undojoin<CR>gi
The <Space><BS> sequence makes sure there's an undo block to join with.
Surprisingly (to me) this doesn't help with the . breakage, so this might leave you in just as annoying a spot as you're in now...

How do I press "<C-L> <C-W>l<C-W>_" in Vim?

In one of the vim config files I have noticed this keyboard mapping
map <C-L> <C-W>l<C-W>_ supposedly for easier moving in tabs and windows. What does that translate to keyboard presses? What is that underscore at the end for?
The command map <C-L> <C-W>l<C-W>_ maps Ctrl-L to Ctrl-W, l, Ctrl-W, _.
You invoke this binding by just pressing Ctrl-L. To invoke what it binds to you would type Ctrl-W, then l, followed by Ctrl-W again, and finally _ (which on a US keyboard is shift-hyphen). This is two separate bindings, <C-W>l moves the cursor to the window to the right, and <C-W>_ resizes current window to the maximum possible vertical size.
The Ctrl+wlCtrl+w_ keys sequence is somewhat too long so someone has created a shortcut ("mapping" in Vim-speak): Ctrl+L for it.
<C-w>l<C-w>_ moves the the cursor to the window on the right (<C-w>l) and maximizes it vertically (<C-w>_).
Mappings always follow the same structure:
map (or imap for insert mode mapping, nmap for normal mode mapping, etc.)
some whitespace
the shortcut you want, here <C-L>
some whitespace
the sequence of commands triggered by the shortcut
See :help windows for more info on window management and :help mapping for more info on mappings.

Resources