Access klipper clipboard on CLI under KDE4
NOTE: find most recent version on github: https://github.com/milianw/shell-helpers/blob/master/clipboard
Here’s a little script you can save in your path and do things like
# paste current clipboard into file
clipboard > "some_file"
# copy some file into clipboard
cat "some_file" | clipboard
Actually I find it rather useful so I thought I should share it.
#!/bin/bash
# Access your KDE 4 klipper on the command line
# usage:
# ./clipboard
# will output current contents of klipper
# echo "foobar" | ./clipboard
# will put "foobar" into your clipboard/klipper
# check for stdin
if ! tty -s && stdin=$(</dev/stdin) && [[ "$stdin" ]]; then
# get the rest of stdin
stdin=$stdin$'\n'$(cat)
# oh, nice - user input! we set that as current
# clipboard content
qdbus org.kde.klipper /klipper setClipboardContents "$stdin"
exit
fi
# if we reach this point no user input was given and we
# print out the current contents of the clipboard
qdbus org.kde.klipper /klipper getClipboardContents
As usually, save the file (attached below) in your $PATH
and make it executable.
PS: Thanks to Martin Vidner for his article on D-BUS btw. - it gave me the proper dbus commands. PPS: Thanks the the various comments below!
Attachment | Size |
---|---|
clipboard.sh | 647 bytes |
Comments
Want to comment? Send me an email!
Comment by Anonymous (2016-03-11 17:49:00)
Great idea - thanks for the post.
Since the features of Klipper are quite limited I switched to CopyQ a while ago. CopyQ has all the functions I need for everyday work (delete clipboard items, QR code generation, paste into VIM). The latest version is available here http://hluk.github.io/CopyQ/.
Your script works fine with CopyQ - after a little modification: Replace
qdbus org.kde.klipper /klipper setClipboardContents “$stdin”
with
and delete line 22.
Voila!
Comment by Anonymous (2016-02-27 09:24:00)
Thanks a lot for offering this method. It’s awesome. I used it to convert html to markdown.
Comment by wxl (not verified) (2013-04-30 19:41:00)
for most purposes, this is excellent! however, trying to post a 3843-line long
VBox.log
,/usr/bin/qdbus
complains the argument list is too long. i can’t off-hand conceive of an elegant way to solve this problem. anyone?Comment by Anonymous (not verified) (2013-02-20 09:51:00)
Thank you so much!
Comment by Anonymous (not verified) (2012-12-24 04:53:00)
This was so helpful. Thank you. Makes it so easy to write the clipboard to a file through a keyboard shortcut. Configured another keyboard shortcut in klipper to edit the clipboard so now can make anotations if needed with one keystroke, then dump with another!
Comment by Anonymous (not verified) (2011-03-14 12:05:00)
that’s great! thnx!
Comment by Jice (not verified) (2011-01-06 21:33:00)
You should use qdbus, this simplifies a lot. Here is the version I’m using:
So you won’t regret dcop good ol’ time anymore ;-)
Comment by Milian Wolff (2011-03-18 17:13:00)
thanks, updated the script now.
Comment by Kevin Brubeck Unhammer (not verified) (2010-12-21 12:52:00)
You should change the script to say “read -r -t 1 stdin”, that is, add the -r option so that read won’t treat \1 etc. as escapes.
It’s very annoying when working on one-liners in the shell, then deciding they should go into a file, and then finding that all the sed commands are messed up =P
Comment by Milian Wolff (2010-12-22 16:31:00)
try the updated version on github: https://github.com/milianw/shell-helpers/blob/master/clipboard
Comment by notslad (not verified) (2010-11-19 07:17:00)
This small wrapper script will put the output of the command into a GUI msgbox.. in case you want to see the results of piping the clipboard contents through a command by running it via alt-f2..
Comment by Paolo (not verified) (2010-09-29 11:19:00)
Very old post but still a good idea.
Just a thing: to get rid of that 1 second wait, I changed the beginning of the script (lines 6,7,9).
This way, the script doesn’t even try to read from standard input if it’s connected to a terminal.
Comment by Milian Wolff (2010-11-20 19:14:00)
thanks, incorporated into my git branch.
Comment by Morten Slott Hansen (not verified) (2010-05-06 10:09:00)
This is awesome. I extended this with
xmlcopyeditor
and KDE4 hotkeys to have a quick way to get the clipboard content into an XML editor.I created a script called
xmlCopyEditor_readClipboard.sh
And attached it to a hotkey combination
meta + shift + x
This has made my life so much better - hope others can find this usefull!
EDIT: fixed formatting, regards - Milian
Comment by Joetke (not verified) (2009-10-20 14:58:00)
Hi, interesting post really. But I wonder whether KDE 4 is mandatory because when I run your script I got this response: Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.kde.klipper was not provided by any .service files Regards.
Comment by Milian Wolff (2009-10-22 00:44:00)
Yes, this version is for KDE 4. I once had a KDE 3 version, but it looks like I lost it. It was much easier though, you’ll just have to exchange the DBUS stuff with the good old DCOP from KDE 3.
Comment by joetke (not verified) (2009-11-05 14:38:00)
Thanks, it works flawlessly. Regards from Strasbourg, France.
Comment by Anonymous (not verified) (2009-08-12 01:21:00)
I have just found the answer to my question. The trick is not to use “dbus-send”. It works with qdbus org.kde.klipper /klipper getClipboardHistoryItem 0 qdbus org.kde.klipper /klipper getClipboardHistoryItem 1 qdbus org.kde.klipper /klipper getClipboardHistoryItem 2
Comment by Anonymous (not verified) (2009-08-11 22:23:00)
Is there some way to access the klipper history from the command line?
Comment by Milian Wolff (2009-08-12 01:09:00)
Just look for yourself:
*history*
method you’d likeFor inspiration on how to use those stuff in bash see my script above.
Comment by Simon (not verified) (2011-12-06 09:32:00)
Thanks Milian this helped me a lot!
Comment by Sam Tresler (not verified) (2009-07-30 02:31:00)
AWESOME! I was sick of ctl-shift-V’ing 40 lines at a time so I googled and found this. Precisely what I was looking for, thank you.
Comment by Antonio (not verified) (2008-11-20 20:59:00)
Of course, it destroyed the “clipboard” file :-)
Greetings from Spain!
P.S. I suppose it’s
Comment by Milian Wolff (2008-11-26 18:42:00)
Thanks, fixed.
Comment by alexs77 (not verified) (2011-09-25 15:26:00)
Dunno if you’re still fixing stuff, but UUoC always makes my wanna cry… :/
You’re “storing” the output of the clipobard script with:
clipboard > file
You can also feed it from standard input without having to “misuse” cat:
clipboard < file
Cheers! And thanks for the script ;)
Comment by Sys (not verified) (2012-08-04 14:35:00)
However, newbies remembering “clipboard < file” have problems trying later things like “clipboard < ls”, which does not work, they still would have to remember this structure: “ls | clipboard”.
Comment by Sys (not verified) (2012-08-04 14:30:00)
However, newbies remembering “clipboard < file” have problems trying later things like “clipboard < ls”, which does not work, they still would have to remember this structure: “ls | clipboard”.