7

I regularly need to paste a certain bit of text or a file (probably an image), but it becomes rather long and annoying to keep going back and putting it into my clipboard as I have to copy and paste other things too.

So what would be very useful for me would be to have a certain keyboard shortcut which just pastes a certain piece of text or a file, but is separate from the main clipboard. The keyboard shortcut should either put the item into my main clipboard when pressed or it should just paste it itself, whichever is easier to do (if both as as easy as each other then both would be nice as there are occasions when I will need one, and occasions when I will need the other).

Is there a way of doing something like this? I am running Ubuntu GNOME 16.04 with GNOME 3.20.

1

1 Answer 1

8

The command to get a fixed string into the clipboard is very easy, it's simply

xsel -ib <<< 'Your string goes here'

or if you want to read the string from a file

xsel -ib < your-file.txt

or from a command output

your-command | xsel -ib

Directly writing a fixed string by emulating keypresses is not much more complex though

xvkbd -file - <<< 'Your string goes here'

or if you want to read the string from a file

xvkbd -file your-file.txt

or from a command output

your-command | xvkbd -file -

You can simply create a custom shortcut in the System SettingsKeyboardShortcuts configuration and assign a command to the key combination you wish.

But take care that the shortcut interpreter is not Bash or a similar shell, so our <<< ("here string" syntax) or | pipes will not work. To solve this anyway, we simply enclose our shell command with bash -c "INSERT COMMAND HERE". Just pay attention that you don't use double quotes inside the command then.

Here are the commands how you would have to enter them into the shortcut settings:

  • Copy "my string" to clipboard:

    bash -c "xsel -ib <<< 'my string'"
    
  • Copy content of my-file.txt to clipboard:

    bash -c "xsel -ib < my-file.txt"
    
  • Copy output of my-command to clipboard:

    bash -c "my-command | xsel -ib"
    
  • Directly paste/write "my string":

    bash -c "xvkbd -file - <<< 'my string'"
    
  • Directly paste/write content of my-file.txt:

    bash -c "xvkbd -file my-file.txt"
    
  • Directly paste/write output of my-command:

    bash -c "my-command | xvkbd -file -"
    

Please note that neither xselnor xvkbd are installed by default, so you probably need to install them first using this command:

sudo apt-get install xsel xvkbd
7
  • This answer is brilliant. Thank you! FWIW: man xvkbd says "When invoking xvkbd from display managers such as XDM, GDM, etc., always use xvkbd with -secure option or you will have serious security risk." I don't know if this is relevant here. Aug 4, 2017 at 15:55
  • Also, xvkbd has a -text param that I think is easier to use than -file - <<<. In particular, for me it omits a trailing newline from the specified text unless I explicitly specify one. Also, I think this would stop any need for spawning a subshell? Hence: xvkbd -secure -text 'my string' Aug 4, 2017 at 15:57
  • @JonathanHartley There was a reason I chose -file over -text, but I don't exactly remember what it was. Maybe something about escaping or quoting problems, or simply to use the same construct wherever possible instead of having two different options. About the -secure option, I think this only applies if the command is invoked directly by the display manager (which runs with elevated privileges) to make sure the user can not exploit it to run any commands with those privileges. As we're running both as normal user and non-interactively here, I don't see a risk.
    – Byte Commander
    Aug 4, 2017 at 17:06
  • Fair enough. Reading more about it, I see other people also avoiding '-text' because they don't want it to interpret its own special slash commands in the text. Very fair. In my case I'm using it with a single hard-coded string, which contains no chars that match commands, so I'm ok. In the general case, I can see you'd want to avoid that possibility. Aug 4, 2017 at 19:35
  • @ByteCommander It seems pasting from the file with a keyboard shortcut using bash -c "xvkbd -file my-file.txt" pastes everything except the last letter instantly and then continuously prints the last letter until another key is pressed. Also the touchpad's synaptic manager seems to fail for some time disabling the touch-to-press and 2-finger-scroll features. I am using Kubuntu. Any ideas, what may be wrong? May 12, 2018 at 7:56

You must log in to answer this question.