简体   繁体   English

如何在 Vim 中复制到剪贴板?

[英]How to copy to clipboard in Vim?

Is it possible to copy to clipboard directly from Vim?是否可以直接从 Vim 复制到剪贴板? yy only copies stuff to Vim's internal buffer. yy只将内容复制到 Vim 的内部缓冲区。 I want to copy to the OS's clipboard.我想复制到操作系统的剪贴板。 Is there any such command in Vim or you can only yank stuff within Vim? Vim 中是否有任何此类命令,或者您只能在 Vim 中拉取东西?

The * register will do this. *寄存器将执行此操作。 In Windows, + and * are equivalent.在 Windows 中, +*是等价的。 In unix there is a subtle difference between + and * :在 unix 中, +*之间存在细微差别:

Under Windows, the * and + registers are equivalent.在 Windows 下,* 和 + 寄存器是等效的。 For X11 systems, though, they differ.但是,对于 X11 系统,它们有所不同。 For X11 systems, * is the selection, and + is the cut buffer (like clipboard).对于 X11 系统,* 是选择,+ 是剪切缓冲区(如剪贴板)。 http://vim.wikia.com/wiki/Accessing_the_system_clipboard http://vim.wikia.com/wiki/Accessing_the_system_clipboard

* is probably what you want most of the time, so I use * because it functions as I expect it to in both environments. *可能是大多数时候你想要的,所以我使用*是因为它在两种环境中都能像我期望的那样工作。

In Linux distros you have to install vim-gtk (aka gvim ) first to gain clipboard functionality.在 Linux 发行版中,您必须首先安装vim-gtk (又名gvim )才能获得剪贴板功能。 This is because non-gtk vim is typically compiled without X11 support.这是因为非 gtk vim 通常是在没有 X11 支持的情况下编译的。 This is to allow it to run on console only machines (often servers).这是为了允许它在仅控制台的机器(通常是服务器)上运行。

And for those confused about how to use registers when yanking or putting, you merely write " then the name of the register. So for copying something to the clipboard register you type "*y and then to put you type "*p (credit: Kyle Mathews)对于那些在 yanking 或 put 时对如何使用寄存器感到困惑的人,您只需写"然后是寄存器的名称。因此,要将某些内容复制到剪贴板寄存器,您可以键入"*y ,然后输入"*p (信用:凯尔·马修斯)

On Mac OSX在 Mac OSX 上

  • copy selected part: visually select text(type v or V in normal mode) and type :w !pbcopy复制所选部分:视觉选择文本(在正常模式下键入vV )并键入:w !pbcopy

  • copy the whole file :%w !pbcopy复制整个文件:%w !pbcopy

  • paste from the clipboard :r !pbpaste从剪贴板粘贴:r !pbpaste

On most Linux Distros, you can substitute:大多数Linux 发行版上,您可以替换:

  • pbcopy above with xclip -i -sel c or xsel -i -b pbcopy上面带有xclip -i -sel c xsel -i -b xclip -i -sel cxsel -i -b
  • pbpaste using xclip -o -sel -c or xsel -o -b pbpaste使用xclip -o -sel -c xsel -o -b xclip -o -sel -cxsel -o -b
    -- Note: In case neither of these tools ( xsel and xclip ) are preinstalled on your distro, you can probably find them in the repos --注意:如果这些工具( xselxclip )都没有预装在你的发行版上,你可以在 repos 中找到它们

In your vimrc file you can specify to automatically use the system clipboard for copy and paste.在您的 vimrc 文件中,您可以指定自动使用系统剪贴板进行复制和粘贴。

On Windows set:在 Windows 上设置:

set clipboard=unnamed

On Linux set (vim 7.3.74+):在 Linux 上(vim 7.3.74+):

set clipboard=unnamedplus

NOTE: You may need to use an up to date version of Vim for these to work.注意:您可能需要使用最新版本的 Vim 才能使它们工作。

http://vim.wikia.com/wiki/Accessing_the_system_clipboard http://vim.wikia.com/wiki/Accessing_the_system_clipboard

Use the register "+ to copy to the system clipboard (ie "+y instead of y ).使用寄存器"+复制到系统剪贴板(即"+y而不是y )。

Likewise you can paste from "+ to get text from the system clipboard (ie "+p instead of p ).同样,您可以从"+粘贴以从系统剪贴板中获取文本(即"+p而不是p )。

@Jacob Dalton has mentioned this in a comment, but nobody seems to have mentioned in an answer that vim has to be compiled with clipboard support for any of the suggestions mentioned here to work. @Jacob Dalton 在评论中提到了这一点,但似乎没有人在回答中提到 vim 必须使用剪贴板支持编译,才能使此处提到的任何建议起作用。 Mine wasn't configured that way on Mac OS X by default and I had to rebuild vim.我的默认情况下在 Mac OS X 上没有这样配置,我不得不重建 vim。 Use this the command to find out whether you have it or not vim --version | grep 'clipboard'使用这个命令来确定你是否拥有它vim --version | grep 'clipboard' vim --version | grep 'clipboard' . vim --version | grep 'clipboard' +clipboard means you're good and the suggestions here will work for you, while -clipboard means you have to recompile and rebuild vim. +clipboard意味着你很好,这里的建议对你-clipboard ,而-clipboard意味着你必须重新编译和重建 vim。

Summing up and make it easier for newbies,总结一下,让新手更容易,

To copy the current line, in command mode type:要复制当前行,请在命令模式下键入:

"*yy

To copy the whole file/buffer, in command mode, first go to the beginning via gg , then type要复制整个文件/缓冲区,在命令模式下,首先通过gg转到开头,然后键入

"*yG

As noted, this requires +clipboard out of vim --version , which indicate the availability of clipboard support, -clipboard means no.如前所述,这需要vim --version之外的+clipboard ,这表明剪贴板支持的可用性, -clipboard表示不支持。

For Ubuntu - July 2018对于 Ubuntu - 2018 年 7 月

Use the register "+ to copy to the system clipboard (ie "+y instead of y ).使用寄存器"+复制到系统剪贴板(即"+y而不是y )。

Likewise you can paste from "+ to get text from the system clipboard (ie "+p instead of p ).同样,您可以从"+粘贴以从系统剪贴板中获取文本(即"+p而不是p )。

You have to also make sure that vim is compiled with support for the clipboard.您还必须确保 vim 是在支持剪贴板的情况下编译的。 Try:尝试:

vim --version | grep .xterm_clipboard -o 

and if it's -xterm_clipboard (a minus prefix) then you do not have support.如果它是-xterm_clipboard (减号前缀),那么你就没有支持。

Here are some instructions for swapping out with a working version of vim that has clipboard support.以下是一些使用具有剪贴板支持的 vim 工作版本换出的说明。

  • $ sudo apt-get purge vim $ sudo apt-get purge vim
  • $ sudo apt-get autoremove (removes any extraneous vim dependency packages from system) $ sudo apt-get autoremove(从系统中删除任何无关的 vim 依赖包)
  • $ sudo apt-get install vim-gnome (or sudo apt-get install vim-gtk3 for newer Ubuntu versions) $ sudo apt-get install vim-gnome(或sudo apt-get install vim-gtk3对于较新的 Ubuntu 版本)

Check again with vim --version | grep .xterm_clipboard -o再次检查vim --version | grep .xterm_clipboard -o vim --version | grep .xterm_clipboard -o and you can confirm the clipboard is now available (ie. +xterm_clipboard ) vim --version | grep .xterm_clipboard -o您可以确认剪贴板现在可用(即+xterm_clipboard

Good luck.祝你好运。

If you are using vim in MAC OSX, unfortunately it comes with older verion, and not complied with clipboard options.如果您在 MAC OSX 中使用 vim,不幸的是它带有旧版本,并且不符合剪贴板选项。 Luckily, homebrew can easily solve this problem.幸运的是,自制软件可以轻松解决这个问题。

install vim:安装vim:

brew install vim

install gui verion of vim:安装 vim 的 gui 版本:

brew install macvim

restart the terminal to take effect.重启终端生效。


append the following line to ~/.vimrc将以下行追加到 ~/.vimrc
set clipboard=unnamed

now you can copy the line in vim with yy and paste it system-wide.现在您可以使用yy在 vim 中复制该行并将其粘贴到系统范围内。



Updated Method :更新方法

I was never satisfied with set clipboard method for years.多年来,我从未对设置剪贴板方法感到满意。 The biggest drawback is it will mess up your clipboard history, even when you use x for deletion.最大的缺点是它会弄乱您的剪贴板历史记录,即使您使用x进行删除也是如此。 Here is a better and more elegant solution.这是一个更好、更优雅的解决方案。

  1. Copy the text [range] of vim into the system clipboard.将 vim 的文本 [range] 复制到系统剪贴板中。 (Hint: use v or V to select the range first, and then type the colon : to activate the Ex command): (提示:先用vV选择范围,然后输入冒号:来激活 Ex 命令):
    :[line-range]yank +
    eg,例如,
    :5,10y * (copy/yank lines 5-10 to the system clipboard * register) :5,10y * (复制/猛拉第 5-10 行到系统剪贴板*寄存器)

  2. Paste the content from the system clipboard into vim on a new line:将系统剪贴板中的内容粘贴到 vim 的新行中:
    :put +

Note:注意:

  1. If you select a word range, the above will not work.如果您选择了一个单词范围,上述方法将不起作用。 use "*y or "+y to save this visual block to clipboard.使用"*y"+y将此可视块保存到剪贴板。 However this is hard to type, I am still thinking about alternatives.然而这很难打字,我仍在考虑替代方案。
  2. :help "* or :help *+ for more informations :help "*:help *+获取更多信息
  3. brew info vim will be able to see other options for installing vim. brew info vim将能够看到安装 vim 的其他选项。 Currrently it does not have any other options.目前它没有任何其他选择。

I wasn't able to copy to my clipboard's system because I had this in my ~/.vimrc file:我无法复制到剪贴板的系统,因为我的 ~/.vimrc 文件中有这个:

 if has('mouse')
   set mouse=a
 endif

But if you add this line next, it will allow you to simply Ctrl + c to get whatever you have selected into your clipboard.但是,如果您接下来添加这一行,它将允许您简单地按 Ctrl + c将您选择的任何内容放入剪贴板。

vmap <C-c> "+y

Original discussion and more details: Copy text out of vim with set mouse=a enabled原始讨论和更多细节: Copy text out of vim with set mouse=a enabled

This answer is specific to MAC users.此答案特定于 MAC 用户。

The default VIM available with MAC does not come with clipboard option enabled. MAC 可用的默认 VIM 没有启用剪贴板选项。 You need that option to access the system clipboard.您需要该选项才能访问系统剪贴板。

To check if your vim has that option enabled use the below command要检查您的 vim 是否启用了该选项,请使用以下命令

vim --version

In the result, you should have +clipboard .结果,你应该有+clipboard If it is -clipboard , then your VIM does NOT have the option to access the system clipboard.如果它是-clipboard ,那么你的 VIM 没有访问系统剪贴板的选项。

You need to MAKE and install your VIM with the option you need.你需要使用你需要的选项来制作和安装你的 VIM。 Following are the commands.以下是命令。

# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"

The above will install the latest VIM with the option +clipboard enabled.以上将安装最新的 VIM,并启用选项+clipboard

Now you need to set the system clipboard.现在您需要设置系统剪贴板。 Append the following line to ~/.vimrc将以~/.vimrc追加到~/.vimrc

set clipboard=unnamed

Now you can yank text to system clipboard.现在您可以将文本拉到系统剪贴板。 Below steps explains how to yank.下面的步骤解释了如何猛拉。

  1. In vim command mode press v , this will switch you to VISUAL mode.在 vim 命令模式下press v ,这会将您切换到 VISUAL 模式。
  2. Move the cursor around to select the text or lines you need to copy.四处移动光标以选择需要复制的文本或行。
  3. Press y , this will copy the selected text to clipboard. Press y ,这会将选定的文本复制到剪贴板。
  4. Go to any external application and CMD + v to paste.转到任何外部应用程序和CMD + v进行粘贴。

I use MACBook Pro with macOS Mojave and the above works in it.我将 MACBook Pro 与 macOS Mojave 一起使用,上面的工作在其中。

我的解决方案是安装包含剪贴板选项的附加 vim:

sudo apt-get install vim-gnome

If you are using GVim, you can also set guioptions+=a .如果您使用 GVim,您还可以set guioptions+=a This will trigger automatic copy to clipboard of text that you highlight in visual mode.这将触发自动复制到您在可视模式下突出显示的文本的剪贴板。
Drawback: Note that advanced clipboard managers (with history) will in this case get all your selection history…缺点:请注意,在这种情况下,高级剪贴板管理器(带有历史记录)将获取您所有的选择历史记录......

You can find the answers here Arch Wiki你可以在这里找到答案Arch Wiki

For Linux: First of all you have to enable Clipboard in your vim version by installing gvim .对于 Linux:首先,您必须通过安装gvim在您的 vim 版本中启用剪贴板。


Next you have to put this line on your .vimrc file.接下来你必须把这一行放在你的.vimrc文件中。

set clipboard=unnamedplus

In vim under ubuntu terminal only,仅在 ubuntu 终端下的 vim 中,

press shift + drag mouse to select a text in vim then ctrl + shift + c on the terminalshift + drag mouse在 vim 中选择一个文本,然后在终端上按ctrl + shift + c

then ctrl + v on other editor然后在其他编辑器上ctrl + v

If you have xclip an easy way of copying text to the clipboard is as follows:如果您有 xclip,将文本复制到剪贴板的简单方法如下:

  1. Yank text you want to copy.猛拉要复制的文本。 ( y command in vanilla vim) (香草 vim 中的y命令)
  2. Type in :call system("xclip -selection clipboard", @")输入:call system("xclip -selection clipboard", @")

:call system() runs a terminal command. :call system()运行终端命令。 It takes two arguments, the first the command, the second what to pipe to that command.它需要两个参数,第一个是命令,第二个是管道到该命令的内容。 For example :echom system("head -1", "Hello\\nWorld") returns Hello (With some padding).例如:echom system("head -1", "Hello\\nWorld")返回 Hello(有一些填充)。 echom returns the output of a command, call doesn't. echom 返回命令的输出, call 不返回。

xclip -selection clipboard just copies text into the system clipboard as opposed to the default X clipboard, (Accessed by the middle moue button). xclip -selection clipboard只是将文本复制到系统剪贴板而不是默认的 X 剪贴板(通过鼠标中间按钮访问)。

@" returns the last yanked text. " is the default register, but you could use any register. @"返回最后一个提取的文本。" 是默认寄存器,但您可以使用任何寄存器。 To see the contents of all registers, type :registers .要查看所有寄存器的内容,请键入:registers

If your vim happens to be compiled without +xterm_clipboard option like it is by default in Debian and I guess Ubuntu, you can pipe selection or entire buffer to external program that handles desktop clipboard.如果您的 vim 恰好在没有+xterm_clipboard选项的情况下编译,就像 Debian 中的默认设置一样,我猜是 Ubuntu,您可以将选择或整个缓冲区通过管道传输到处理桌面剪贴板的外部程序。 For xclip (which you may need to install previously), the command will be :w !xclip -sel clip对于xclip (您可能需要之前安装),命令将是:w !xclip -sel clip

  1. Put set clipboard=unnamed in your vimrc .set clipboard=unnamed放在您的vimrc
  2. Select what you want to copy in Visual mode (Press v to enter).Visual模式下选择要复制的内容(按 v 进入)。
  3. Back to Normal mode (Press escape[esc]), press y to copy.回到Normal模式(按escape[esc]),按y 复制。
  4. If you want to paste something from OS's clipboard, press p/P in Vim Normal mode.如果你想从操作系统的剪贴板粘贴一些东西,在 Vim Normal模式下按 p/P。

I've been struggling with this for months till now on MacOsX using keyboard shortcuts.几个月来,我一直在使用键盘快捷键在 MacOsX 上为此苦苦挣扎。 I know question isn't about using keyboard shorts.我知道问题不是关于使用键盘短裤。 But this might help someone with the same concern.但这可能会对有同样担忧的人有所帮助。

I found that if you uncheck:我发现如果你取消选中:

View -> Allow Mouse Reporting查看 -> 允许鼠标报告

from Terminal menu, you'll be able to copy to clipboard using从终端菜单,您将能够使用复制到剪贴板

command + c命令+ c

again.再次。

This question already has a lot of answers.这个问题已经有很多答案了。 I am adding my way which I think is quick.我正在添加我认为很快的方式。

Quickly, you can press V (Shift + v) to active visual mode.很快,您可以按V (Shift + v) 激活视觉模式。 In visible mode, you can use j and k to select the text you want to copy.在可见模式下,您可以使用jk来选择要复制的文本。 After selection, use选择后,使用

"*y

Now, selected text is copied to clipboard.现在,选定的文本被复制到剪贴板。

Maybe someone will find it useful.也许有人会发现它很有用。 I wanted to stay independent from X clipboard, and still be able to copy and paste some text between two running vims.我想保持独立于 X 剪贴板,并且仍然能够在两个正在运行的 vim 之间复制和粘贴一些文本。 This little code save the selected text in temp.txt file for copying.这个小代码将选定的文本保存在 temp.txt 文件中以进行复制。 Put the code below into your .vimrc.将下面的代码放入您的 .vimrc。 Use CTRL-c CTRL-v to do the job.使用 CTRL-c CTRL-v 来完成这项工作。

vnoremap :w !cp /dev/null ~/temp.txt && cat > ~/temp.txt vnoremap :w !cp /dev/null ~/temp.txt && cat > ~/temp.txt

noremap :r !cat ~/temp.txt noremap :r !cat ~/temp.txt

I'm a Vim newby but, to copy all the text to system clipboard (eg if you want to paste it to a word processor or another text editor like gedit, mousepad etc...), in normal mode:我是 Vim 新手,但是,要在正常模式下将所有文本复制到系统剪贴板(例如,如果您想将其粘贴到文字处理器或其他文本编辑器,如 gedit、鼠标垫等...):

ggVGy ggVGy

or, more simply:或者,更简单地说:

:%y :%y

As suggested, I also installed vim-gtk and put按照建议,我还安装了vim-gtk并放入

set clipboard=unnamedplus设置剪贴板=无名加

in my .vimrc在我的 .vimrc

and everything works fine一切正常

If you want to copy only a portion of text, use visual mode (v) , select the text you want to copy and press y .如果您只想复制一部分文本,请使用视觉模式 (v) ,选择要复制的文本并按y

Finally, I suggest a clipboard program like Clipman (my favorite), Clipit, Parcellite or similar.最后,我建议使用 Clipman(我最喜欢的)、Clipit、Parcellite 或类似的剪贴板程序。

(I'm using vim 8.0 in Debian Stretch Xfce) (我在 Debian Stretch Xfce 中使用 vim 8.0)

FORGIVE ME FOR MY ENGLISH!原谅我的英语! :-) :-)

I have been using these for many years now:我多年来一直在使用这些:

nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$

You can now just use upper case Y to copy to clipboard, and lowercase y won't be affected eg as by set clipboard=unnamed so you can still choose if the copy will go to the clipboard or not.您现在可以只使用大写Y复制到剪贴板,而小写y不会受到影响,例如set clipboard=unnamed因此您仍然可以选择副本是否将进入剪贴板。

Tested on ubuntu 21.04, vim 8.2.在 ubuntu 21.04、vim 8.2 上测试。

In case you don't want to use any graphical interface for vim and you prefer to just stick with terminal emulator there may be a much simpler approach to this problem.如果您不想为 vim 使用任何图形界面,而您更喜欢只使用终端模拟器,则可能有一个更简单的方法来解决这个问题。 Instead of using yank or anything like this, first take a look at documentation of terminal you use.不要使用 yank 或类似的方法,首先查看您使用的终端的文档。 I've been struggling with the same issue (trying to use +clipboard and xclip and so on) and in the end it turned out that in my terminal emulator it's enough to just press shift and select any text you want to copy.我一直在为同样的问题苦苦挣扎(尝试使用 +clipboard 和 xclip 等),最后结果证明在我的终端模拟器中,只需按 shift 并选择要复制的任何文本就足够了。 That's it.就是这样。 Quite simple and no need for messing with configuration.非常简单,不需要搞乱配置。 (I use urxvt by the way). (顺便说一下,我使用 urxvt)。

I had issue because my vim was not supporting clipboard :我有问题,因为我的 vim 不支持剪贴板

vim --version | grep clip
-clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard

I installed vim-gnome (which support clipboard) and then checked again:我安装了 vim-gnome(支持剪贴板),然后再次检查:

vim --version | grep clipboard
+clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      +mouseshape      +startuptime     +xterm_clipboard

Now I am able to copy and paste using "+y and "+p respectively.现在我可以分别使用 "+y 和 "+p 进行复制和粘贴。

My solution was putting the following line to .vimrc:我的解决方案是将以下行放到 .vimrc 中:

map <C-y> :w !xclip -sel c <CR><CR>

The script copies the selected line (trough visual mode) or the file content (if none is selected) to the clipboard using Ctrl + y.该脚本使用 Ctrl + y 将所选行(通过可视模式)或文件内容(如果未选择)复制到剪贴板。 I'm using Manjaro Linux if that matters.如果重要的话,我正在使用 Manjaro Linux。

I'm on mac osx (10.15.3) and new to vim.我使用的是 mac osx (10.15.3) 并且是 vim 新手。 I found this so frustrating and all the answers on here too complicated and/or didn't apply to my situation.我发现这太令人沮丧了,这里的所有答案都太复杂和/或不适用于我的情况。 I ended up getting this working in 2 ways:我最终以两种方式完成这项工作:

  1. key mapping that uses pbcopy : works on the old version of vim that ships with mac. 使用 pbcopy 的键映射:适用于 mac 附带的旧版本 vim。

    Add vmap '' :w !pbcopy<CR><CR> to your ~/.vimrc添加vmap '' :w !pbcopy<CR><CR>到你的 ~/.vimrc
    Now you can visually select and hit '' (two apostrophes) to copy to clipboard现在您可以直观地选择并点击'' (两个撇号)以复制到剪贴板

  2. Install newer version of vim so I can access the solution most recommended in other answers:安装较新版本的 vim,以便我可以访问其他答案中最推荐的解决方案:

    brew install vim
    alias vim=/usr/local/bin/vim (should add this to your ~/.bashrc or equivalent) alias vim=/usr/local/bin/vim (应该将它添加到您的 ~/.bashrc 或等效项中)
    Now you can visually select and hit "+yy to copy to clipboard现在您可以直观地选择并点击"+yy复制到剪贴板

I wrote a simple line in my .vimrc to get copy working.我在我的 .vimrc 中写了一个简单的行来让复制工作。 Hope this helps someone.希望这可以帮助某人。 My vim is not installed with Clipboard support, unfortunately, so none of these suggestions worked for me.不幸的是,我的 vim 没有安装剪贴板支持,所以这些建议都不适合我。 Basically, paste this line in your .vimrc:基本上,将此行粘贴到您的 .vimrc 中:

map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>

If you'd like to read details about what this does, you can read about this on my blog如果您想阅读有关此功能的详细信息,可以在我的博客上阅读此内容

For some international keyboards, you may need to press " + Space to get a " .对于某些国际键盘,您可能需要按 +空格才能获得一个"

So in those case you would have to press " Space + y or " Space * y因此,在这种情况下,您必须按 Space + y Space * y

if you connect to the unix system thru putty then follow the steps below to copy content of a file 如果您通过putty连接到unix系统,请按照以下步骤复制文件的内容

  • Highlight text you want to copy ( you'll be able to copy page by page ) 突出显示要复制的文本(您可以逐页复制)
  • press ctrl+c ( it'll copy the text to clipboard 按ctrl + c(它会将文本复制到剪贴板
  • paste in any external editor Done 粘贴在任何外部编辑器完成

for OSX, like the 10342 answers above made clear, you need to make sure that vim supports the clipboard feature, said the the one that comes pre-shipped with OSX does NOT support clipboard, and that if you run brew install vim it would work.对于 OSX,就像上面明确的 10342 个答案一样,你需要确保 vim 支持剪贴板功能,说 OSX 预装的那个不支持剪贴板,如果你运行brew install vim它会工作.

Except that running vi will still make you run the preshipped OSX version, not the one you installed from brew.除了运行 vi 仍然会让你运行预装的 OSX 版本,而不是你从 brew 安装的版本。

to get over this, I simply aliased my vim command to the brew version, not the OSX default one:为了解决这个问题,我只是将我的 vim 命令别名为 brew 版本,而不是 OSX 默认版本:

alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/vim"

and now i'm golden现在我是金色的

In my instance selecting text by highlighting (clicking and dragging) with my mouse was causing vim to enter into visual mode.在我的实例中,通过用鼠标突出显示(单击并拖动)来选择文本导致 vim 进入可视模式。

On a mac anyway, the easiest solution is to use fn + mouse click and drag to avoid entering into visual mode.无论如何,在 Mac 上,最简单的解决方案是使用fn + mouse click and drag以避免进入可视模式。

If you wish to avoid this behavior by default you can edit your vimrc如果您希望在默认情况下避免这种行为,您可以编辑您的 vimrc

I have struggled a lot in copying to clipboard.我在复制到剪贴板时遇到了很多困难。 Inside Vim it is quite simple using visual mode but if you want to copy to the clipboard things are quite messsed.在 Vim 中,使用可视模式非常简单,但如果你想复制到剪贴板,事情就很麻烦了。 I have simple method of copying using xclip utility.我有使用 xclip 实用程序进行复制的简单方法。 For this you must have to install xclip first.为此,您必须先安装 xclip。

for the whole file it is very simple对于整个文件,它非常简单

xclip -sel clip filename

but if you want to copy only a particular range of line numbers但如果您只想复制特定范围的行号
tail -n +[n1] filename | head -n [n2] | xclip -sel clip

you can make use of ~/.bashrc to simplify this你可以使用 ~/.bashrc 来简化这个

#rangecp copy lines from n1 to n2 from a given file to clipboard
function rangecp()
{
    if [ -f $1 ]
    then
        if [ ! -z $3 ]
        then
            diff=$(($3 - $2 + 1))
            #diff=`expr $3 - $2 + 1`
            tail -n +$2  $1 | head -n $diff | xclip -sel clip
        elif [ ! -z $2 ]
        then
            tail -n +$2  $1 | xclip -sel clip
        else
            echo "Provide a range from [n1] lines to [n2] lines"
        fi
    else
        echo "[ $1 ] file doesn't exist"
    fi
}

then那么

source ~/.bashrc
How to use使用方法

rangecp filename.txt 50 89
rangecp filename.txt 50

I saw many answers on this question and the way to make this work was a combination of many.我在这个问题上看到了很多答案,而使这项工作的方法是多种方法的结合。

The steps I followed to make vim copy to system clipboard are我将 vim 复制到系统剪贴板的步骤是

  1. Uninstall vim using sudo apt remove vim .使用sudo apt remove vim卸载sudo apt remove vim (I was too lazy to find how to re-compile it with the +clipboard support. (我懒得找到如何使用 +clipboard 支持重新编译它。
  2. Install a different vim package called vim-athena using sudo apt install vim-athena that ships with +clipboard.使用 +clipboard 附带的sudo apt install vim-athena安装一个名为 vim-athena 的不同 vim 包。
  3. Add to ~/.vimrc the following line: set clipboard=unnamedplus .~/.vimrc添加以下行: set clipboard=unnamedplus
  4. Source the file by entering command mode and typing source % .通过进入命令模式并输入source %
  5. Save and exit.保存并退出。

Note: I am using Ubuntu 20.04.注意:我使用的是 Ubuntu 20.04。

selecting with the the help of the mouse and right-click copy worked in my case. mouse的帮助下进行selectingright-click copy在我的情况下工作。

I didn't want the line numbers included so I :set nonumber before copying . 我不想要包含行号,所以我:set nonumbercopying之前:set nonumber

Nothing above worked for me on my windows laptop. 我的Windows笔记本电脑上面没有任何上述功能。

Ctrl + C was fine for copying.. but I needed Shift + Insert to Paste ! Ctrl + C可以复制..但我需要Shift + Insert来粘贴!

(A good reason to always get a laptop where Insert can always be accessed without pressing a secondary key) (一个很好的理由总是得到一台笔记本电脑,无需按下辅助键即可访问Insert

Shift + Ctrl + C if you are in graphical mode of Linux, but first you need to select what you need to copy. 如果您处于Linux的图形模式,请 Shift + Ctrl + C ,但首先需要选择需要复制的内容。

在此输入图像描述

I want to supplement a way to copy the line to the clipboard and use it on the function.我想补充一种将行复制到剪贴板并在函数上使用的方法。

here is an example that you open the URL in the browser这是您在浏览器中打开 URL 的示例

let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
    " copy the line and put it in the register *
    normal "*yy
    :execute "silent !start ".g:chrome_exe." ".getreg("*")
endfunction

map ,url :call OpenURL()<CR>

在带有 gnome(ubuntu、xubuntu 等)的 linux 中安装 vim-gnome,您将能够使用 VISUAL 进行选择,然后 Ctrl + C 正常并使用 Ctrl + V 粘贴到其他应用程序中

sudo apt install vim-gnome

For Mac OS X, in the terminal:对于 Mac OS X,在终端中:

  • run vim --version | grep clipboard运行vim --version | grep clipboard vim --version | grep clipboard to check if the clipboard is enabled(with +) vim --version | grep clipboard检查剪贴板是否启用(带+)

vim --版本 | grep 剪贴板

  • add the code below into the.vimrc file将下面的代码添加到 .vimrc 文件中

 if has("clipboard") set clipboard=unnamed " copy to the system clipboard if has("unnamedplus") " X11 support set clipboard+=unnamedplus endif endif

  • restart terminal & vim重启终端& vim

reference: Setting up Vim to yank to clipboard on Mac OS X参考: 在 Mac OS X 上设置 Vim 到剪贴板

Besides vim-gnome, "+y is also supported by default in neovim on Ubuntu 20.04.除了 vim-gnome,Ubuntu 20.04 上的neovim也默认支持 "+y。

If you don't want to install a new program, you could always do the lazy method of cat file.txt or gedit file.txt and copy from there.如果您不想安装新程序,您可以随时使用cat file.txtgedit file.txt的惰性方法并从那里复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM