Enabling mouse support in Vim

Jacob Allred

Hopefully you are using newer equipment than this...

If you’ve ever logged into a server using an ssh client (like PuTTY), then you’ve probably used Vim. This infinitely customizable text editor typically runs within a keyboard-only shell, and thus only lets you use the keyboard (unless you are running something like gVim). You use the arrow keys to move the cursor around and a slew of keyboard shortcuts to insert, move, delete, or otherwise manipulate the text.

If you are a Linux user I just told you a bunch of stuff you already know. But did you know that Vim has mouse support? You just have to turn it on. Open Vim and enter this command:

:set mouse=a

You’ll now have mouse support until you exit Vim and load it up again. To make the change permanent, edit your .vimrc file (typically at ~/.vimrc) and add set mouse=a to the end of the file.

So how do you use the mouse in Vim? What benefits will you get? There are a few things you need to know:

  1. Copying text: You may be in the habit of highlighting text and having it put on your system’s clipboard. This won’t work anymore. When you click and drag to highlight text, you’ll be put into Visual mode, an amazingly powerful part of Vim that many people never use. I highly recommend learning more about Visual mode, but if you want the original behavior you can hold shift while selecting text.
  2. Placing the cursor: Instead of having to use the arrow keys to move your cursor, you can now just click wherever you want it to be. Huge time saver.
  3. Scrolling: Yup, you can scroll with the mouse wheel now. No more hitting page down a hundred times in a giant file. Just scroll that wheel to get to wherever you want in your file.

After a few days of reaping the many benefits of Douglas Engelbart’s wonderful little invention, you may be asking yourself: Why isn’t mouse support turned on by default?! I’m not really sure. I did a bit of searching but couldn’t find a definitive answer. If you figure it out, please let me know!