Science and technology

Copy and paste on the Linux command line with xclip

How do you normally copy all or a part of a textual content file when engaged on the Linux desktop? Chances are you open the file in a textual content editor, choose all or simply the textual content you wish to copy, and paste it some place else.

That works. But you are able to do the job a bit extra effectively on the command line utilizing the xclip utility. xclip offers a conduit between instructions you run in a terminal window and the clipboard in a Linux graphical desktop setting.

Installing xclip

xclip is not commonplace package with many Linux distributions. To see if it is put in in your laptop, open a terminal window and kind which xclip. If that command returns output like /usr/bin/xclip, then you definately’re able to go. Otherwise, that you must set up xclip.

To do this, use your distribution’s bundle supervisor. Or, when you’re adventurous, grab the source code from GitHub and compile it your self.

Doing the fundamentals

Let’s say you wish to copy the contents of a file to the clipboard. There are two methods to try this with xclip. Type both:

xclip file_name

or

xclip -sel clip file_name

What’s the distinction between the 2 instructions (other than the second being longer)? The first command works when you use the center button on the mouse to stick textual content. However, not everybody does. Many individuals are conditioned to make use of a right-click menu or to press Ctrl+V to stick textual content. If you are a type of folks (I’m!), utilizing the -sel clip choice ensures you’ll be able to paste what you wish to paste.

Using xclip with different functions

Copying the contents of a file on to the clipboard is a neat parlor trick. Chances are, you will not be doing that fairly often. There are different methods you should utilize xclip, and people contain pairing it with one other command-line utility.

That pairing is finished with a pipe (|). The pipe redirects the output of 1 command line utility to a different. Doing that opens a number of potentialities. Let’s check out three of them.

Say you are a system administrator and that you must copy the final 30 strains of a log file right into a bug report. Opening the file in a textual content editor, scrolling all the way down to the tip, and copying and pasting is a bit of labor. Why not use xclip and the tail utility to shortly and simply do the deed? Run this command to repeat these final 30 strains:

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

Quite a little bit of my writing goes into some content material administration system (CMS) or one other for publishing on the net. However, I by no means use a CMS’s WYSIWYG editor to jot down—I write offline in plain text formatted with Markdown. That mentioned, lots of these editors have an HTML mode. By utilizing this command, I can convert a Markdown-formatted file to HTML utilizing Pandoc and duplicate it to the clipboard in a single fell swoop:

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

From there, I paste away.

Two of my web sites are hosted utilizing GitLab Pages. I generate the HTTPS certificates for these websites utilizing a software referred to as Certbot, and I would like to repeat the certificates for every website to GitLab at any time when I renew it. Combining the cat command and xclip is quicker and extra environment friendly than utilizing an editor. For instance:

cat /and so forth/letsencrypt/dwell/web site/fullchain.pem | xclip -sel clip

Is that every one you are able to do with xclip? Definitely not. I am certain you could find extra makes use of to suit your wants.

Final ideas

Not everybody will use xclip. That’s high quality. It is, nonetheless, a type of little utilities that basically is useful whenever you want it. And, as I’ve found on a couple of events, you do not know whenever you’ll want it. When that point comes, you will be glad xclip is there.

Most Popular

To Top