16

I want to simulate keyboard input so that I can "paste" my clipboard contents to applications that don't allow it (e.g. remote KVM). Right now, I'm trying to use xdotool and xclip:

xdotool type "$(xclip -o)"

This command works if I stay in a terminal window, and type that command myself. It types back my clipboard contents when I run the command. My goal is to bind this command to a hotkey, so that it works in any application.

Linux Sendkeys

If I use this hotkey, unexpected behavior occurs to whatever window has focus. e.g. my terminal window size shrinks (it's somewhat amusing, actually). Similar results occur if I save it as a script and call the script, or if I encapsulate the command with sh -c. How can I make practical use of the powerful xdotool type command?

3
  • That's because the command will be run in a background terminal, you are doing nothing wrong. A terminal will "pop out" without you see it, then will execute this command, then leave. Nov 5, 2012 at 12:17
  • Did you try putting it in a script and keybinding the script ? Or did you try keybinding sh -c 'xdotool type "$(xclip -o)"' instead ?
    – user55822
    Nov 5, 2012 at 17:11
  • Thanks for the ideas @user55822. I had the same results using a script. I saw more unexpected behavior when I used sh -c (gedit opening new tabs). Oddly, it stopped doing anything after I tried different hotkeys.
    – blee
    Nov 5, 2012 at 23:16

5 Answers 5

18
+50

You have two problems with this command. The first is that you need to make sure that the meta-keys in the binding are not pressed when it tries to run the command. Second, by default xclip selects XA_Primary buffer rather than the XA_CLIPBOARD. So you should pass clipboard as the selection parameter for xclip. Change your hotkey command to the following:

sh -c 'sleep 0.5; xdotool type "$(xclip -o -selection clipboard)"'
3
  • This does seem to work quite reliably.
    – blee
    Nov 17, 2012 at 5:27
  • This is so useful for websites that disallow pasting passwords, thanks! Jan 8, 2020 at 5:23
  • Nice ; this lead me to this script which splits the lines for multi-line pastes. The use case for this is that typing Markdown into a Google Doc formats it, but pasting just leaves it as Markdown. Bound the script to <kbd>ctrl-alt-V</kbd> and now I can edit in Vim and still get a formatted document. github.com/POMATu/xdotool-type-clipboard/blob/master/…
    – Adrian
    Apr 27, 2023 at 23:21
3

xdotool creates keypresses, and it won't release the Ctrl key for you... The simplest solution is to add a delay, eg sleep 0.5 to wait for half a second, and to make sure you release Ctrl fast enough.

1
  • Great idea. xdotool type "Hello World!" mostly works for me now. It oddly doesn't work for a few seconds after I use it, but that's not really a huge problem. The xclip output isn't working yet, but I'll try to figure out why. Thanks.
    – blee
    Nov 13, 2012 at 3:58
3

sh -c 'xdotool type --clearmodifiers -- "$(xclip -o -selection clipboard)"'

"--clearmodifiers" gets rid of CTRL/ALT/Shift etc, hopefully meaning the sleep is not required

"--" means end of xdotool options so that if the pasted test begins with - xdotool will not try to interpret it as an option

1
  • When I tried this, my modifier keys got "stuck" down until I pressed and released them. Also the begging of my text did not make it to the application still.
    – Rick
    Dec 29, 2020 at 1:25
0

Actually there are two buffers accessible with Ctrl+V (the clipboard copy-paste) and Shift+Insert (the mouse mark and middle-click paste).

If you have the sessions appropriately connected to share clipboard, then one of the two should work automatically, no?

1
  • I need input automation because I often use remote KVM to access machines (Raritan and IPMI). Usually to install an OS or because networking is broken on the machine.
    – blee
    Nov 16, 2012 at 13:44
0

Since Unbuntu now uses Wayland, xdotool doesn't work by default.

I was able to modify Alex L's solution to use ydotool:

sh -c 'sleep 0.5; xclip -o -selection clipboard | ydotool type --file -'

I needed newlines to be typed as carriage returns, which wasn't available from the built in 0.1.8 ydotool, but did work great from 1.0.4 installed from sources following this guide.

You must log in to answer this question.

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