DEV Community

Migsar Navarro
Migsar Navarro

Posted on

Use xclip to copy a file to clipboard

This is a very brief post that I want to write here and maybe I'll try to expand later.

I have tried in the past to copy text from a file into the clipboard to paste it in a gist at Github. The easiest way for someone not familiar with the command line is to simply open the file in a regular graphical editor, then select all the text and finally copy it from there.

The thing is, that is not very straightforward, right?

I must admit I've not read the man page for xclip but also, man pages are usually exhaustive but not very concise when you are trying to solve a problem. After taking a look at a few StackOverflow answers and the man page I realize this can be done in a very simple way:

xclip -selection clipboard -i script.js

There are two nice things to notice here that once you know make xclip very flexible:

  1. -selection it can be "primary" (default), "secondary", or "clipboard". Apparently it is not 100% standardized but it is fairly safe to assume that "clipboard" will work in most cases for browser based apps.

  2. -i you don't even have to select the text but you can immediately upload the whole file. For me this is perfect. But I admit there are cases in which selecting some parts would work better.

There seem to be some other very interesting options in the man page, but that will have to wait for later.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay