使用 xclip 在 Linux 命令行中复制粘贴
复制 粘贴

> 
> 了解如何在 Linux 中使用 xclip。
> 
> 
> 


![](/data/attachment/album/201907/12/114625cu6ayuvvvm5pgfs4.jpg)


在使用 Linux 桌面工作时，你通常如何复制全部或部分文本？你可能会在文本编辑器中打开文件，选择全部或仅选择要复制的文本，然后将其粘贴到其他位置。


这样没问题。但是你可以使用 [xclip](https://github.com/astrand/xclip) 在命令行中更有效地完成工作。`xclip` 提供了在终端窗口中运行的命令与 Linux 图形桌面环境中的剪贴板之间的管道。


### 安装 xclip


`xclip` 并不是许多 Linux 发行版的标准套件。要查看它是否已安装在你的计算机上，请打开终端窗口并输入 `which xclip`。如果该命令返回像 `/usr/bin/xclip` 这样的输出，那么你可以开始使用了。否则，你需要安装 `xclip`。


为此，请使用你的发行版的包管理器。如果你喜欢冒险，你可以[从 GitHub 获取源代码](https://github.com/astrand/xclip)并自己编译。


### 基础使用


假设你要将文件的内容复制到剪贴板。在 `xclip` 中可以使用两种方法。输入：



```
xclip file_name
```

或者



```
xclip -sel clip file_name
```

两个命令之间有什么区别（除了第二个命令更长）？第一个命令在你使用鼠标中键粘贴的情况下有效。但是，不是每个人都这样做。许多人习惯使用右键单击菜单或按 `Ctrl+V` 粘贴文本。如果你时其中之一（我就是！），使用 `-sel clip` 选项可确保你可以粘贴要粘贴的内容。


### 将 xclip 与其他应用一起使用


将文件内容直接复制到剪贴板是个巧妙的技巧。很可能你不会经常这样做。还有其他方法可以使用 `xclip`，其中包括将其与另一个命令行程序结合。


结合是用管道（`|`）完成的。管道将一个命令行程序的输出重定向到另一个命令行程序。这样我们就会有更多的可能性，我们来看看其中的三个。


假设你是系统管理员，你需要将日志文件的最后 30 行复制到 bug 报告中。在文本编辑器中打开文件，向下滚动到最后，复制和粘贴有一点工作量。为什么不使用 `xclip` 和 [tail](https://en.wikipedia.org/wiki/Tail_(Unix)) 来快速轻松地完成？运行此命令以复制最后 30 行：



```
tail -n 30 logfile.log | xclip -sel clip
```

我的写作有相当一部分用于内容管理系统 （CMS） 或者在其他网络中发布。但是，我从不使用 CMS 的 WYSIWYG 编辑器来编写 - 我采用 [Markdown](https://gumroad.com/l/learnmarkdown) 格式离线编写[纯文本](https://plaintextproject.online)。也就是说，许多编辑器都有 HTML 模式。通过使用此命令，我可以使用 [Pandoc](https://pandoc.org) 将 Markdown 格式的文件转换为 HTML 并将其一次性复制到剪贴板：



```
pandoc -t html file.md | xclip -sel clip
```

在其他地方，粘贴完成。


我的两个网站使用 [GitLab Pages](https://docs.gitlab.com/ee/user/project/pages/) 托管。我使用名为 [Certbot](https://certbot.eff.org/) 的工具为这些站点生成 HTTPS 证书，每当我更新它时，我需要将每个站点的证书复制到 GitLab。结合 [cat](https://en.wikipedia.org/wiki/Cat_(Unix)) 命令和 xclip 比使用编辑器更快，更有效。例如：



```
cat /etc/letsencrypt/live/website/fullchain.pem | xclip -sel clip
```

这就是全部可以用 xclip 做的事么？当然不是。我相信你可以找到更多用途来满足你的需求。


### 最后总结


不是每个人都会使用 `xclip`。没关系。然而，它是一个在你需要它时非常方便的一个小工具。而且，正如我几次发现的那样，你不知道什么时候需要它。等到时候，你会很高兴能用上 `xclip`。




---


via: <https://opensource.com/article/19/7/xclip>


作者：[Scott Nesbitt](https://opensource.com/users/scottnesbitt) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
How do you usually copy all or part of a text file when working on the Linux desktop? Chances are you open the file in a text editor, select all or just the text you want to copy, and paste it somewhere else.

That works. But you can do the job a bit more efficiently at the command line using the [xclip](https://github.com/astrand/xclip) utility. xclip provides a conduit between commands you run in a terminal window and the clipboard in a Linux graphical desktop environment.

## Installing xclip

xclip isn't standard kit with many Linux distributions. To see if it's installed on your computer, open a terminal window and type **which xclip**. If that command returns output like */usr/bin/xclip*, then you're ready to go. Otherwise, you need to install xclip.


To do that, use your distribution's package manager. Or, if you're adventurous, [grab the source code](https://github.com/astrand/xclip) from GitHub and compile it yourself.

## Doing the basics

Let's say you want to copy the contents of a file to the clipboard. There are two ways to do that with xclip. Type either:

`xclip file_name`


or

`xclip -sel clip file_name`


What's the difference between the two commands (aside from the second one being longer)? The first command works if you use the middle button on the mouse to paste text. However, not everyone does. Many people are conditioned to use a right-click menu or to press Ctrl+V to paste text. If you're one of those people (I am!), using the **-sel clip** option ensures you can paste what you want to paste.

## Using xclip with other applications

Copying the contents of a file directly to the clipboard is a neat parlor trick. Chances are, you won't be doing that very often. There are other ways you can use xclip, and those involve pairing it with another command-line application.

That pairing is done with a *pipe* (|). The pipe redirects the output of one command line application to another. Doing that opens several possibilities. Let's take a look at three of them.

Say you're a system administrator and you need to copy the last 30 lines of a log file into a bug report. Opening the file in a text editor, scrolling down to the end, and copying and pasting is a bit of work. Why not use xclip and the [tail](https://en.wikipedia.org/wiki/Tail_(Unix)) utility to quickly and easily do the deed? Run this command to copy those last 30 lines:

`tail -n 30 logfile.log | xclip -sel clip`


Quite a bit of my writing goes into some content management system (CMS) or another for publishing on the web. However, I never use a CMS's WYSIWYG editor to write—I write offline in [plain text](https://plaintextproject.online) formatted with [Markdown](https://gumroad.com/l/learnmarkdown). That said, many of those editors have an HTML mode. By using this command, I can convert a Markdown-formatted file to HTML using [Pandoc](https://pandoc.org) and copy it to the clipboard in one fell swoop:

`pandoc -t html file.md | xclip -sel clip`


From there, I paste away.

Two of my websites are hosted using [GitLab Pages](https://docs.gitlab.com/ee/user/project/pages/). I generate the HTTPS certificates for those sites using a tool called [Certbot](https://certbot.eff.org/), and I need to copy the certificate for each site to GitLab whenever I renew it. Combining the [cat](https://en.wikipedia.org/wiki/Cat_(Unix)) command and xclip is faster and more efficient than using an editor. For example:

`cat /etc/letsencrypt/live/website/fullchain.pem | xclip -sel clip`


Is that all you can do with xclip? Definitely not. I'm sure you can find more uses to fit your needs.

## Final thoughts

Not everyone will use xclip. That's fine. It is, however, one of those little utilities that really comes in handy when you need it. And, as I've discovered on a few occasions, you don't know when you'll need it. When that time comes, you'll be glad xclip is there.

## 8 Comments