Using xsel to copy and paste text between the CLI and GUI

Dec 14, 2016 using tags ubuntu, xsel

Sometimes it can be cumbersome to copy a large block of text from a terminal window and this gets more awkward when pages and pages of output scroll by. Thankfully the nifty little xsel command line tool was built just for this!

If you don’t already have it installed, the following should get you going on a Debian-like system:

$ sudo apt-get install xsel

Copy a text file and paste it into your browser (or any GUI application)

$ cat /etc/hosts | xsel --clipboard

The contents of /etc/hosts will now be available to paste in your browser!

File redirection also works as you would expect:

$ xsel --clipboard < /etc/hosts

Paste the contents of your clipboard into your terminal (or any other CLI app)

$ xsel --clipboard

Just like other unix utilities, you also benefit from piping and redirection:

$ xsel --clipboard > ~/my-hosts.txt

Search for all localhost entries within the copied content:

$ xsel --clipboard | grep -i localhost

Should note that xsel works just as you would expect with primary and secondary selections - in addition to clipboard - though I don’t know of many GUI applications that make use of them.