macro to paste pre-configured text

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="[email protected]"
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"


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

https://espanso.org/

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