1

I'm trying to create a plugin that will, when the user clicks the mouse, switch focus to the window the user clicked in without changing the cursor position in that window. To do this, I intend to get the screen coordinates of the character that was clicked on, query the window layout to find out what window is under it, and then switch to that window.

One way to do this would be to set a mark, invoke a <LeftMouse> keypress (to invoke its default behavior, which is to move the cursor to the clicked position), query the cursor position, and load the mark, but of course this 1) gives file coordinates rather than screen coordinates and 2) wouldn't work if you clicked in a window other than the one you were already editing, which is the entire point of this exercise.

1
  • 2
    :h getmousepos()
    – Matt
    Sep 30, 2022 at 8:35

1 Answer 1

1

Thanks to @Matt for showing me the solution. The command I need to addd to my init.vim is surprisingly simple:

; to enable click events in normal mode
set mouse=n
; switch to the window that was clicked when the mouse is pressed
nnoremap <LeftMouse> :call win_gotoid(getmousepos()['winid'])<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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