How to clear the line number in Vim when copying?

2020-02-02 06:16发布

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 vim
10条回答
神经病院院长
2楼-- · 2020-02-02 06:31

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.

查看更多
该账号已被封号
3楼-- · 2020-02-02 06:33

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!).

查看更多
别忘想泡老子
4楼-- · 2020-02-02 06:33

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

查看更多
放我归山
5楼-- · 2020-02-02 06:34

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)
查看更多
Fickle 薄情
6楼-- · 2020-02-02 06:37

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

查看更多
我想做一个坏孩纸
7楼-- · 2020-02-02 06:41

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

查看更多
登录 后发表回答