How to clear the line number in Vim when copying?

LinuxVim

Linux Problem Overview


I copy some code from one part of one file to another part in vim, I find that, there are line numbers in each line and the format is gone, how to set correct format as origin ?

like this:

            40         root /opt/release/current/public;
 67             41         passenger_enabled on;
 68              42 

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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.

Solution 3 - Linux

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)

Solution 4 - Linux

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

Solution 5 - Linux

On Mac: I found out that you can select the desired area with Option+Command and copy paste it to another editor.

Solution 6 - Linux

All the previously entered solution are very good one. However, sometimes your are using vim on a remote server (so you cant copy to your clipboard using "+y). Often terminals support copy paste operation.

I use vim to output visual selection to a new shell where I can copy text using terminal feature:

:'<,'>w ! bash -c cat

Then I can easily copy the output.

Same pattern for pasting in vim:

:r ! bash -c cat

Then I paste and send EOF to cat using Ctrl+d. This method also avoid reindenting the text you paste (Note: you can disable automatic indentation using :set pi!).

Solution 7 - Linux

Have a look at the pastetoggle option sometimes set to F11.

As an alternative you could always write the section you want to copy into a temporary file (ma, goto end line then use :'a,.w tempfile) then read it into the second file.

For further investigation you might want to look at the autoindent option.

Solution 8 - Linux

On Windows VIM GUI: :set nu and then hold down Ctrl-Shift while highlighting the desired text with the mouse. This yanks the line numbers into the buffer.

Solution 9 - Linux

On Windows using Putty, I have noticed in vim you can hold down ALT and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.

Steps:

  1. Press and Hold ALT
  2. Drag over area
  3. Release ALT
  4. Copy using CTRL+Shift+C (or right click if that is turned on)

Variations:

The specific key may be different based on your specific set up. If ALT does not work, try the following keys instead:

  1. CTRL
  2. CTRL+Shift
  3. ALT+Shift

Note:

If anyone knows of any other key combinations that have worked for them let me know and I will update this answer with them.

Solution 10 - Linux

I mapped the below command to a key.
It strips the whitespace around the copied line numbers.
It ignores the line text and any blank lines behind.

:1,$s/^\s*[0-9]\+\s//\|1,$s/^\s*[0-9]\+\n/\r/<cr>

Solution 11 - Linux

You can also consider using sed and pbcopy to copy the lines to a clipboard, where you can paste to another terminal or apps outside of vim.

sed -n <line start #>,<line end #>p <file name> | pbcopy

Solution 12 - Linux

simplest way just: $ cat "your file" and then just copy it from your terminal without numbers

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhyView Question on Stackoverflow
Solution 1 - LinuxseheView Answer on Stackoverflow
Solution 2 - LinuxSandeep SinghView Answer on Stackoverflow
Solution 3 - Linuxsij_aView Answer on Stackoverflow
Solution 4 - LinuxSandeepView Answer on Stackoverflow
Solution 5 - LinuxKonstantinos IliadisView Answer on Stackoverflow
Solution 6 - LinuxLynchView Answer on Stackoverflow
Solution 7 - LinuxColWhiView Answer on Stackoverflow
Solution 8 - LinuxGaurav View Answer on Stackoverflow
Solution 9 - LinuxAlejandro ColonView Answer on Stackoverflow
Solution 10 - Linuxbilyboy65View Answer on Stackoverflow
Solution 11 - LinuxSteven TsaoView Answer on Stackoverflow
Solution 12 - LinuxbenSZView Answer on Stackoverflow