4

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? This could remedy the biggest shortcoming of VIM.

8

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).

| improve this answer | |
  • 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. – elmclose Aug 1 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 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. – Martin Tournoij Aug 4 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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