How to toggle mouse support in vim?

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.


Add below code in .vimrc file.

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

Tags:

Vim