5

How can I switch off vim to automatically enter edit mode on middle mouse button click?

Background: I like to create command strings like JJJj to concat three lines and then go one line down, select this command string with the mouse and paste it again and again into vim using the middle mouse button. However, recently, vim automatically enters insert mode on middle mouse click rendering this procedure dysfunctional. I don't know whether it's a change in vim itself or of some default configuration of my distribution (Gentoo), but I need a way to switch that off again.

To clarify one thing: I don't have set mouse=a set (neither in my ~/.vimrc nor somewhere else. So for instance, left mouse clicks do not move the cursor to the clicked to location. This is probably the reason, why [cas]es tip to use set mouse= in ~/.vimrc doesn't solve this issue.

Also, I'm talking about vim (in xterm), not gvim.

System details:

  • Gentoo Linux
  • Vanilla Linux 4.9.13
  • X.Org X Server 1.19.3
  • XTerm(327)
  • vim 8.0.386 (seems modified by Gentoo)
  • are you using any vim plugins which might alter vim's paste behaviour? if not, the only other thing i can suggest is to contact gentoo's vim package maintainer. or try compiling your own from source without any gentoo specific patches to see if it does the same (FYI Vim 8.0.x on my debian sid machines do not do this) – cas Jul 19 '17 at 2:16
  • I reported a bug on this on Gentoo bug tracker: bugs.gentoo.org/show_bug.cgi?id=625590 – Bodo Thiesen Jul 20 '17 at 9:23
  • I am also seeing this in ubuntu 14.04 with a recent (around 2018-02-01) update. I'm also not sure if it was vim or xterm, although I think it must be vim. Very annoying and I hope someone has an idea! (I am also using vim (not gvim) in an xterm with the mouse mode disabled. – Acorn Feb 7 '18 at 18:21
  • The problem is that vim reconises special characters placed around pasted text. When it sees this in command (normal) or insert modes it starts insert mode, and sets paste mode so the text is inserted unmodified (no re-formating). See the link unix.stackexchange.com/questions/364047/… for a complete answer, and two different solutions (depending on what you want) – anthony Feb 9 '18 at 2:07
5

This is fully answered here. See the excellent linked answer for details and alternative solution.

Short answer: put :set t_BE= in your .vimrc file.

  • Up vote this answer! – anthony Feb 9 '18 at 2:07
2

Try disabling vim's mouse mode with :set mouse=. Add that to your ~/.vimrc (without the :). You may also need to comment out any existing set mouse= line if there is one.

This will disable all of vim's mouse-handling, returning to the default X handling of the mouse - i.e. left-click to begin selection, right-click to extend selection, and middle-click to paste. In particular, note that clicking the mouse in the vim window will not move the mouse cursor, or interact with vim's Visual selection mode.


BTW, rather than use on the mouse for sub-standard versions of features built-in to vim, have you considered using :map, or recording macros?

It's easy enough to map sequences of commands to keys, e.g. :map , JJJj to map them to the comma key.

Or use q followed by a letter or digit to record a macro into a register. e.g. qaJJJjq to bind JJJj to @a. You can then run that macro just by typing @a. You can also record numerous macros and map them to , or whatever as needed, to quickly switch between multiple common operations. e.g. :map , @a

I use the , (comma) key for short-term (i.e. current editing session) mappings because it's not mapped to anything I ever use. For permanent mappings, I use function keys and set them in my ~/.vimrc file. e.g. map <F5> {!}par^V^M} maps the F5 key to move the cursor to the start of the current paragraph, pipe it through the par paragraph reformatter, and then move the cursor past the paragraph (vim has built-in paragraph reformatting but I prefer par).

Note that mappings and macros can be as arbitrarily complex as you want. Recording a macro with q will record whatever you type until you type q again. and if you want to embed a control character such as ESC or carriage-return into a :map command, you can use Ctrl-V aka ^V to "escape" them while entering the map - e.g.

:map , Do^V^[!!date -R^V^MkJ

That maps , to replace the remainder of the current line (from the current cursor position) with the RFC-2822 formatted current date & time.

or if you wanted to search for a pattern, join three lines, then search again, you could do something like this:

:map , JJJn

Then perform the first search with /pattern<enter>, and press , to do the join and search for the next occurrence. At that point you can choose whether to join and search again (press , again) or just search again (press n) or do something else entirely. I routinely do things like this when I don't want to do a simple search-and-replace but want to decide each time (e.g. when there's likely to be too many false-positive matches and it's going to take longer than it's worth to craft a perfect regexp search pattern)

The above is only a very simple example of what this can be used for - it's not much different to just typing 3J and then alternating between pressing n for find-next and . to repeat last command...and you can, of course, :map , .n. It really becomes useful when you need to repeat a sequence of commands that can't be repeated with just .

  • Comma is mapped to something by default. It is the reverse form of ;, which is applicable after using any of the commands f, F, t or T. – Wildcard Jul 19 '17 at 0:37
  • 1
    correction then: "not mapped to anything I ever use or care about" :) – cas Jul 19 '17 at 1:03
  • Ok, nice idea to use :map, I'll do that in future. However, the question was about getting middle mouse clicks to work as before again, and your tip sadly doesn't work. After adding "set mouse=" as last line to my ~/.vimrc, I still get the described behavior (and the mouse doesn't do any other actions in vim either, so no cursor movement using the mouse ect, even without that command). So, can't accept your answer despite it contains really good tips and verbose explanations (for me even way TOO verbose - but you couldn't know that, so no blame here). (Upvoted instead ;) – Bodo Thiesen Jul 19 '17 at 1:25
  • what version of vim are you using and on what OS? that seems like extremely broken behaviour. I've never seen mouse-pasting in vim enter insert mode (unless the pasted text included an i or a etc, of course). is this gvim or vim? – cas Jul 19 '17 at 1:28
  • see edited question – Bodo Thiesen Jul 19 '17 at 1:47

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.