您的位置:首页 > 运维架构 > Linux

Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)

2015-03-03 19:29 459 查看
http://justcoding.iteye.com/blog/1829963

1. Windows下

使用系统自带的
clip
命令。
# 位于
C:\Windows\system32\clip.exe


示例:

C代码


echo Hello | clip

# 将字符串Hello放入Windows剪贴板

dir | clip

# 将dir命令输出(当前目录列表)放入Windows剪贴板

clip < README.TXT

# 将readme.txt的文本放入Windows剪贴板

echo | clip

# 将一个空行放入Windows剪贴板,即清空Windows剪贴板

2. Ubuntu下

ubuntu下的用户可以只用apt-get来安装:

C代码


sudo apt-get install xclip

其他发行版的用户可以选择自己的安装方式,也可以用源码编译安装,xclip项目的主页是:http://sourceforge.net/projects/xclip/

xclip可以将内容输出到‘X’的剪切板中,比如:

C代码


echo "Hello, world" | xclip

执行这个命令后你就可以用鼠标中键来在X程序中将内容粘贴出来。但是更多的时候,我们需要不仅仅把内容输出到‘X’的剪切板中,而是希望可以在
GUI程序 中用ctrl + v也可以粘贴(比如,输出到gnome的剪切板中),下面这段命令就可以让你将内容输出到gnome的剪切板中:

C代码


echo "Hello, world" | xclip -selection clipboard

再在一个GUI程序中按下ctrl + v,看下是不是粘贴上去了呢?顺着这个命令,我也重新写了一下ifconfig,让它在执行后输入内容到终端的同时,也将ip地址输出到剪切板中,因为通常情况下,查看ifconfig就是为了获取机器的ip地址:

C代码


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'

或者

C代码


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代码


cat README.TXT | xsel

cat README.TXT | xsel -b # 如有问题可以试试-b选项

xsel < README.TXT

# 将readme.txt的文本放入剪贴板

xsel -c

# 清空剪贴板

4. Mac下

使用
pbcopy
命令。 # 对应有个
pbpaste
命令。

示例:

C代码


echo 'Hello World!' | pbcopy

# 将字符串Hello World放入剪贴板

C代码


cat myFile.txt | pbcopy

C代码


pbpaste > file.txt

要复制结果又想看到命令的输出

命令的结果输出时,如果给复制命令(即上面提到的命令clip、xsel、pbcopy)那么命令输出就看不到了。如果你想先看到命令的输出,可以下面这么做。

C代码


$ echo 'Hello World!' | tee tmp.file.txt

Hello World!

$ xsel < tmp.file.txt

$ rm tmp.file.txt

即先使用
tee
命令把输出输到控制台和一个文件中。命令执行完成后,再把输出的内容放到剪贴板中。

复制SSH的公有KEY

使用下面的命令:

C代码


$ pbcopy < ~/.ssh/id_rsa.pub

注:不同系统使用不同的复制命令。避免用文本编辑器打开这个文件、选中文本、CTRL + C这样繁琐操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: