0

I'm aware of how to copy the contents of file:

xsel -b < myfile.txt

But this is not what I want.

I would like something like this:

./file2clip.sh myfile.txt

Which would copy myfile.txt to the clipboard so then I could ctrl + v (paste) or right click and paste the file anywhere (on the desktop, in the file explorer, etc.).

How can I copy a file/files to the clipboard so that I can paste it with ctrl + v anywhere in the gui?

Windows equivalent: (powershell)

Set-Clipboard -Path myfile.txt

Macos equivalent: (applescript)

#!/usr/bin/osascript
on run args
    set the clipboard to POSIX file (first item of args)
end
./file2clip.applescript myfile.txt
  • I don't understand what's wrong with `xsel -b < myfile.txt`? – Arkadiusz Drabczyk May 22 '21 at 13:14
  • @ArkadiuszDrabczyk, Again, I do **not** want to copy the content of `myfile.txt`. Why? Because then I cannot paste it in the gui with `ctrl` + `v`. I would like to paste the copied file, with `ctrl` + `v` or right click paste in the gui. – lordcommander May 22 '21 at 13:19
  • So are you asking how to copy the file's *name* using xsel? if so, you can use any method that streams the name to standard output ex. `printf '%s' myfile.txt | xsel -b` or (in shells that provide here string redirection) `xsel -b <<< myfile.txt` – steeldriver May 22 '21 at 13:33
  • @steeldriver, No, All I want to do is `./file2clip.sh myfile.txt` and then be able to **paste** the file anywhere in the gui with `ctrl` + `v` or right click and paste, **Simulating the process of using `ctrl` + `c` while the file is selected**. In your way, I use `xsel -b <<< myfile.txt` but then I **can't** paste the file in the gui with `ctrl` + `v`. – lordcommander May 22 '21 at 13:40
  • @lordcommander: ok, I think I got it. And I don't know how do that - it looks like a filemanager-specific operation and I don't use file managers. – Arkadiusz Drabczyk May 22 '21 at 13:49
  • It depends on what you call the *gui* and the file manager being used. No silver bullet there. Which gui/filemanager do you want this to work for? – Eduardo Trápani May 22 '21 at 15:12
  • @Eduardo Trápani, I'm hoping for it to work for everything. But if I had to choose, probably nautilus or thunar – lordcommander May 22 '21 at 15:36
  • How is this question different from https://unix.stackexchange.com/questions/597981/how-do-i-copy-a-file-to-the-clipboard-in-the-terminal? If it is no different, then please don't repost questions even if they get no answer. – Quasímodo May 22 '21 at 16:42
  • @Quasímodo, It's better to understand – lordcommander May 22 '21 at 16:43

2 Answers2

1

One way of doing it follows as:

  1. Install xclip, such as:

    sudo apt-get install xclip
    
  2. Pipe the output into xclip to be copied into the clipboard:

    cat file | xclip
    
  3. Paste the text you just copied into a X application:

    xclip -o
    

To paste somewhere else other than an X application, such as a text area of a web page in a browser window, use:

cat file | xclip -selection clipboard

Consider creating an alias:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

  • Terminal 1:

    pwd | c
    
  • Terminal 2:

    cd 'v'
    

Notice the `...` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

AdminBee
  • 21,637
  • 21
  • 47
  • 71
0

Just tested xsel and it works. Have you tried shift+ins? That works for me.

$ xsel -b <<< bla
# or
$ xsel -b <<< "bla bla"
# for string with spaces
tansy
  • 669
  • 4
  • 6