7

I can use the mouse to highlight, copy and paste text in VIM. So clearly VIM is aware of the mouse. As such it must be possible to get it to put the cursor where the mouse pointer is. Does anybody know how?

1

2 Answers 2

11

I assume you're using Vim in a terminal?

The way the mouse works in a terminal is kinda weird: by default, it's the terminal that handles the mouse and Vim never sees anything. Because it's all text-based this actually works reasonably well: you can select, copy, paste, etc. But this is all on the terminal level, rather than the application (Vim) level.

One of the effects of this is that the text displayed is copied as-is; for example if you use set list then the list characters will be copied as well, as the terminal has no way of knowing they're not actual characters not present in the text file. Depending on what you want to do, this can be either a good or bad thing.

Vim can be made "mouse-aware" with the mouse setting; using set mouse=a enables it everywhere, but there's some more finer-grained controls for it if you want (see :help mouse).

3
  • 1
    Thank you very much Martin. This is brilliant. I had no idea this was possible. I wonder why people bother with scripts and macros to put the cursor in the middle of a long line etc.
    – user25109
    Aug 1, 2020 at 16:40
  • @elmclose I would guess that that's for the cases when you want exact positioning (which is hard to aim by hand), or when the line is so long it's out of screen width (and not wrapped), in which case you can't do it with mouse in principle.
    – Ruslan
    Aug 1, 2020 at 18:09
  • 1
    Personally I prefer using the keyboard since I find it's often faster and more convenient for me @elmclose; but others prefer to use the mouse. Vim has a lot of tools to position the cursor quite efficiently with the keyvoard, but it takes some effort to master those (I haven't fully either!) and the mouse is certainly easier in that sense. Aug 4, 2020 at 14:34
0

If your vim has balloon_eval or balloon_eval_term feature, you may add the code below to your .vimrc file.

set ballooneval
" or set balloonevalterm if you use vim in a terminal
autocmd CursorHold * silent call setpos( '.', [ v:beval_bufnr, v:beval_lnum, v:beval_col, 0 ] )
1
  • If it reacts too slowly, you can set updatetime=1 and set balloondelay=1. Dec 2, 2022 at 11:10

Your Answer

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