7

I am looking to avoid ever typing in my email address again. Is there a way to configure a key to paste some pre-set bit of text like my email.

I'd like this to work across all applications ideally.

thanks

Braiam
  • 66,947
  • 30
  • 177
  • 264
henryw374
  • 173
  • 1
  • 5

3 Answers3

8

1) Install xclip and xdotool

sudo apt-get install xclip xdotool

2) Make a file myscript.sh

mkdir ~/bin
gedit ~/bin/myscript.sh

3) Copy paste this script into it

#!/bin/bash
TEXT="myname@address.com"
printf '%s' "$TEXT" | xclip -i -selection clipboard
xdotool key --clearmodifiers "ctrl+v"

4) Make the file executable

chmod +x ~/bin/myscript.sh

5) Make a shortcut

Settings -> Keyboard -> Shortcuts -> Custom shortcuts -> click [+]

  • Name: anything, Command: /home/USERNAME/bin/myscript.sh
    • Replace the USERNAME with your username
  • Click on the Disabled text and then press your magical keystroke. For example Ctrl-Shift-S
  • You may have to restart unity with setsid unity or by logging in and out to make the shortcut work. (at least i had this bug on Ubuntu 13.10)

Troubleshooting

If you have any problems with pasting the text. Try changing xdotool key to xdotool sleep 0.5 key. It gives you time to take your hands off the keyboard before xdotool tries to press "Ctrl-V"

squirl
  • 103
  • 4
Niko Föhr
  • 1,441
  • 2
  • 16
  • 25
  • For me, my TEXT variable had spaces in it. In order for that to work properly, I had to use `printf "$TEXT"` instead of `printf '%s' $TEXT`. What is the advantage of using the `%s`? – Garrett Jun 09 '15 at 03:42
  • If you don't want this script to purge you current clipboard, then at the beginning, put `OLD_TEXT=$(xclip -o -selection clipboard)` and at the end put `sleep 1; printf "$OLD_TEXT" | xclip -i -selection clipboard`. – Garrett Jun 14 '15 at 05:56
  • 1
    I am not going to argue that this will work, but isn't there some kind of tool that makes this a little less hand-built? – Shadoninja Jul 27 '16 at 14:42
  • @Shadoninja There's AutoKey, which tries to tackle this, but for me when it pasted it dropped letters randomly from the paste text. – mlissner Aug 22 '16 at 17:14
  • That edit is definitely necessary for me. Any way to get it to do new lines? I've tried adding `\n` to my TEXT variable, but it doesn't seem to work. In Firefox it's all one line, but in gedit, it's OK. – mlissner Aug 22 '16 at 17:46
  • 1
    Worth pointing out: one might try to circumvent the clipboard by setting the shortcut command to something like `xdotool type --clearmodifiers myname@myaddress` , but that ends up being flaky. – Don Hatch Jun 06 '19 at 10:52
1

The previous reply is from 2013 and we need something that works on 2021.

xdotool doesn't work on wayland.

xdotool does not always work as expected (i.e. it's sometimes very flaky).

An alternative tool to xdotool is ydotool.

1. install ydotool and xclip

sudo apt install -y ydotool xclip

2a. if you want to type text from the clipboard

Copy/paste is disabled on some applications and you may want to use this tool to type text instead of pasting.

Create a file ~/bin/myscript.sh

#!/bin/bash

xclip -o | ydotool type --delay 1000 --key-delay 70 --file -

2b. if you just want to type a text

This is easier.

Create a file ~/bin/myscript.sh

#!/bin/bash

TEXT="ad eundem rivum lupus et agnus venerant"
ydotool type --delay 1000 --key-delay 70 $TEXT

3. create a shortcut

Settings -> Keyboard -> Shortcuts -> Custom shortcuts -> click [+]

  • Name: anything, Command: /home/USERNAME/bin/myscript.sh
    • Replace the USERNAME with your username
  • Click on the Disabled text and then press your magical keystroke. For example Ctrl-Shift-S
maxadamo
  • 142
  • 7
1

Try espanso which is a pretty good system-wide text expander:

https://espanso.org/