vim

How to Set and Disable Mouse in Vim

To set the mouse in the Vim editor use :set mouse=a and to disable it use :set mouse-=a commands. These commands can easily let you switch the mouse operations in the Vim Editor.

The Vim editor is designed to be a keyboard-only editor, but when it comes to offering features it is no less than any other advanced editor because you can also use the mouse in the Vim editor.

The mouse functionality can be useful if you are switching from a GUI-based text editor to Vim, or if you are working with split windows and finding it hard to manage them. Moreover, the mouse functionality can help you quickly switch the tabs if you are working in multiple tabs.

In this tutorial, I will be exploring how to set the mouse functionality and how to disable it.

Set Mouse in Vim

To enable the mouse in the Vim editor use :set mouse=a command in the NORMAL mode.

:set mouse=a

Or place set mouse=a in the vimrc file using the following command to enable mouse functionality permanently.

echo "set mouse=a" >> ~/.vimrc

The a option is used to enable mouse functionality in all modes. But in some cases, the mouse does not enable using the a option. To resolve this problem, employ the :set mouse=nvi command instead of using a.

:set mouse=nvi

You can also enable the mouse for other modes as well:

n Enable the mouse for the NORMAL mode only.
v Enable the mouse for the VISUAL mode only.
i Enable the mouse for the INSERT mode only.
c Enable the mouse for the COMMAND mode only.
h Enable the mouse for NORMAL, VISUAL, INSERT, and COMMAND line modes to edit the help file.
a Enable the mouse for NORMAL, VISUAL, INSERT, and COMMAND line modes.
r To display the hit-enter and more-prompts prompts.

Note that the :set mouse=a command disables the copy and paste option. Please see the last section to counter this limitation of Vim mouse functionality.

There are various other options that you can use to customize the Vim mouse experience such as mousefocus, mousehide, and mouseshape; to read more about Vim mouse operations, use the help command:

:help mouse

Mouse Functions in Vim

You can do the following tasks when the mouse is enabled in Vim:

  • Select the text without entering the VISUAL mode.
  • Change the size of the split windows vertically and horizontally.
  • Switch the tabs.
  • Close the tabs by clicking the X button.

Disable Mouse in Vim

Multiple methods can be used to disable the mouse in the Vim editor, but the simplest method is to use the :set mouse-=a command:

:set mouse-=a

Or you can use:

:set mouse=""

Or simply put nothing after the equals sign:

:set mouse=

To permanently disable the mouse functionality, just place any of the above commands in the vimrc file.

echo "set mouse-=a" >> ~/.vimrc

Toggle Mouse Functionality in Vim

Mouse functionality can be useful while working with split windows or using multiple tabs. However, the drawback of enabling the Vim mouse is that you cannot copy/paste to and from the terminal to outside the terminal. You may need to disable the mouse functionality to access the copy/paste functionality.

Creating a custom command or a shortcut key to toggle the mouse functionality can solve the issue.

function! Mouse()

if &mouse=='a'

set mouse-=a

echo "Mouse Disabled"

else

set mouse=a

echo "Mouse Enabled"

endif

endfunc

The Mouse() function & is used with the mouse keyword to access the value of the option. Next, the conditional operators are used to check if the mouse is enabled then disable it, and vice versa. The echo command will prompt a message in the status bar of the Vim editor about the mouse functionality status.

Paste the above function in the vimrc file and use :call Mouse() command in the Vim editor to toggle the mouse operation.

:call Mouse()

The call command in Vim is utilized to invoke custom Vim Script functions with arguments. In Vim 9 versions, the call command is optional, and the function can be directly invoked by typing its name.

You can map the Vim function to a key using the following command in the vimrc file:

noremap m :call Mouse()<CR>

Now, simply press the m key to enable or disable mouse functionality at any time.

Copy and Paste in Vim with Mouse

As mentioned earlier, the setting mouse for all the modes (:set mouse=a) disables the copy-and-paste option. Enabling the copy/paste option while keeping the mouse enabled depends upon the operating system you are working on.

In the macOS, press and hold the fn key to select and copy the text. The fn key temporarily blocks the mouse functionality in the Vim editor. On Linux, the same functionality can be achieved by using the shift key.

Conclusion

Vim mouse functionality can be useful in many ways, such as controlling the split windows and managing the multiple tabs. It can be enabled by executing the :set mouse=a command, while a indicates that the mouse functionality is enabled for all the modes. However, it can be set for a specific mode as well, such as for NORMAL mode use n, for INSERT mode, use i, and for VISUAL mode use v. To disable the mouse, use the :set mouse-=a command to disable it for all the modes. I would like to map a key to toggle mouse functionality because you may not be able to copy/paste content with the mouse is enabled for Vim. So, to use copy and paste map a key to toggle mouse functionality at any time.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.