43

I saw completely disable mouse in console Vim on Stack Overflow. It sounded like exactly what I wanted, but there the problem was related to Cygwin, so the title was a misnomer.

I want to know how to completely disable all mouse interactions in Vim on the console. I don't want scrolling, selecting, clicking, or otherwise looking at the mouse to have any effect within Vim.

I like having the mouse in the console, just not when I'm using Vim. Is this possible?

I have a laptop with Ubuntu installed on it and when I type, occasionally the touch pad mouse decides to randomly click or drag between some random coordinates on the screen, even when I try my hardest not to touch the damned thing. This has the effect of me typing code in the wrong places all the time, and it's getting really old because sometimes it introduces subtle irreversible bugs into my code.

I've been able to get rid of scrolling and visual select using the commands below in my .vimrc, but it still will relocate the cursor when I click.

" disable mouse interactions "
set mouse=nicr
map <ScrollWheelUp> <nop>
map <S-ScrollWheelUp> <nop>
map <C-ScrollWheelUp> <nop>
map <ScrollWheelDown> <nop>
map <S-ScrollWheelDown> <nop>
map <C-ScrollWheelDown> <nop>
map <ScrollWheelLeft> <nop>
map <S-ScrollWheelLeft> <nop>
map <C-ScrollWheelLeft> <nop>
map <ScrollWheelRight> <nop>
map <S-ScrollWheelRight> <nop>
map <C-ScrollWheelRight> <nop>

What else do I need to do?

P.S. I tried set mouse=, but that seems to reenable visual select...

2
  • 7
    set mouse= effectively disables every mouse interaction with Vim, but not with your terminal emulator. Are you sure that "visual select" is Vim's?
    – romainl
    Commented Sep 14, 2017 at 7:44
  • This could be due to loading a central configuration _after _ loading the config in /etc/vimrc. See my answer here: unix.stackexchange.com/a/506723/194822 Commented Mar 16, 2019 at 19:47

5 Answers 5

62

You should add the following two lines at the end of /etc/vim/vimrc :

set mouse=
set ttymouse=
8
  • 5
    +1 for ttymouse, all websites were mentionning mouse but ttymouse was the one I was missing. Thanks !
    – KaKi87
    Commented Apr 21, 2020 at 22:19
  • 2
    Crazy thing is that, when inside the editor, issuing :set mouse-=a works just fine. However, within the vimrc file only set ttymouse= does the trick. Why is that? Anyway: Thanks for the ttymouse from me as well. Commented Dec 29, 2020 at 19:01
  • 1
    set ttymouse= breaks the auto resize of vim when I resize my terminal emulator (terminator)
    – on8tom
    Commented Oct 6, 2021 at 12:08
  • @on8tom did you try with another terminal emulator? Maybe it is a bug on their side? Commented Oct 6, 2021 at 12:46
  • the same goes for gnome-terminal but setting set mouse= in /usr/share/vim/vim82/defaults.vim fixes it. but that also disables all mouse whithout setting set ttymouse=
    – on8tom
    Commented Oct 6, 2021 at 12:58
6

This might work if you put it at the end of your .vimrc

set mouse=c

It's probaly not Vim that takes the mouse input, but rather your Terminal-emulator. To disable mouse support on your terminal this might help.

3
  • 8
    Why c and not completely blank?
    – Rich
    Commented Nov 1, 2017 at 13:27
  • This worked for me when using vim over ssh (to a Debian Bookworm remote host) via xterm. Correct as per the docs at vimhelp.org/term.txt.html#mouse-using
    – Samveen
    Commented Jan 16 at 6:15
  • Disabling Mouse Reporting in iTerm2 fixed this issue for me. Thanks for the steering @gustav Commented Jan 26 at 3:27
1

This is only a partial solution at best, but you can globally disable the mouse/touchpad while vim is running with xinput in a shell script:

xinput set-prop $ID "Device Enabled" 0

And then

xinput set-prop $ID "Device Enabled" 1

When you're ready to have the touchpad work again.

Use xinput --list to get the id of your pointing device.

1
  • This might combine well with the FocusGained and FocusLost autocommand events, though it looks like it there might be some challenge in finding a terminal + Vim setup where those events can be detected.
    – 8bittree
    Commented Sep 15, 2017 at 0:32
1

You can use -C flag

vim -C somefile
1
  • 7
    Welcome to this site @quant2016! Your answer was flagged as low quality because of its length. It would be useful for future readers that you explain why -C is a solution. Note that this flag doesn't only disable the mouse but it sets the compatible mode of vim which has a lot of unattended side effect here, so I think that this should really be mentioned because it could cause more issues than it solves.
    – statox
    Commented Aug 4, 2020 at 10:08
0

Even with

set mouse=
set ttymouse=

and the various mouse scroll 'key' bindings mapped to <nop> I was still having issues where the scroll wheel was moving the cursor.

I think what is going on is that the terminal is sending up/down keys instead of mouse scrolls. In my case I'm using gnome-terminal with tmux inside.

I couldn't work out how to switch the mouse off more in tmux (it's already off!) and gnome-terminal doesn't seem to have a setting to control this.

For my purposes making up/down do nothing in normal mode does what I want. Bear in mind this also stops you using the up/down keys to move the cursor which you might not like.

You might want to also do this in other modes but probably not command mode since up/down let you cycle through history in there!

"seems that either tmux or gnome-terminal converts scroll into up/down key presses
:nmap <Up> <Nop>
:nmap <Down> <Nop>

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.