1. Windows下
使用系統自帶的clip命令。# 位于C:\Windows\system32\clip.exe
示例:
echo Hello | clip
# 將字符串Hello放入Windows剪貼板
dir | clip
# 將dir命令輸出(當前目錄列表)放入Windows剪貼板
clip < README.TXT
# 將readme.txt的文本放入Windows剪貼板
echo | clip
# 將一個空行放入Windows剪貼板,即清空Windows剪貼板
2. Ubuntu下
ubuntu下的用戶可以用apt-get來安裝:pycharm git ssh
sudo apt-get install xclip
其他發行版的用戶可以選擇自己的安裝方式,也可以用源碼編譯安裝,xclip項目的主頁是:http://sourceforge.net/projects/xclip/
xclip可以將內容輸出到‘X’的剪切板中,比如:
echo "Hello, world" | xclip
執行這個命令后你就可以用鼠標中鍵來在X程序中將內容粘貼出來。但是更多的時候,我們需要不僅僅把內容輸出到‘X’的剪切板中,而是希望可以在GUI程序 中用ctrl + v也可以粘貼(比如,輸出到gnome的剪切板中),下面這段命令就可以讓你將內容輸出到gnome的剪切板中:
echo "Hello, world" | xclip -selection clipboard
再在一個GUI程序中按下ctrl + v,看下是不是粘貼上去了呢?順著這個命令,我也重新寫了一下ifconfig,讓它在執行后輸入內容到終端的同時,也將ip地址輸出到剪切板中,因為通常情況下,查看ifconfig就是為了獲取機器的ip地址:
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'
或者
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命令。
示例:
cat README.TXT | xsel
cat README.TXT | xsel -b # 如有問題可以試試-b選項
xsel < README.TXT
# 將readme.txt的文本放入剪貼板
xsel -c
# 清空剪貼板
4. Mac下
使用pbcopy命令。 # 對應有個pbpaste命令。
示例:
echo 'Hello World!' | pbcopy
# 將字符串Hello World放入剪貼板
cat myFile.txt | pbcopy
pbpaste > file.txt
要復制結果又想看到命令的輸出
命令的結果輸出時,如果給復制命令(即上面提到的命令clip、xsel、pbcopy)那么命令輸出就看不到了。如果你想先看到命令的輸出,可以下面這么做。
$ echo 'Hello World!' | tee tmp.file.txt
Hello World!
$ xsel < tmp.file.txt
$ rm tmp.file.txt
即先使用tee命令把輸出輸到控制臺和一個文件中。命令執行完成后,再把輸出的內容放到剪貼板中。pycharm git ssh
復制SSH的公有KEY使用下面的命令:
$ pbcopy < ~/.ssh/id_rsa.pub
注:不同系統使用不同的復制命令。避免用文本編輯器打開這個文件、選中文本、CTRL + C這樣繁瑣操作。