Easy clipboard in bash

Dawid Laszuk published on
2 min, 327 words

Those who know me, know that I'm not a fan of using mouse. Whenever possible I try to avoid using it. Although, after years of experience, I am fluent in keyboarding there are still some tasks that I need mouse. Well, until quite recent.

The number one of annoying tasks is copying things from terminal to clipboard. Depending what exactly I needed to do with it I'd either select something with mouse and then copy, or stream output to file and then select it within editor. Some people found it weird, but yes, often copying through editor is much faster.

Revolution came with xclip. I'm not yet fluent in doing everything with it, but even with limited experience that I have it feels like a superpower. This program allows to copy into/from X clipboard.

Most unix distro have it installed by default or at least in package manager. In case of Ubuntu one can install it with: $ sudo apt-get update $ sudo apt-get install xclip

Examples: Copy current directory into clipboard: $ pwd | xclip

Paste whatever you have in clipboard: $ xclip -o

Copy to global clipboard so you can use in any other program: $ echo $PATH | xclip -selection clipboard

If this is too long to type, one can always use aliases, i.e. setting shorter name for long command. Either type directly into terminal (but that is only for current terminal), or update your ~/.bashrc file with: alias "c=xclip" alias "v=xclip -o" alias "cs=xclip -selection clipboard" alias "vs=xclip -o -selection clipboard"

Then copying content of a file to a clipboard: $ cat file.txt | cs

... and CTRL+V wherever one wishes.

Yes, this is awesome!

Source: StackOverflow: How can I copy the output of a command directly into my clipboard?