2

I have an interactive fzf bash script that puts my choice into the X clipboard, like so:

#!/bin/bash

cat /path/to/file.txt | fzf | xclip -sel p -sel s -sel c

exit 0

The text file contains some lines:

one
two
three
four
five

If I launch the script from within a terminal emulator manually: bash path/to/script.sh, then xclip permanently stores piped content and it is available to paste even after closing the terminal emulator.

However, I want to bind it to a keyboard shortcut within my DE to use on demand, so I need to use it with a terminal emulator command, like: mate-terminal -x bash /path/to/script.sh. It opens fine, but as soon as it terminates after making my choice, the xclip content is lost and not available anymore to paste. If I delay the script's termination with sleep 5, then while the terminal is still open, pasting piped content works, but as soon as it terminates, it is lost.

I need a way for the X clipboard to keep the content even after the terminal emulator closes.

1
  • Because otherwise the script would launch in the background, without possibility of interaction from the user with fzf.
    – Stan Shank
    May 28, 2020 at 18:01

1 Answer 1

2

Use the -loops 0 option (default). Make the X selection indefinitely available, until another process requests ownership of the X selection.

However, binding the program to a keyboard shortcut kills the xclip daemon after it returns. This behavior is not present when launching the program from a terminal. The -verbose option keeps the program running in the foreground, which causes the terminal to be kept around as well.

To keep the xclip daemon running, with no terminal window visible, an option is to use the nohup program. It runs a command which will have the init process as PPID (e.g. systemd).

#!/bin/bash

cat /path/to/file.txt | fzf | nohup xclip -loops 0 -sel p -sel s -sel c
3
  • It doesn't work, and actually -loops already assumes 0 if not specified, according to the man page.
    – Stan Shank
    May 28, 2020 at 18:03
  • @StanShank I revised the answer with a specific solution to the case of the hotkey. May 29, 2020 at 0:44
  • 2
    Thank you! I have additionally prevented the creation of nohup.out by appending >/dev/null 2>&1.
    – Stan Shank
    May 29, 2020 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .