How To Automate Repetitive Typing (Text Expansion) With AutoKey On Linux
Text expansion (or substitution) helps you save time by transforming abbreviations into a long string of text. Use it to boost your productivity by quickly inserting a phrase that you need to type frequently, your address, code and so on, in any application.
This article explains how automate repetitive typing using AutoKey, a desktop automation utility for Linux / X11. Using this you'll type less and get more done, no matter the application you use, since the text expansion / substitution will work in any application - a web browser, text or code editor, and so on.
For example you can type
addr
, then press Space
, Tab
or Enter
, and AutoKey expands this to your complete address. Or you can set a keyboard shortcut in AutoKey, and pressing it automatically enters your address (or any other text you like) in the focused application. There are more advanced use cases as well, like selecting some text and wrapping it in code blocks for example.It's worth noting that since this is an X11 application, it won't work properly if you use Wayland (e.g. Fedora uses Wayland by default; if you want to use Xorg you must logout, and select "GNOME on Xorg" from the cog icon next to the Sign In button).
Related: Use Espanso Text Expander To Save Time And Increase Productivity (Linux, Windows, macOS)
Install AutoKey
Install AutoKey:
- Debian, Ubuntu, and Debian or Ubuntu-based Linux distributions like Linux Mint, Elementary OS, Pop!_OS, and so on
For Ubuntu 20.04 / Linux Mint 20 / Debian bullseye or sid and newer, install AutoKey from the repositories:
sudo apt install autokey
For Ubuntu 18.04, Debian Buster, Linux Mint 19.x, etc.: I recommend using the latest AutoKey release. For example on Ubuntu 18.04, which has AutoKey 0.90.4 (Debian Buster and older have the same version, but I didn't try it in Debian), I would get freezes quite often when trying to use this application, this being fixed after installing the latest version that's available on the AutoKey GitHub.
So in this case, download the latest AutoKey from the GitHub releases tab. You'll find DEB packages for download - get (and install) the autokey-common DEB package and either autokey-qt for the QT version (e.g. if you use Kubuntu / KDE Plasma) or the autokey-gtk for the GTK version (e.g. if you use Ubuntu with a GNOME-based desktop, including Xfce, Cinnamon, MATE, etc.).
Remove any AutoKey packages you may have installed before installing the DEBs from the AutoKey GitHub releases page:
sudo apt purge autokey*
For example, assuming you've downloaded the autokey-common and autokey-gtk DEB packages in your home folder, install them on Debian / Ubuntu using:
sudo apt install ~/autokey-*.deb
- Fedora:
- Install AutoKey-GTK:
sudo dnf install autokey-gtk
- Install AutoKey-Qt:
sudo dnf install autokey-qt
- Arch Linux or Manjaro: AutoKey can be installed from the Arch User Repository.
- If you use some other Linux distributions, either search for Autokey in the repositories, or see the installation instructions on the AutoKey project page (you can also clone the repository and run it directly without installing it).
It's worth noting that AutoKey needs a tray / appindicator to work. If you use GNOME Shell, make sure you have a tray (e.g by installing the AppIndicator Support extension -- this is not required on Ubuntu since this extension is installed by default). The tray can be used to show the main application window (AutoKey starts in the tray, having its main window hidden) and run phrases or scripts.
I should also add that AutoKey needs to be running for the text expansion abbreviations and hotkeys to work, so make sure you enable the
Automatically start AutoKey at login
option from its preferences.Text expansion on Linux using AutoKey
You can change both the abbreviation and the phrase in the instructions below to suit your needs, as this is just an example to show how AutoKey works and how to create your own text expansions.
Let's create an abbreviation that will be substituted by a phrase using AutoKey. Launch the main AutoKey window (click
Show Main Window
from the AutoKey tray menu), click on My Phrases
in the sidebar, then click New > Phrase
from the toolbar to add a new phrase. Let's call this new phrase test
in this example.In the right-hand side pane, enter the phrase that you want this to be expanded to. For this test, let's use
Testing AutoKey text expansion on Linux
as the phrase:AutoKey supports using either an abbreviation or a hotkey to insert this repetitive text. Below I'll explain how to use both of these options.
Expand text using an abbreviation
Click the
Set
button next to Abbreviations
. In the window that pops up, add test
as the abbreviation, and click the OK
button (leaving the abbreviation options to default, as seen in the screenshot below):Next, click the
Save
button in the AutoKey toolbar. After this you're ready to try your test expansion.To try this test text expansion, type
test
in a text editor, in a browser text input field or wherever you want, then press either Space
, Enter
or Tab
, or any non-word, and the text should be automatically expanded from test
to Testing AutoKey text expansion on Linux
.This is because in the abbreviation window we've used the default trigger of
All non-word
. You could change this so the text substitution only happens when you press Space and Enter
, or Tab
, from the abbreviations window:You can also set various other options there, like matching phrase case to typed abbreviation, trigger when typed as part of a word (I don't recommend this), and so on.
Inserting repetitive text using a hotkey
Abbreviations is only one way of inserting text using AutoKey. If you prefer, you can also insert text using a keyboard shortcut, which doesn't require typing an abbreviation.
To insert some text that you need to type frequently using a keyboard shortcut, click the
Set
button next to Hotkey
, then click Press to Set
in the newly opened window, and press the key combination you want to use for this phrase:Make sure you use a hotkey that's not already in use or else it may not work / it will interfere with the already set hotkey. To try this out, set it to something like
Ctrl + F1
(which shouldn't be set by the system at least).Remember to click the
Save
button in the AutoKey toolbar after setting the phrase hotkey, or else the changes won't have any effect. Now try it out: focus a text editor, some text field in a web form, etc., and press the hotkey you've set in AutoKey - the Testing AutoKey text expansion on Linux
phrase should be automatically inserted as soon as you press the keyboard shortcut.Restricting the text expansion to a particular application / window
If you want to:
- use the text expansion only with a particular application or window, or
- use the same abbreviation / hotkey in multiple applications / windows, but with a different phrase for each application or window
Then you can apply a window filter to your phrase.
To do this, press the
Set
(1) button next to Window Filter
, then click on the Detect Window Properties
(2) button in the window that pops up, and click on the window where you want this text expansion to work (3) (Gedit in the example screenshot above). After that, a new
Set Window Filter
window will pop up, asking you if you want to use the window class (so it matches that window no matter its title) or window title (so it matches that window only when it has that particular window title) -- choose the one that suits your needs, and click OK
(4).Remember to click the
Save
button in the AutoKey toolbar after setting this, or else the changes won't have any effect.Wrapping text or code around existing text
One of the main reasons I started using AutoKey is to be able to wrap some code around existing text. E.g. I can select
this text
, then press a keyboard shortcut, and this text
becomes <code>this text</code>
.AutoKey actually has an example for performing this task (and some other advanced tasks) built in, but I thought I'd mention this anyway, since it may not be clear this is both possible and easy to do with this neat application:
text = clipboard.get_selection()
keyboard.send_key("<delete>")
keyboard.send_keys("The text %s was here previously" % text)
You will find this example in the AutoKey
Sample Scripts
in its sidebar, with the Selection Test
name.When assigned a keyboard shortcut, this script replaces the current selected text with
The text %s was here previously
, where %s
is the original selected text. Example: if you select the word
TEST
, then press the hotkey assigned to this script, TEST
is replaced with The text TEST was here previously
(this works in an editable text field so don't try it on this web page as it won't work).This way you can for example wrap
<code>
</code>
or ``
``
around selected text, e.g.:text = clipboard.get_selection()
keyboard.send_key("<delete>")
keyboard.send_keys('<code>%s</code>' % text)
Using this, selecting
TEST
and pressing the assigned hotkey changes TEST
to <code>TEST</code>
.As a side note, make sure you're using AutoKey v0.95.4 or newer if you want to place
<code>
and </code>
literals in Phrases, or if you want to send this using the keyboard.send_keys
function, as it wasn't possible before this version due to a bug.See the AutoKey wiki for frequently ask questions, known limitations, and some sample scripts.