You are not logged in.

#1 2016-01-05 08:54:16

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Shell command for accessing the X11 clipboard

Ok so I've been setting up a bunch of aliases in my .bashrc file and I'm curious if it's possible to create an alias that pulls in the contents of the clipboard?

I've already checked the Debian repo's online so even though I'm doing this on a Mac the utility is available for anyone on BL.

It's a command line youtube downloader called youtube-dl. When I've been using it I grab the url first from my browser then alt+tab to my terminal type youtube-dl then paste the url in and hit return.

It would be awesome if it were possible to write an alias that when executed simply grabbed the contents of the clipboard which in this  case would always be a url added it to the command and ran it.,

Something like yt="youtube-dl $(url)" But I don't know how to do it. I know there's a utility called pbpaste that would paste the clipboard into the terminal but I'm not sure how you'd code that in an alias?

Any suggestions?

Offline

#2 2016-01-05 12:18:14

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Re: Shell command for accessing the X11 clipboard

twoion wrote:

Use xclip for getting data from the clipboard.

# Regular clipboard
CLIPBOARD=$(xclip -o -selection CLIPBOARD)

So if I understand this correctly I would add the above line into my .bashrc and each time something was copied to the clipboard the variable would be updated. Then when I fire off my alias yt="youtube-dl CLIPBOARD" it would insert the contents of the clipboard into the command and execute?

Offline

#3 2016-01-06 08:24:20

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,654
Website

Re: Shell command for accessing the X11 clipboard

Why not just grab the clipboard at the time it's needed, rather than storing it in a variable?

youtube-dl $(xclip -o -selection CLIPBOARD)

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#4 2016-01-06 08:43:49

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Shell command for accessing the X11 clipboard

A bit OT, but this is killer for the YT purpose. Bind it to a keystroke and see the magic happen with a single stroke.

https://github.com/gotbletu/shownotes/b … ownload.sh

I modified it and have three versions of it, the original for mp3, which I never use. and two versions for webm and mp4 formats. Both bound to keystrokes.

Offline

#5 2016-01-06 08:48:33

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Re: Shell command for accessing the X11 clipboard

johnraff wrote:

Why not just grab the clipboard at the time it's needed, rather than storing it in a variable?

youtube-dl $(xclip -o -selection CLIPBOARD)

Basically it comes down to I don't really know what I'm doing and everything I try including the suggestion you just made don't seem to work.

So I set the alias temporarily then tried it out and this is the error message it gave me.

$ alias ytd="youtube-dl $(xclip -o -selection CLIPBOARD)"
$ ytd
Usage: youtube-dl [OPTIONS] URL [URL...]

youtube-dl: error: You must provide at least one URL.
Type youtube-dl --help to see a list of all options.

Out of interest I decided to try xclip on it's own and whether I just typed $ xclip or $ xclip -o -selection CLIPBOARD nothing was printed in the terminal. The only command that did print anything to the terminal when tried on it's own (not as part of the alias) was pbpaste.

Offline

#6 2016-01-06 10:22:11

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Shell command for accessing the X11 clipboard

Use

$ alias ytd="youtube-dl $(xclip -o -selection PRIMARY)"

or, since PRIMARY is default

$ alias ytd="youtube-dl $(xclip -o)"

Offline

#7 2016-01-06 10:32:41

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Re: Shell command for accessing the X11 clipboard

I just remembered that typing "alias" at the prompt displays all configured alias' so I tried it and instead of the whole command that I've been entering it only showed

alias ytd='youtube-dl '

A quick Google and I find out it's not possible to use variables within alias' in Bash. You need to write a function and put that in your .bashrc

Last edited by becker_11 (2016-01-06 10:48:52)

Offline

#8 2016-01-07 02:37:45

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,654
Website

Re: Shell command for accessing the X11 clipboard

It's not possible to put variables in an alias, but it is possible to use commands.

Test:
(First select the word "variables" (or anything else of course) somewhere so there's something in the clipboard.)

john@raffles4:~$ xclip -o
variables
john@raffles4:~$ alias yt='echo $(xclip -o)'
john@raffles4:~$ yt
variables

Maybe you didn't have the url in the clipboard when you tried it before?

But, even so, functions are much more flexible and definitely the way to go for anything but the simplest aliases.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

Board footer

Powered by FluxBB