0

I'd like to run xsel as background process to keep clipboard content for future use. I run it in openbox startup script like that:

xsel -b -k

But when I select something with Ctrl-C and close original application, there is nothing in clipboard. Also I don't see xsel process in task manager.

What I'm doing wrong?

PS. I don't what to use parcellite of smth like that. Just simple shell scripts.

0

3 Answers 3

1

If you absolutely want to do this manually, this will provide a constant backup of your most recent copy.

#!/bin/bash

history_file="clipboard_history.txt"
last_item=""

>"$history_file"

add_to_history() {
    local item="$1"
    # empty or a duplicate check
    if [[ -n "$item" && "$item" != "$last_item" ]]; then
        last_item="$item"
        # writes to history file, overwriting previous content
        echo "$item" > "$history_file"
    fi
}

# copies the history to clipboard
copy_from_history() {
    local entry
    entry=$(<"$history_file")
    if [[ -n "$entry" ]]; then
        echo "$entry" | xclip -selection clipboard
    else
        echo "No recent entry in the history"
    fi
}

# infinite loop poll's every two seconds
while true; do
    clipboard_content=$(xclip -selection clipboard -o)

    if [[ -n "$clipboard_content" ]]; then
        add_to_history "$clipboard_content"
    fi

    sleep 2
done

Save as: clipkeeper.sh and change mode to executable chmod +x clipkeeper.sh
Usage: ./clipkeeper.sh &disown

It will output task number and PID number:

[1] 123456

To stop it, type pkill 123456. Use the PID number in the terminal, of course.
That should do it!

0
1

Finally, I've found the solution.

xsd is based on xclipd and fixes this issue in it. I run xsd in startup script and both clipboard and file copy using "Ctrl-C-Ctrl-V" work fine.

0

If you just want your clipboard to work, you should run a clipboard manager.

The lightest one I know of is xclipd. Try this:

  • Clone the xclipd repository and cd to the folder.
  • Run make.
  • Now run ./xclipd.

Until you terminate xclipd, the clipboard will stay alive: you can use it even if the original application quits.

The clipboard is officially called the CLIPBOARD selection, and the reason you’re losing the clipboard is that selections are peer-to-peer. By default, the applications are playing football with the CLIPBOARD selection: if the selection belongs to program 1 and you copy or paste something in program 2, program 2 steals the selection from program 1. If you terminate a program while it’s holding the selection, the selection dies along with the program.

What a clipboard manager does is keep track of each selection from a central place, hold onto the contents in case an application quits, and answer the applications’ requests for copying and pasting and so on.

The answer to a similar question here has a good explanation of the clipboard under X, and the answer here on AskUbuntu has a good reading list for learning more.

2
  • Thanks! Initially it seemed to me that this was exactly what I was looking for. Text was copied between applications without any problems. However, I discovered that for some unknown reason, copying files in Spacefm via 'Ctrl-C Ctrl-V' stopped working... Commented Jul 3 at 14:54
  • Glad you found a solution!
    – wobtax
    Commented Jul 4 at 15:33

You must log in to answer this question.

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