10

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?

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

2 Answers 2

10

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=
    else
        " enable mouse everywhere
        set mouse=a
    endif
endfunc

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.

10

Add below code in .vimrc file.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .