Is there a way to assign text snippets to keystrokes

I want to create a keystroke that enters ones email address, postal address, city/state, directory name. How can one create these mappings?


Solution 1:

There is no really "obvious" way, i.e., different approaches exist.

For the current legacy graphical server: Xorg

Likely the most accessible for newer users is the keyboard macro program Autokey. It offers a graphical user interface to define text snippets and expansions. You can make a shortcut key type certain phrases, but even better, it can watch your typing and when you type an abbreviation you defined (e.g. :add) it will expand that into a full phrase.

Espanso works in a similar way, but may be harder to learn to setup. You configure it through text files. The documentation is excellent, though.

Many people use custom scripts, that use automation tools like xdotool to simulate typing keystrokes, and xclip or xsel to manipulate the clipboard. Such a script is called using a shortcut key, and then allows to quickly select and insert the snippy. Jacob Vlijm published a ready-to-go script on Askubuntu. Snippy text expander is a script written in bash. In both examples, you put your text snippets as little text files in a defined folder.

A very simple approach using only xdotool assigned to a shortcut key

Single line xdotool commands assigned to a shortcut key

For very basic needs, you could assign a command that emulates some typing to a shortcut key. For example, if you wish Shift + F1 to type your full name, then you could assign following command to the Shift+F1 key.

xdotool keyup Shift+F1 type "My full name"

Remark keyup simulates releasing the shortcut key again. If the script kicks in while you are still holding Shift, otherwise, caps would be typed instead.

Multi-line xdotool scripts

For more elaborate snippets, you better create a small xdotool script. For example, create a script address in a folder, for example in a folder "Snippets" under your home folder. Enter the following text and save.

#!/usr/bin/xdotool
keyup Shift+F2
type "My full name"
key Return
type "Street and number"
key Return
type "Zip and City"

Remark: The shebang line points to the xdotool executable. This way, xdotool will be loaded to execute the commands that follow. A new line is entered with the "Return" key (Enter).

Make the text file executable using the File Properties dialog in your file manager.

Assign a shortcut key, e.g. Shift+F2 to the script: as command, specify the full path name to the script, e.g. /home/<yourlogin/Snippets/address.

For the future graphical server: Wayland

These tools all work well on systems using the Xorg graphical server. Nowadays, a move is ongoing to a replacement of Xorg, Wayland. All tools mentioned above do not work anymore on Wayland. The developper of Espanso is working on Wayland support. Command line tools to manipulate the clipboard (wlclipboard) and to type characters (ydotool, evemu) are becoming available for Wayland, but I am not aware of ready-to-use snippet scripts thus far.