Copy and paste at the Linux command line with xclip

Learn how to get started with the Linux xclip utility.
191 readers like this.
Green paperclips

Original image in public domain. Modified by Jason Baker. CC BY-SA 4.0.

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 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 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 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 formatted with Markdown. 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 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. I generate the HTTPS certificates for those sites using a tool called Certbot, and I need to copy the certificate for each site to GitLab whenever I renew it. Combining the cat 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.

Tags
That idiot Scott Nesbitt ...
I'm a long-time user of free/open source software, and write various things for both fun and profit. I don't take myself all that seriously and I do all of my own stunts.

8 Comments

Yet another case of feline abuse. You don't need cat, xclip can take a file argument:

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

If i didn't use it in the command, my copy of cat would have felt left out ...

Thanks for pointing out an alternative way of copying those certs!

In reply to by Nick Fagan (not verified)

It's also really handy if you have a program you want to send information to the clipboard, but don't want to wrestle work figuring out X's libraries. Just throw a system call to xclip and pipe your data to it and voila.

Haven't read all of this yet but shift-ctrl and either x, c, or v cuts, copies, and pastes in every terminal emulator I've ever used

Not xterm or rxvt-unicode!

I don't think this article is necessarily intended to account for terminal emulators anyway, but to provide an alternate method to perform common copy/paste actions.

In reply to by Edcompsci

Hear, hear! I'm always Ctrl-Shift-V keyin' so in my .zshrc (.bashrc for the bashers) I alias xclip like so:

alias xclip="xclip -sel clip"

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.