Vim: set mouse to current cursor position

191 Views Asked by At

I use vim with the option set mouse=a so I can move the vim cursor quickly by pointing and clicking (preferably with my laptop keyboard trackpoint). However, I sometimes also use vim keys to set the cursor (e.g. to another split window). Because of this two way vim cursor manipulation, I sometimes find it annoying that the mouse is at some other far away position I previously clicked on when all I want to do is e.g. move the cursor by a few lines from its current position. I am aware of movement with the keys i j k l H L M etc. but sometimes pointing and clicking is so much faster, especially if you want to go to a specific column of a line.

By the way, this is what ChatGPT came up with as a solution to this problem:

" Function to set the mouse position to match cursor position
function! SetMousePosition()
    let l:winpos = getpos('.')
    let l:mousecmd = "\e[" . l:winpos[1] . ";" . l:winpos[2] . "H"
    call system("printf '" . l:mousecmd . "' >/dev/tty")
endfunction

" Mapping to call the function when moving the cursor
au CursorMoved * call SetMousePosition()

but it doesn't seem to work. Maybe I have to use another terminal (I use Ubuntu gnome-terminal)? I know this all sounds very picky, but isn't that what we all love about vim, being able to customize it to ones specific needs. Or maybe I need a different workflow of navigating the cursor altogether. I am open for suggestions!

0

There are 0 best solutions below