In my default Vim settings I've enabled a gutter and line numbers. Sometimes I copy some lines to clipboard in order to paste somewhere else. Currently I need to disable line numbers and the gutter. Otherwise they are included selection and thus in the copied text. Then I select the lines with my mouse and copy them using CtrlShiftv. After copying I can re-enable line numbers and gutter again. Is it possible without disabling line numbers and gutter?
2 Answers
The trick is to enable the mouse with :set mouse=a
.
By default, Vim doesn't handle the mouse; the terminal emulator does, and from the terminal emulator's perspective "it's all text", it can't distinguish between your "number" or gutter.
gVim "emulates" a terminal emulator in many ways, and acts in the same way.
Then enabling the mouse, it's Vim that does all the mouse handling, and Vim does know what your gutter is, and what your actual text buffer is, so it can handle selection more intelligently.
Note that using set mouse=a
has some side-effects; clicking anywhere in Vim will put the cursor there, and you can now scroll Vim buffers with your mouse wheel, instead of your terminal's scrollback (both can be considered either bugs or features, depending on your preferences).
A good general rule when using Vim is to not use the mouse—Vim is designed from the ground up to be used with keyboard commands. In order to copy text, you can use the y
command to yank a particular selection or chunk of text. Like all other Vim commands, you can think of y
as a verb and give it modifiers, like y2j
to yank the current line and the next two down, or ytm
to yank from the current cursor position until just before the occurrence of m on the current line.
If you're copying to the clipboard in order to paste into a different application, the Vim Tips wiki page on accessing the system clipboard may be of use to you. The specifics will probably depend on what OS you're using, and what version of Vim. I run Vim on Linux, and in my vimrc file I have:
set clipboard=unnamedplus
This lets me yank into the system clipboard, so I can yy
to grab the current line in Vim and then paste into whatever else I'm using.
:set mouse=a number
, select some text with the mouse, and paste it, I don't get the numbers? I tested both in Vim and gVim on Linux.