Linux 复制时如何清除Vim中的行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5728259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear the line number in Vim when copying?
提问by why
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 ?
我将一些代码从一个文件的一部分复制到 vim 的另一部分,我发现每一行都有行号并且格式消失了,如何将正确的格式设置为原点?
like this:
像这样:
40 root /opt/release/current/public;
67 41 passenger_enabled on;
68 42
采纳答案by sehe
If you have line numbers, I'm quite sure you are not using Vim's yank/put operations (these will nevercopy the linenumbers, foldcolumn, icons etc) because in terms of the edit buffer, they don't exist.
如果您有行号,我很确定您没有使用 Vim 的 yank/put 操作(这些操作永远不会复制行号、折叠列、图标等),因为就编辑缓冲区而言,它们不存在。
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')
为了像您期望的那样获得鼠标的行为。否则,使用V<movement>...
y( y
for yank
,对应于 'copy') 进行选择
Then on the destination use p(put at cursor), or P(put before cursor)
然后在目的地使用p(放在光标处),或P(放在光标前)
Let me know if that helped or you need more info
让我知道这是否有帮助,或者您需要更多信息
回答by ColWhi
Have a look at the pastetoggle option sometimes set to F11.
看看有时设置为 F11 的 pastetoggle 选项。
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.
作为替代方案,您始终可以将要复制的部分写入临时文件(ma
, goto end line then use :'a,.w tempfile
),然后将其读入第二个文件。
For further investigation you might want to look at the autoindent option.
为了进一步调查,您可能需要查看 autoindent 选项。
回答by Sandeep Singh
In normal mode, type :se nonu
在正常模式下,键入 :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.
这是删除行号的最简单方法,您将能够复制没有行号的文本。
回答by Lynch
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.
以前输入的所有解决方案都非常好。但是,有时您会在远程服务器上使用 vim(因此您无法使用 复制到剪贴板"+y
)。通常终端支持复制粘贴操作。
I use vim to output visual selection to a new shell where I can copy text using terminal feature:
我使用 vim 将视觉选择输出到一个新的 shell,在那里我可以使用终端功能复制文本:
:'<,'>w ! bash -c cat
Then I can easily copy the output.
然后我可以轻松复制输出。
Same pattern for pasting in vim:
在 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!
).
然后我使用Ctrl+粘贴并发送 EOF 给 cat d。此方法还可避免重新缩进您粘贴的文本(注意:您可以使用 禁用自动缩进:set pi!
)。
回答by bilyboy65
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>
回答by Gaurav
On Windows VIM GUI: :set nu
and then hold down Ctrl-Shiftwhile
highlighting the desired text with the mouse. This yanks the line numbers
into the buffer.
在 Windows VIM GUI 上::set nu
然后按住Ctrl-Shift同时用鼠标突出显示所需的文本。这会将行号拖入缓冲区。
回答by sij_a
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.
如果有人想要更快的方法(无论如何在 Linux 上),我注意到在 vim 中你可以按住 ctrl 并拖动你想要复制的区域,你将避开行号并选择你想要的部分。
Steps:
步骤:
- ctrl and drag over area
- release ctrl
- copy (either keyboard shortcut or right click)
- ctrl 并拖动区域
- 释放控制
- 复制(键盘快捷键或右键单击)
回答by Sandeep
A permanent solution for this is to add the below code at the end of your .vimrc
file located in your home directory.
对此的永久解决方案是在.vimrc
位于主目录的文件末尾添加以下代码。
se mouse+=a
By adding this you will be able to select only text and not the line numbers as shown in below image:
通过添加它,您将只能选择文本而不是行号,如下图所示:
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
如果您没有.vimrc
在主目录中获取文件(我遇到了这个问题),请:scriptnames
在 vi 编辑器中键入命令,它将显示您的.vimrc
文件的位置。参考
回答by Steven Tsao
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
和pbcopy
将行复制到剪贴板,在那里您可以粘贴到 vim 之外的另一个终端或应用程序。
sed -n <line start #>,<line end #>p <file name> | pbcopy
sed -n <line start #>,<line end #>p <file name> | pbcopy
回答by Konstantinos Iliadis
On Mac: I found out that you can select the desired area with Option+Command and copy paste it to another editor.
在 Mac 上:我发现您可以使用 Option+Command 选择所需区域并将其复制粘贴到另一个编辑器。