How to clear the line number in Vim when copying?

If you have line numbers, I'm quite sure you are not using Vim's yank/put operations (these will never copy the linenumbers, foldcolumn, icons etc) because in terms of the edit buffer, they don't exist.

My guess is you are working in a terminal emulator and using the mouse to copy stuff to the clipboard, which possibly selects the 'extraneous' room of the screen (including virtual spaces at the end, line numbers, fold markers etc)

You might have luck setting

:se mouse+=a

in order to get the behaviour of the mouse like you expect it. Otherwise, do the selection with V<movement>...y (y for yank, which corresponds to 'copy')

Then on the destination use p (put at cursor), or P (put before cursor)

Let me know if that helped or you need more info


In normal mode, type :se nonu

This is the easiest way to remove the line numbers and you will be able to copy the text without the line numbers.


In case anyone wants a quicker way (on Linux anyways), I have noticed in vim you can hold down ctrl and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.

Steps:

  1. ctrl and drag over area
  2. release ctrl
  3. copy (either keyboard shortcut or right click)

A permanent solution for this is to add the below code at the end of your .vimrc file located in your home directory.

se mouse+=a

By adding this you will be able to select only text and not the line numbers as shown in below image:

enter image description here


If you are not getting your .vimrc file in your home directory (i faced this problem), type the command :scriptnames in vi editor, it will display the location of your .vimrc file. Reference