Linux: xclip,pbcopy,xsel用法 terminal 複製粘帖 (mac , ubuntu)

http://justcoding.iteye.com/blog/1829963linux

 

1. Windows下ubuntu

 

使用系統自帶的clip命令。
# 位於C:\Windows\system32\clip.exebash

 

示例:app

C代碼   收藏代碼
  1. echo Hello | clip  
  2. # 將字符串Hello放入Windows剪貼板  
  3.    
  4. dir | clip  
  5. # 將dir命令輸出(當前目錄列表)放入Windows剪貼板  
  6.    
  7. clip < README.TXT    
  8. # 將readme.txt的文本放入Windows剪貼板  
  9.    
  10. echo | clip  
  11. # 將一個空行放入Windows剪貼板,即清空Windows剪貼板  

 

2. Ubuntu下ssh

 

ubuntu下的用戶能夠只用apt-get來安裝:編輯器

C代碼   收藏代碼
  1. sudo apt-get install xclip  
 
其餘發行版的用戶能夠選擇本身的安裝方式,也能夠用源碼編譯安裝,xclip項目的主頁是: http://sourceforge.net/projects/xclip/

xclip能夠將內容輸出到‘X’的剪切板中,好比:oop

C代碼   收藏代碼
  1. echo "Hello, world" | xclip  

 

執行這個命令後你就能夠用鼠標中鍵來在X程序中將內容粘貼出來。可是更多的時候,咱們須要不單單把內容輸出到‘X’的剪切板中,而是但願能夠在 GUI程序 中用ctrl + v也能夠粘貼(好比,輸出到gnome的剪切板中),下面這段命令就可讓你將內容輸出到gnome的剪切板中:this

C代碼   收藏代碼
  1. echo "Hello, world" | xclip -selection clipboard  

 

再在一個GUI程序中按下ctrl + v,看下是否是粘貼上去了呢?順着這個命令,我也從新寫了一下ifconfig,讓它在執行後輸入內容到終端的同時,也將ip地址輸出到剪切板中,由於一般狀況下,查看ifconfig就是爲了獲取機器的ip地址:spa

C代碼   收藏代碼
  1. alias ifconfig='/sbin/ifconfig && echo `/sbin/ifconfig | sed -n 2p | awk "{ print \\$2 }" | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"` | xclip -selection clipboard'  
 

或者.net

C代碼   收藏代碼
  1. xclip -sel clip < file   

 

此時你就能夠在網頁等編輯框CTRL+V了。

 

項目主頁:http://sourceforge.net/projects/xclip/
命令man page: http://linux.die.net/man/1/xclip

 

-i, -in read text into X selection from standard input or files (default) -o, -out prints the selection to standard out (generally for piping to a file or program) -f, -filter when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified -l, -loops number of X selection requests (pastes into X applications) to wait for before exiting, with a value of 0 (default) causing xclip to wait for an unlimited number of requests until another application (possibly another invocation of xclip) takes ownership of the selection -d, -display X display to use (e.g. "localhost:0"), xclip defaults to the value in $ DISPLAY if this option is omitted
 
3. Linux下

使用xsel命令。

 

示例:

C代碼   收藏代碼
  1. cat README.TXT | xsel  
  2. cat README.TXT | xsel -b # 若有問題能夠試試-b選項  
  3. xsel < README.TXT  
  4. # 將readme.txt的文本放入剪貼板  
  5.    
  6. xsel -c  
  7. # 清空剪貼板  
 

4. Mac下

 

使用pbcopy命令。 # 對應有個pbpaste命令。

 

示例:

C代碼   收藏代碼
  1. echo 'Hello World!' | pbcopy  
  2. # 將字符串Hello World放入剪貼板  
 
C代碼   收藏代碼
  1. cat myFile.txt | pbcopy  
 
C代碼   收藏代碼
  1. pbpaste > file.txt  
 
要複製結果又想看到命令的輸出

命令的結果輸出時,若是給複製命令(即上面提到的命令clip、xsel、pbcopy)那麼命令輸出就看不到了。若是你想先看到命令的輸出,能夠下面這麼作。

C代碼   收藏代碼
  1. $ echo 'Hello World!' | tee tmp.file.txt  
  2. Hello World!  
  3. $ xsel < tmp.file.txt  
  4. $ rm tmp.file.txt  
 
即先使用 tee命令把輸出輸到控制檯和一個文件中。命令執行完成後,再把輸出的內容放到剪貼板中。
 
複製SSH的公有KEY

使用下面的命令:

C代碼   收藏代碼
  1. $ pbcopy < ~/.ssh/id_rsa.pub  

 

注:不一樣系統使用不一樣的複製命令。避免用文本編輯器打開這個文件、選中文本、CTRL + C這樣繁瑣操做。
相關文章
相關標籤/搜索