22

How can I configure a shortcut key to send a text string to the current program?

The purpose is to type common entries quicker (email address, street address, phone number, username, favorite quote, etc).

I don't need any further automation than just entering the text.

Gentoo Linux (3.2.12-gentoo) Xfce Desktop Environment (Version 4.8)

Steven
  • 726
  • 1
  • 7
  • 14

5 Answers5

25

One simple approach is xdotool, like

xdotool type 'text'
jw013
  • 50,274
  • 9
  • 137
  • 141
  • That's half the solution... how do I assign a keyboard shortcut to run that? Making a shortcut in Xfce 4.8 Settings, Keyboard didn't work. – Steven Apr 20 '12 at 02:24
  • @Steven Does it work directly from a terminal? – jw013 Apr 20 '12 at 02:31
  • 4
    Yes. Also, 'sleep 5 && xdotool type text' sends the text to the "active" window. – Steven Apr 20 '12 at 02:35
  • 2
    @Steven I haven't used Xfce in a while, but my next hunch is that when the command is run, the active window is wrong somehow. I'd try running `xdotool getwindowfocus` both from a terminal and as a keyboard shortcut while the same terminal is open and compare the results for equality. You'll probably have to redirect the keyboard shortcut version to a temporary file in order to see it. – jw013 Apr 20 '12 at 02:40
  • 4
    I use `xbindkeys`. – Mikel Apr 20 '12 at 02:47
  • Combining all of the above with `xvkbd` worked for me. `xbindkeys` for the keyboard shortcut and `xvkbd` for the typing. Adding a brief sleep was also required, so the final `.xindkeysrc` command looks like `"sleep 0.1 && xvkbd -text me@howdy"` This substitutes for textexpander for macos on linux – here Dec 14 '14 at 07:31
  • This doesn't work in Ubuntu 18.04 – Conjecture Aug 26 '19 at 22:43
7

The xdotool works. However if you integrate it with the keyboard shortcut .. a simple xdotool type 'text' fails.

THe following shell script helped me in that

windowid=$(xdotool getwindowfocus)
sleep 0.5 && xdotool windowactivate --sync $windowid type 'text'
user1958007
  • 71
  • 1
  • 1
  • 2
    The manuals for `xdotool` show that it has an internal sleep. `xdotool sleep 0.3 type 'Emotional 2003'` suffices. (The short sleep is necessary for some reason, I don’t know. It seems like there is a short switch of the activated or focused window when running a command?) – k.stm Nov 15 '18 at 17:40
5

You may use AutoKey. Here is the description from the web site. “AutoKey is a desktop automation utility for Linux and X11. It allows you to manage collection of scripts and phrases, and assign abbreviations and hotkeys to these. This allows you to execute a script or insert text on demand in whatever program you are using.”

It is packaged, as far as I can tell, for Gentoo (announced here); for Ubuntu (here); Debian (here)...

Michael Kohne
  • 1,169
  • 9
  • 15
2

One-liner that worked for me (stolen from the comment by k.tsm)

xdotool sleep 0.3 type 'string that you want to type / paste'

Then assign a shortcut to this command in KDE and after your pressing the shortcut, the required text is magically typed for you.

user3804598
  • 189
  • 1
  • 6
0

you can use this to control open file dialog input in linux :

import pyautogui
import subprocess , time

pyautogui.hotkey('ctrl', 'o' , interval = 0.15)
pyautogui.hotkey('ctrl', 'l' , interval = 0.15)
subprocess.Popen("xdotool sleep 0.3 type  '{Path_file}'".format(Path_file = "............................") , shell = True)
time.sleep(10)
pyautogui.press('enter')