Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts that simulate typing the clipboard contents. Useful when pasting is not allowed.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#NoTrayIcon ; Hide the tray icon
^+v::Send {Raw}%Clipboard%
#!/bin/bash
xclip -selection clipboard -out | tr \\n \\r | xdotool selectwindow windowfocus type --clearmodifiers --delay 25 --window %@ --file -

It "types" the contents of the clipboard.

Why can't you just paste the contents you ask? Sometimes pasting just doesn't work.

  • One example is in system password fields on OSX.
  • Sometimes you're working in a VM and the clipboard isn't shared.
  • Other times you're working via Remote Desktop and again, the clipboard doesn't work in password boxes such as the system login prompts.
  • Connected via RDP and clipboard sharing is disabled and so is mounting of local drives. If the system doesn't have internet access there's no easy way to get things like payloads or Powershell scripts onto it... until now.

Windows

The Windows version is written in AutoHotKey and easily compiles to an executable. It's a single line script that maps Ctrl-Shift-V to type the clipboard.

^+v::Send {Raw}%Clipboard%

Linux

The following should work on Linux, provided you have xdotool and xclip installed. This version lets you select the window you want to send the keystrokes to.

xclip -selection clipboard -out | tr \\n \\r | xdotool selectwindow windowfocus type --clearmodifiers --delay 25 --window %@ --file -

Explanation of the flags used:

  • xclip -selection clipboard gets the contents of the clipboard.
  • -out writes the text to stdout.
  • tr \\n \\r replaces newlines with carriage returns to ensure they don't get missed in some applications.
  • selectwindow allows you to pick a window to send text to. This means you don't have to have the window active when you run the command.
  • windowfocus focuses the selected window. Most apps I tried would ignore keystroke events if they weren't in focus.
  • --clearmodifiers makes sure that no modifier keys are pressed before typing.
  • --delay 25 was the best balance between speed and not missing keystrokes in the applications I was using. This shouldn't be noticable for short text, but makes a difference with longer text.
  • --window $@ means that keystrokes will only be sent to that window. If you focus a different window the typed keystrokes won't suddenly be sent to your new window.
  • --file - reads from stdin.

The following is a version that just "pastes" immediately to the active window.

xclip -selection clipboard -out | tr \\n \\r | xdotool type --clearmodifiers --delay 25 --file -

I saved this to a script and then mapped the script to a hotkey using Gnome's custom keyboard shortcuts.

OSX

The Mac version is writtern in AppleScript.

tell application "System Events" to keystroke the clipboard as text

The equivalent one-liner from the command line would be:

osascript -e 'tell application "System Events" to keystroke the clipboard as text'

To bind this to a keyboard shortcut you have several options. Sticking with builtin OSX utilities you can follow this guide.

Otherwise, you can use a third party program that lets you set custom hotkeys such as: BetterTouchTool, Keyboard Maestro, or Hammerspoon

Credits:

  • @Indigo744 for the suggestion to use {Raw} in the Windows version
  • @L3vi47h4N for the Linux version
  • @brabster for the --clearmodifiers suggestion in the Linux version
@Indigo744
Copy link

Indigo744 commented Jun 19, 2018

I would suggest you to use

^+v::Send {Raw}%Clipboard%

Otherwise some expression will be interpreted... Like {Enter} to Enter, ^c to CTRL+C...
See https://autohotkey.com/docs/commands/Send.htm

Copy link

ghost commented Jun 25, 2018

With a typical Linux install, the following works (requires xclip and xdotool):
sh -c 'sleep 1.0; xdotool type "$(xclip -o -selection clipboard)"'

@nanoDBA
Copy link

nanoDBA commented Aug 23, 2018

Still trying to figure out why this awesome idea to simulate typing the clipboard contents isn't working in a local PowerShell console host (version 5.1.17134.228 on Windows 10) for me - it does work in the ISE console and in CMD.EXE. Also works fine in PowerShell Core 6.01.

Works fine in RDP sessions to PowerShell 5.1.14393.2273 as well.

@ypid-geberit
Copy link

ypid-geberit commented Sep 20, 2018

@nanoDBA I would assume that this is because Windows tries protect processes running under a different user from being influenced by your current user. (I say tries because I am very aware how real isolation looks like -> Qubes OS.) You should be able to fix that by running the script as admin.

Ref: andreas-hofmann/neoqwertz#1

@ethack
Copy link
Author

ethack commented Nov 7, 2018

Thanks for the contributions @Indigo744 and @L3vi47h4N!

I had no idea people were commenting here.

@haakonstorm
Copy link

haakonstorm commented Sep 1, 2019

I need a way to be able to paste special characters, so that they are typed, on macOS.
text \t text2
is written literally.
Tips?

@ethack
Copy link
Author

ethack commented Sep 1, 2019

@haakonstorm if the special characters are in the clipboard they get typed correctly. For instance if I copy "text text2" and then use the script to type the clipboard it types it out as "text text2" (where the actual tab character is between the text). If you're looking to define a string with escape sequences like "\t" or "\n" in Applescript then I would have tried \t like you did as well. Or look at if you can define characters as hex codes or something.

@neillindberg
Copy link

neillindberg commented Feb 10, 2020

I feel I've found knowledge around my mission - I just want to add a right-click option on my Windblows context menu that will paste my email as I need to type it a zillion times per day. I've gotten the item added, but how to tell this system to paste some certain text? I think I've even confused Dr. Google and his cousin Stack Overflow, M.D.

@brabster
Copy link

brabster commented Jul 15, 2020

Suggest --clearmodifiers flag on the Linux version - I was getting random characters omitted from what was pasted without it.

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

@FloPinguin
Copy link

FloPinguin commented Oct 10, 2020

Thanks for the great ahk version! My usecase: Some websites are blocking pasting in input fields (For example credit card information). And I don't have time to unblock it with developer tools or typing it manually.

@TrevCan
Copy link

TrevCan commented Jul 24, 2021

Thanks for making this!, I will add an option (maybe -p or something) to be able to use a password manager such as pass ( https://www.passwordstore.org/) to type out my password, i know pass already has a script that uses it but it's too long (passmenu --type)!

@TrevCan
Copy link

TrevCan commented Jul 24, 2021

Hey @ethack , I have added some new things to my fork (https://gist.github.com/TrevCan/8af5e1f8ff15f76d8f1b0ea71ecaa64e), including support for the pass password manager, --help usage section, as well as a proper license. You might want to check it out in case you'd like to merge.

@clydeevans393
Copy link

clydeevans393 commented Mar 16, 2022

I can't get this to type anything that requires SHIFT. On a mac, it will work perfectly. When going from Mac clipboard to PC (windows 10) It doesn't "hit" SHIFT for capitals. Is there something I'm missing?

@641i130
Copy link

641i130 commented Mar 31, 2022

Adding this after the Linux one liner helped me with a ton of headache. (Only modifier keys would feel like they're being held down still).

sleep 0.2; xdotool keyup Control_L Control_R Shift_L Shift_R Meta_L Meta_R

@clydeevans393
Copy link

clydeevans393 commented Apr 2, 2022

sleep 0.2; xdotool keyup Control_L Control_R Shift_L Shift_R Meta_L Meta_R

Im operating from a Mac. So I'm using tell application "System Events" to keystroke (the clipboard as text)

I dont think this will work together unless Im missing something. Im new to scripts. Thanks for the reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment