1. 程式人生 > >(轉)vim 訪問系統剪貼簿

(轉)vim 訪問系統剪貼簿

原文出處:http://vim.wikia.com/wiki/Accessing_the_system_clipboard

Please review this tip:

  • This tip was imported from vim.org and needs general review.
  • Add suitable categories so people can find the tip.
  • Please avoid the discussion page (use the Comments section below for notes).
  • If the tip contains good advice for current Vim, remove the {{review}} line.

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

 Printable Monobook Previous Next

created 2005 · complexity basic · author JimD · version 7.4

When performing copy, cut, and paste with commands like yd, and p, by default Vim uses its own location for this, called the unnamed register (

:help quotequote). Note that this is different from what most modern graphical text editors and other applications like web browsers do; these applications interact with the system clipboard when using keybindings like CTRL-C, CTRL-X, and CTRL-V. Fortunately, in most cases it is easy to get Vim to work with the system clipboard.

Vim offers the + and * registers to reference the system clipboard (:help quoteplus and :help quotestar). Note that on some systems, + and * are the same, while on others they are different. Generally on Linux, + and * are different: +corresponds to the desktop clipboard (XA_SECONDARY) that is accessed using CTRL-C, CTRL-X, and CTRL-V, while *corresponds to the X11 primary selection (XA_PRIMARY), which stores the mouse selection and is pasted using the middle mouse button in most applications. We can use these registers like any other register. Here are a few common commands that demonstrate this:

  • gg"+yG – copy the entire buffer into + (normal mode)
  • "*dd – cut the current line into * (normal mode)
  • "+p – paste from + after the cursor (works in both normal and visual modes)
  • :%y * – copy the entire buffer into * (this one is an ex command)

One distinction to make is that using + and * is different from using CTRL-SHIFT-V on the terminal (or doing a right-click and then selecting "paste" in the terminal menu), where Vim essentially inserts each character in the clipboard one-by-one. In general using + and * is much more reliable than using CTRL-SHIFT-V.

See also pasting registers for more general information about pasting from registers.

I searched though the tips and found only a few passing references to this feature: VimTip71 (implies it's a feature of gvim, but I find it works in console Vim just fine under Linux; I only use gvim under MS Windows). VimTip771 and VimTip964 refer to an extra utility named xclip which is only for X11 and seems to be completely unnecessary for Vim using the * register), and VimTip960 (misses the point but the comments mention it a few times).

Using the clipboard as the default registerEdit

VimTip21 is spot on but the title suggests it's for MS Windows only. It also mentions that we can change out settings so the "anonymous" register is aliased to the * register using:

set clipboard=unnamed
Note: in vim 7.3.74 and higher you can set clipboard=unnamedplus to alias unnamed register to the + register, which is the X Window clipboard.

VimTip432 and VimTip600 use the * register in some native Vim code (using @*) without explaining it in general. VimTip448 also uses it (for converting hex to decimal) but explains it in passing. VimTip478 uses it in a function for copying the results of a :g search into the * register. VimTip687 suggests that Mac OS X doesn't implement the * register (yet?) but suggests a workaround using the pbcopy and pbpaste utilities. VimTip876 also makes passing reference to it.

There are several tips for copying the current filename into the clipboard including: VimTip432VimTip891VimTip600(most of the fuss is resolving forward slashes and backslashes for MS Windows paths).

Checking for X11-clipboard support in terminalEdit

Getting Vim to work with the X11 clipboard can be a struggle if you want to run Vim in a terminal. In this case, you will have to check for X11 clipboard support. The GUI version of Vim always has clipboard support.

From the console, type:

vim --version | grep clipboard

If you see +clipboard or +xterm_clipboard, you are good to go. If it's -clipboard and -xterm_clipboard, you will need to look for a version of Vim that was compiled with clipboard support. On Debian and Ubuntu, to obtain clipboard support install the packages vim-gtk or vim-gnome (not vim-tiny).

If you are running Redhat/CentOS, you can install the vim-X11 package (if you have gvim then this is already installed). This provides the vimx command, which is a console version of Vim with X11-clipboard support. Or if you are running Debian (only test on 7.0), installing vim-gnome package fixes the problem. I like aliasing Vim to vimx by adding this line to my .bashrc/.cshrc:

.bashrc or .zshrc: if [ -e /usr/bin/vimx ]; then alias vim='/usr/bin/vimx'; fi
.cshrc: if (-e /usr/bin/vimx) alias vim '/usr/bin/vimx'

If you are running Gentoo, emerge vim with the 'X' USE flag. For instance, adding this line to /etc/portage/package.use:

app-editors/vim X

See alsoEdit

CommentsEdit

Under Windows, the * and + registers are equivalent. For X11 systems, though, they differ. For X11 systems, * is the selection, and + is the cut buffer (like clipboard).

Text selected, or otherwise highlighted in one X11 app is available in the selection buffer, and text explicitly copied or cut is available in the cut buffer.

All my inter-window copy & paste to gvim is handled by the <F7> key. This works for all OSes only for gvim.

"copy
vmap <F7> "+ygv"zy`>
"paste (Shift-F7 to paste after normal cursor, Ctrl-F7 to paste over visual selection)
nmap <F7> "zgP
nmap <S-F7> "zgp
imap <F7> <C-r><C-o>z
vmap <C-F7> "zp`]
cmap <F7> <C-r><C-o>z
"copy register

autocmd FocusGained * let @[email protected]+

The + and * registers do not work running Vim under cygwin without an X session (for instance running Vim directly from a cmd or rxvt window). They also don't work under Mac OS X using Vim from the command line (via Terminal.app or iTerm.app). This should be documented (as should workarounds).

For such systems, fakeclip plugin provides comprehensive and extensible emulation for the clipboard registers.

in response to above comment (cygwin vim sans X session), as of the software versions below, there doesn't seem to be any problems using the "* register in or outside of rxvt, in a cmd window:

WinXP (SP3)
cygwin (2.831)
rxvt (20050409-21)
vim (7.3.1314-1)

If you're having trouble with GVIM on Ubuntu, and it doesn't seem to be doing what everybody is saying it should, try closing it completely and reopening. It's supposed to "just work" in the case of copy-paste integration with the system clipboard, especially if you use GVIM's GUI buttons or menus. However, sometimes it just stops working, and you have to close and reopen.

The Ubuntu vim-gtk package contains the +xterm_clipboard option, while the standard vim package does not ...

I used to use the xsel/xclip kludges years ago with gpm, on Debian... but I've found out here that simply installing vim-gtk3 with vim-gnome (they are dependent, and vim.gtk3 gets mapped to vim) makes using "*p a complete breeze, in old CLI vim. --October 5, 2016

If for some reason your version of vim under Unix-like system has no support for clipboard, you can use a handy utility called xsel. Xsel lets you manipulate X selections (check man xsel for more info). To work around the lack of clipboard I use following mappings:

" Copy to X CLIPBOARD
map <leader>cc :w !xsel -i -b<CR>
map <leader>cp :w !xsel -i -p<CR>
map <leader>cs :w !xsel -i -s<CR>
" Paste from X CLIPBOARD
map <leader>pp :r!xsel -p<CR>
map <leader>ps :r!xsel -s<CR>
map <leader>pb :r!xsel -b<CR>

So, to paste from X clipboard, you use :r!xsel -p|-s|-b depending which X clipboard you want to access. To copy, use :w!xsel -i -p|-s|-b.

This was recently added, but there is no indication why it is useful or needed. I have moved it here for now because to my knowledge the normal clipboard access commands should work and are much more desireable.

--Fritzophrenic 17:30, November 8, 2010 (UTC)

I am running ubuntu 10.04 with vim-gtk. The * register works as described for gvim but not the command line. vim --version does show the " +xterm_clipboard" option. This is version 7.2.

I had a similar issue on Ubuntu 14.04. It turns out the default clipboard setting is "autoselect,exclude:cons\|linux"and I was changing it with set clipboard+=unnamedplus, which turned it into "autoselect,exclude:cons\|linux,unnamedplus". However, according to the help page for clipboard-exclude, the exclude option must come last. Changing my code to set clipboard^=unnamedplus solved the problem by prepending the unnamedplus setting, rather than appending it.
BTW, I have Vim GNOME 7.4.52 from the Ubuntu repositories, and clipboard=unnamed uses the X clipboard, while clipboard=unnamedplus uses the (GNOME) System Clipboard.
--LeahCim (talk) 02:01, September 1, 2014 (UTC)

I have found using the autocutsel package on X11 in conjunction with set clipboard=unnamed provides a seamless experience.

Simple workaround for X clipboardsEdit

There is a convenient workaround to create a few commands for pasting/copying selections into/out of various X11 clipboards with `xsel`.

:command -range Cz :silent :<line1>,<line2>w !xsel -i -b
:command -range Cx :silent :<line1>,<line2>w !xsel -i -p
:command -range Cv :silent :<line1>,<line2>w !xsel -i -s
:cabbrev cv Cv
:cabbrev cz Cz
:cabbrev cx Cx

:command -range Pz :silent :r !xsel -o -b
:command -range Px :silent :r !xsel -o -p
:command -range Pv :silent :r !xsel -o -s

:cabbrev pz Pz
:cabbrev px Px
:cabbrev pv Pv

With this code blob dumped in your ~/.vimrc you can do the following

1. Select a blob of text in visual mode
2. Step into command line with ':'
3.
   :`<,`>cz

and your selected text will be copied into one of X's 3 default clipboards.

there are 6 commands, 3 for pasting, 3 for copying, and each command represents a buffer.

z = "X11-Clipboard"
x = "X11 Primary Selection"
v = "X11 Secondary Selection"

DISPLAY environment variable on Linux systemsEdit

Make sure your DISPLAY environment variable is set appropriately - otherwise vim can not connect to your x-session to access the clipboard.

To check run:

echo $DISPLAY

which should output something like:

:0.0

Workaround using x11 minimum packages (under Linux)Edit

If nothing work and you would like to copy from vim (under xterm) to leafpad, you may use this add-on to your ~/.vimrc

Press F9, and it will copy to the x11 clipboard.

function Func2X11()
:call system('xclip -selection c', @r)
endfunction
vnoremap <F9> "ry:call Func2X11()<cr>
vnoremap <m-c> "ry:call Func2X11()<cr>
vnoremap <ESC-c> "ry:call Func2X11()<cr>

I hope that it may help you.

Workarounds for xtermEdit

If :version shows "+xterm_clipboard", but using the clipboard still does not work, try using other system tools as a workaround. For example, you can search for a clipboard.sh script which can be used like 'cat a.txt | clipboard' to copy the contents of file a.txt to the system clipboard. When using kubuntu, the system clipboard is managed by 'Klipper', you can use 'Klipper' shortcut to paste anything to the system clipboard from anywhere because its shortcut has the highest precedence. --Changc11, October 10, 2013

Has a case like this been discussed at the vim_use mailing list? If clipboard support is provided by Vim, and if the versions of Vim and the system are not excessively old, the clipboard should work. This idea is better as a comment until something more conclusive is known. JohnBeckett (talk) 10:27, October 13, 2013 (UTC)

相關推薦

vim 訪問系統剪貼簿

原文出處:http://vim.wikia.com/wiki/Accessing_the_system_clipboard Please review this tip: This tip was imported from vim.org and needs gener

Vim簡明教程【CoolShell】

vim的學習曲線相當的大(參看各種文字編輯器的學習曲線),所以,如果你一開始看到的是一大堆VIM的命令分類,你一定會對這個編輯器失去興趣的。下面的文章翻譯自《Learn Vim Progressively》,我覺得這是給新手最好的VIM的升級教程了,沒有列舉所有的命令,只是列舉了那些最有用的命令。非

跟我一起玩Win32開發21:複製&貼上&剪貼簿操作

我要提醒一下大家,看了我的博文學到的知識,千萬不要用於實際開發,不然你會被你的上司罵:“媽的,這些東西哪來的,從來沒有人這樣做過。”不信你試試,腦細胞被凍結的經理或者技術總監們肯定會這樣說的。 如果是一些有X年工作經驗(X永遠不可能大於100)的程式設計師肯定會說:“你怎麼

Java服務化系統線上應急和技術攻關,你必須擁有的那些應用層指令碼和Java虛擬機器命令

原文部落格:https://www.jianshu.com/p/46a120f9e5a3無論是技術開發人員還是架構設計人員都是在實踐中成長起來的,他們通過實踐進行總結,總結後把經驗昇華並再次應用到實踐中去,進一步提供生產效率。 本文介紹筆者在網際網路公司裡線上應急和技術攻關過程中積累的應用層指令碼和Java虛

Vim系統剪貼簿互動方法

Vim是一款十分強大的文字編輯器(程式編輯器?),但是我們無論是編輯文字還是編寫程式都是需要文字能進入系統剪貼簿並希望系統剪貼簿中的內容能夠貼上到Vim中。這個時候我們需要用到“+y命令(在norma

vim支援系統剪貼簿

剛裝上ubuntu,想在vim上使用系統剪貼簿,找了一些vim的剪貼簿內容,都說vim裡面有很多剪貼簿,其中+為系統剪貼簿,"+p為從系統剪貼簿貼上到vim,"+y為從vim選擇內容貼上到系統剪貼簿,但是我根本沒找到+剪貼簿,所以正確的操作如下:1.首先,檢視vim版本是否支援clipboardvim --v

【Unity3d】在Unity3d中,使用C#中訪問系統剪貼簿

直接上程式碼 public static class ClipboardHelper { private static PropertyInfo m_systemCopyBufferProperty = null; private static PropertyIn

【轉載】js訪問系統剪貼簿

問題描述 IE 支援使用 window.clipboardData 物件內的一系列方法訪問系統剪貼簿; Chrome 和 Safari 中存在類似的 Clipboard 物件,且有與 IE 中類似的方法,但其功能尚未實現; Firefox 和 Opera 不支援這類物件。 造成的影響

ubuntu 無法使用 vim 系統剪貼簿或 :reg 無+號暫存器

轉載自:http://blog.csdn.net/lanchunhui/article/details/51476823   傳送門 在 vim normal 模式下,使用"+y / "+p未執行預期的複製貼上任務時,此時輸入:reg查詢暫存器內容,多半沒有+號暫

tmux中如何完整copy單獨一個pane中的文字到系統剪貼簿本地shell和遠端ssh

以下配置在tmux 2.4以上版本通過。 多個pane的情況下,如果是按住shift用滑鼠左鍵拖動,會橫跨多個pane,從而copy到不需要的文字。 經過多次嘗試,最後試出了在本地shell和遠端ssh都可以copy單獨一個pane中的文字: #############

android開發 -- 複製文字內容到系統剪貼簿自由複製

直接上程式碼:(對應的類:android.content.ClipboardManager) //獲取剪貼簿管理器: ClipboardManager cm = (ClipboardManager) getSystemSe

:【vim小小記】vim的複製貼上(包括系統剪貼簿)

1、vim常用複製貼上命令 Vim的複製貼上命令無疑是y (yank),p(paster),加上yy,P PS: vim有個很有意思的約定(我覺得是一種約定),就是某個命令的大小寫都是實現某種功能,只是方向不同,比如: w 跳轉到下個word,W:跳轉到上個word f 某行

phpthinkphp在linux系統下pdfpng圖片【

dirname solution ima .tar.gz center thinkphp loaded cep .net PHP)中使用imagick實現把PDF轉成圖片 操作之前,先在linux服務器安裝 ghostscript 650 2017-06-17 13:31

手把手教你SOAP訪問webservice並DOM解析返回的XML數據

3層 cor lock pos dom解析 tco 前言 nbsp encoding http://blog.csdn.net/u012534831/article/details/51357111 前言: 目前我們項目組還在采用webservice這種http方式,並且某

Perfmon - Windows 自帶系統監測工具

跟蹤 存取 內存使用情況 ota 點擊 for 等等 order 服務 本文轉自:http://blog.csdn.net/oscar999/article/details/7918385 一、 簡述 可以用於監視CPU使用率、內存使用率、硬盤讀寫速度、網絡速度等。 Per

Building Vim from source

ack xsubpp editor -a alt tags gui github clu Compiling Vim from source is actually not that difficult. Here‘s what you should do: First

強大的vim配置文件,讓編程更隨意

c-c 語言設置 檢測 nes d+ efault office eth conf 歡迎來到小碼哥的博客 博客搬家啦 blog.ma6174.com 強大的vim配置文件,讓編程更隨意 花了很長時間整理的,感覺用起來很方便,共享一下。 我的vim配置主要

在Vue中如何使用axios跨域訪問數據

基本上 屬性 本地測試 original ogr 必須 內容 .com 內心 最近在項目中需要用到axios,所以就惡補一下這個axios到底是什麽東東。越來它是vue-resource的替代品,官網也說了,以後都用axios, vue-resource不在維護。那麽這

linux系統進程的內存布局

觸發 後者 請求 但是 釋放內存 存儲 .org 遵從 流行 摘自http://www.cnblogs.com/diegodu/p/4552490.html 僅僅是學習收藏。 內存管理模塊是操作系統的心臟;它對應用程序和系統管理非常重要。今後的幾篇文章中,我將著眼於實際

vim map nmap

sheng blog right lang reset expr color omap 沒有 轉自:http://blog.csdn.net/taoshengyang/article/details/6319106 有五種映射存在 - 用於普通模式: 輸入命令時。