I have been working on my laptop a lot lately, and I am accidentally clicking while typing. I know I could remove set mouse=a in my .vimrc, but sometimes I like using the mouse.

What can I do to create a toggle function to toggle mouse support?

  • 1
    Why not use mouse=nv, so that it's not enabled during insert mode? – Patrick Sep 21 '14 at 20:25
  • This might fit my use-case, but I was really looking for a function to toggle it. – zephyrthenoble Sep 22 '14 at 2:23

You can retrieve the value of an option by using its name with a & prepended. So a simple toggle function for the mouse option would be:

function! ToggleMouse()
    " check if mouse is enabled
    if &mouse == 'a'
        " disable mouse
        set mouse=
        " enable mouse everywhere
        set mouse=a

This toggles between "no mouse" and "mouse in all modes". You can use it via :call ToggleMouse()

PS: don't use something like this for options that are boolean, for these :set option! can be used to invert them.


Add below code in .vimrc file.

  map <F3> <ESC>:exec &mouse!=""? "set mouse=" : "set mouse=nv"<CR>

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.