35

So I've figured out how to add line numbers to vim (:set no or :set number) but how can I make it so that when I use my mouse in a terminal emulator to select a block of lines, it does not also select the numbers?

For example, say I have three lines that look like so in vim:

1    First line
2    Second
3    Third

If I want to select the three lines with the mouse what I want is for it to ONLY select the actual text. But what ends up happening is it selects the line numbers as well as all the space to the left and right of the line numbers.

Is there any way to change this behavior? BTW, I'm using the gnome terminal editor in gnome if that makes a difference.

Adam Plumb
  • 3,738
  • 8
  • 35
  • 34

8 Answers8

48

Use the following:

:set mouse=a

to turn on xterm style mousing in all modes. This will allow you to do what you want. Keep in mind if the vim is remote via ssh, you'll need X11 forwarding turned on for the selection to make it to your local clipboard.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
18

AFAIK, that is not possible.

The only thing I can add at the moment is that you'd be better off with

:set invnumber

It will inverse the current condition, so you can map it to a key, and toggle it. That way you don't have to remember two commands.

Rook
  • 60,248
  • 49
  • 165
  • 242
8

I agree with the first answer. If you use gvim, you can experiment with using set mouse=n and set mouse=a, which should change the line number selecting behavior.

Grant
  • 2,838
  • 1
  • 19
  • 17
  • 1
    see `help mouse` `set mouse=a` seemed to give me the behavior I would expect. I'm no vim expert, but it seems like `set mouse=c` is what I was getting out of the box. I stuck `set mouse=a` into `~/.vimrc` and so far, so good. – Nick Gerner Mar 04 '11 at 00:21
  • Excellent! Works even better than expected. – pid Oct 14 '16 at 12:14
7

When I want to select text into a terminal, I remove line numbers.

:set nonu

When I finished

:set nu
Luc M
  • 16,630
  • 26
  • 74
  • 89
5

You're probably not on a Macintosh, and I can't tell if you mean you want to use system copy rather than vim's yank.

But if you are both those things, you can use option-drag to select text. This creates a 2d box over the text, selecting things that are under it. It should be functionally the same as 'take columns x1 to x2 of rows y1 to y2'.

John McDonnell
  • 1,470
  • 2
  • 18
  • 19
  • Yes! This exactly solves all my problems! For Windows users, use alt and drag to select the text – rasen58 Mar 19 '16 at 21:50
3

I'm pretty sure this is not possible in terminal-vim. There is no accepted standard (e.g. a TTY escape) for indicating blocks of characters that aren't highlightable by mouse, as far as I know.

I would use gvim if you want to be able to do this at any cost. Its behavior is as you describe.

joshk0
  • 2,574
  • 2
  • 25
  • 36
1

My recommendation is to get used to the little box on the right hand corner of the screen which has the current line's number and character position information. Before I used vim I could never imagine living without line numbers, but since then I've moved beyond this. Not having line numbers clutter up the screen allows for distraction free code viewing, and do you really need to know that the line number above your current active line is one less than your current line's number, and the line below is one more?

Bjorn
  • 69,215
  • 39
  • 136
  • 164
-1

A copy without mouse:

Enter Visual mode: Position your cursor, press v (For complete line V) move up or down until desired position

Press control + c to copy into clipboard (if +clipboard exists in :ve command output) press ESC and paste it wherever you want with control + v.

Or press y to yank into register and press p to put the text after the cursor

  • how is this working for you? I tried what you said and nothing is copied in my clipboard... – lucian Feb 19 '19 at 10:20