Score:1

Bash script with output to the system clipboard as a launcher command

cn flag

I am running Xubuntu 20.04. I have created a small bash script where the user can select a line from a list with fzf and output the selection to the system clipboard. This is a simple one-liner:

cat list-select | fzf | xclip -sel c

where list-select is a simple text file containing the items to be selected.

The script works if I run it in the terminal, but what I would like to do is to create a keyboard shortcut for this script. This way, If I am for example writing an email, I could simply paste an element from this list using this script.

I thought that creating a Launcher would be a good idea, so I did a right-click on the Desktop and chose Create Launcher. I gave the launcher a name, wrote the path of the executable as a command and chose Run in Terminal as an option. Here is the desktop file:

[Desktop Entry]
Version=1.0
Type=Application
Name=ClipSel
Comment=
Exec=/home/itsme/clipsel.sh
Icon=
Path=
Terminal=true
StartupNotify=false

My script is:

#!/bin/bash 
cat /home/itsme/list-select | fzf | xclip -sel c 

And it is executable:

$ ls -lah clipsel.sh
 -rwxrwxr-x 1 itsme itsme 62 Nov 14 16:13 clipsel.sh 

The problem is now that, when I execute this launcher, I am able to choose a line in the list, but there is nothing in the system clipboard.

I assume I must change something in the command of the launcher, but I don't know exactly what. Any idea?

Also, if you think of any other way to do this, I would be glad to hear about it.

Best regards

Maxclac avatar
cn flag
The middle click is not working. Are there other kinds of clipboards to test?
terdon avatar
cn flag
Nah, I think it's just the "execute in terminal" that's causing the issue, see my answer below. And although there are three clipboards (primar, secondary and clipboard) only the two are relevant, really. See https://specifications.freedesktop.org/clipboards-spec/clipboards-latest.txt.
Score:0
cn flag

I don't have fzf installed, but I reproduced your issue using a simpler version of your script:

#!/bin/bash
echo "pasted string" | xclip -sel c 

And confirmed that it doesn't work when placed in a launcher unless you set "execute in terminal" to false. This launcher, however, works as expected:

[Desktop Entry]
Name=testXclip
Exec=/home/terdon/scripts/foo.sh
Comment=
Terminal=false
Icon=
Type=Application
Maxclac avatar
cn flag
Thank you for trying this. However, I need to execute in terminal so that fzf can work.
terdon avatar
cn flag
@Maxclac ah, I see it's an interactive program. Crap :/
Score:0
cn flag

OK, it turns out I don't need to create a launcher to achieve what I want.

I can simply go into the Keyboard settings and add an application shortcut there

It is working if I install copyq and use this command instead:

terminator -e "cat list-select | fzf | copyq copy -"

I use terminator as a terminal emulator but I assume it could work for other ones.

It would be interesting to understand why it does not work with xclip.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.