negative zero

Newsboat Copy to Clipboard

2020 May 25

[rss] [tech] [tips]


Context

I use Newsboat as an RSS/Atom reader. I use it for three main types of feeds:

  1. Blog/news feeds which I will read in the feed reader
  2. Link aggregating feeds (such as a feed for a YouTube channel which provides me with links to new videos, or a Reddit forum which provides links to third-party content)
  3. Podcasts (actually just a form of 2)

For 1, Newsboat is sufficient as-is. For 3, Newsboat includes a program called Podboat. But 2 involves me copying a lot of links out of Newsboat, to paste elsewhere. I can do this with the mouse, but this requires me to take my hands off the keyboard, and newsboat breaks links when they wrap, so I have to copy long links in multiple steps. Last night, I finally decided to figure out how to copy links to the clipboard from Newsboat.


Setting It Up

I used a program called xclip which is a command line interface to X selections (the X11 clipboard). I was able to install xclip from my package manager. The basic syntax for copying the output of a command to the keyboard is:

[some command] | xclip -selection c

Since the easiest way to interact with a link in Newsboat is by opening it in the browser (which is usually not desired behavior for me anyway), I decided that the best way to approach this problem would be to hijack the "browser" to instead just copy the URL to the clipboard.

When Newsboat tries to open a URL in the browser, it executes the specified browser command, followed by the URL, such as...

lynx https://example.com

Unfortunately, if we just execute "xclip -selection c https://example.com" it tries to read https://example.com as a file path instead of a string. So we need to pipe the URL as a string into xclip. To do this, I wrote a tiny script:

#!/bin/sh
echo -n "$@" | xclip -selection c

I stored this in /usr/local/bin/copy-to-clipboard. I made it executable:

sudo chmod +x /usr/local/bin/copy-to-clipboard

Now I could copy things to the clipboard by passing them as arguments to copy-to-clipboard, for instance:

copy-to-clipboard https://example.com

Finally, I had to set the Newsboat browser setting to my new script. Add the line:

browser copy-to-clipboard

to ~/.newsboat/config

And now I'm good to go! When I try to "open" links in Newsboat, it copies them to the clipboard, and I can paste them elsewhere.