8

I've been using Vim for decades (and Vi before it since around 1984.) I use it now on Linux. Somewhere along the line one behavior crept in that I'd like to disable: when I click in a window to move the focus, and the click happens to be in the body of the window, Vim moves the cursor to wherever the click landed. If the window is large, it's sometimes not obvious where the cursor was, and that can be important for what I'm trying to do.

I know I could try to click on the window border somewhere, and this wouldn't happen. But I'd rather not have to be that meticulous, especially when there are a lot of windows around. I also know there are other ways to change focus, but I don't want to cycle through every window on my 3-monitor workspace.

I'm running Ubuntu and Xubuntu, and have loaded Vim-gtk because I sometimes want to run full graphical, where I could live with this behavior. But I'm usually not in graphical mode.

I see there's a Vim-tiny that probably does not have this behavior, but I don't know what else I would lose.

I've looked through the setup stuff for the window manager and don't see anything relevant. The Vim man page doesn't have anything that jumped out at me either. My .vimrc doesn't seem to address the issue (but it's long and I may have forgotten/missed something.)

Any suggestions?

1
  • You could try set mousefocus, but I don't think that's quite what you're looking for.
    – Tumbler41
    Commented Apr 18, 2017 at 20:51

4 Answers 4

3

Here's a heavy solution:

nnoremap <LeftMouse> ma<LeftMouse>`a

It will let the click go through (thus changing the focus), but make sure the cursor always goes back to where it was before the click was initiated.

1
  • 2
    This seems to work for me, after I removed the initial 'n'; changing focus seems to leave me in whatever mode I was in, and I just don't want the mouse to move the cursor ever. But then I usually have my fingers pretty much glued to the keys -- I don't use the mouse much for anything except moving windows, changing focus, or when the window or display manager require the mouse.
    – 4dummies
    Commented May 16, 2017 at 16:19
6

From Vim Tips Wiki:

When you are using another application and select go back to Vim by clicking inside Vim's text area, it not only switches application focus to Vim, but it also moves the cursor to that location. If you don't want the cursor to move, put the following in your vimrc:

augroup NO_CURSOR_MOVE_ON_FOCUS
  au!
  au FocusLost * let g:oldmouse=&mouse | set mouse=
  au FocusGained * if exists('g:oldmouse') | let &mouse=g:oldmouse | unlet g:oldmouse | endif
augroup END

Although I am unable to test this on Linux, I've recently started using it on Windows, and it appears to do what you're asking for. (Note that somebody reported an issue with split windows, i.e. after :split, but I haven't observed it myself.)

2
  • This has no effect on Windows in either _gvimrc or _vimrc.
    – felwithe
    Commented Apr 15, 2020 at 17:36
  • This worked perfectly to solve an issue where the mouse would auto-highlight after I clicked on the vim window and it regained focus (with CLI vim in windows).
    – dogoncouch
    Commented Sep 26, 2022 at 14:48
3

You can set mouse= to disable mouse entirely, use set mouse=a to restore mouse behaviour. You can automatically enable/disable mouse only in terminal vim but not in GUI vim by checking for if has("gui"), or by putting your config in .gvimrc.

Alternatively, you can get into the habit of changing window using Alt-Tab instead of the mouse. As a side effect, this prepares your fingers to be in the keyboard to operate vim.

Another alternative, depending on your window manager, you can get into the habit of doing Alt-Click to focus a windows without changing the focus inside that window.

2

:nnoremap <Down> <Nop>

:nnoremap <Up> <Nop>

does the trick (in conjunction with :set mouse=). Of course, you can't use the arrow keys any more, but if you're using vim, you didn't care anyway. Edit: Need to this in insert mode too, so add the following:

:inoremap <Down> <Nop>

:inoremap <Up> <Nop>

1
  • 2
    Welcome to Vi and Vim! A few comments: Disabling the arrow keys shouldnt effect the mouse, and youve missed the point of the question
    – D. Ben Knoble
    Commented Aug 27, 2019 at 18:29

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