Skip to main content
Version: v2.2.1

Extensions

Extensions are the heart of Espanso's dynamic matches and can be used to accomplish complex tasks.

Date Extension

The Date Extension can be used to include date and time information in a match. For example, the following match will be expanded into It's 09:30 if triggered at 9:30 am:

- trigger: ":now"
replace: "It's {{mytime}}"
vars:
- name: mytime
type: date
params:
format: "%H:%M"

The most important aspect to consider is the format parameter, which specifies how the date will be rendered.

To see a list of all the possible formatting options, follow this link to refer to the

official chrono documentation.

Future and past dates

In the previous section, we discussed how to add the current date to your matches, but you might also want to add future and past dates.

Luckily, Espanso supports the concept of offset, that is, the number of seconds to be added to the current time when expanding a match. An example will surely make it easier to understand:

Let's say we need tomorrow's date as part of a snippet. We could do that with:

- trigger: ":tomorrow"
replace: "{{mytime}}"
vars:
- name: mytime
type: date
params:
format: "%x"
offset: 86400

If you now type :tomorrow, you should see tomorrow's date appear (something like 03/20/2022)!

What made this possible was the offset parameter set to 86400, which equals to 24 hours in the future, expressed in seconds (60 seconds x 60 minutes x 24 hours = 86400 seconds).

If you need past dates, you can specify a negative offset as well. Such as:

- trigger: ":yesterday"
replace: "{{mytime}}"
vars:
- name: mytime
type: date
params:
format: "%x"
offset: -86400

Advanced: Localized dates

info

Localized dates have been introduced in version v2.1.4-beta, so make sure to have an up-to-date release.

Espanso automatically localizes dates based on your system's country and language, but advanced users might want to force a particular locale.

For this reason, the Date extension supports the locale option to override your system preferences. For example, the following snippet always expand to the current date in US format:

matches:
- trigger: ":today"
replace: "{{mytime}}"
vars:
- name: mytime
type: date
params:
format: "%x"
locale: "en-US"

The locale parameter must be specified in the BCP47 format. You can find a list of the available locales here.

Choice Extension

New in version 2.1.2

The Choice extension was introduced in version 2.1.2.

The Choice extension can be used to open a selection dialog, letting you choose the right value from a list. For example, lets consider the following snippet:

- trigger: ":quote"
replace: "{{output}}"
vars:
- name: output
type: choice
params:
values:
- "Every moment is a fresh beginning."
- "Everything you can imagine is real."
- "Whatever you do, do it well."

In this case, typing :quote will open the Search bar, letting you choose the right value:

Choice Extension

Difference with Match Disambiguation

If your goal is to choose between different replacements starting from a single trigger, you should prefer the built-in Match Disambiguation feature.

In a nutshell, Espanso automatically shows a selection dialog after typing a trigger that's shared between multiple matches. For example, the previous example is functionally equivalent to adding these 3 matches:

- trigger: ":quote"
replace: "Every moment is a fresh beginning."
- trigger: ":quote"
replace: "Everything you can imagine is real."
- trigger: ":quote"
replace: "Whatever you do, do it well."

Because all three matches share the same trigger, Espanso will let you choose the right one after typing :quote.

At this point, you might be wondering why the Choice extension was needed in the first place. The answer is that for some advanced use-cases (including scripts and other transformations), having an extension that lets you choose a value comes handy.

Advanced use with IDs

Using the Choice extension as shown in the previous section is enough for most use-cases, but it has one significant limitation: the label shown in the list and the final value are the same.

For example, if you select "Every moment is a fresh beginning." from the search bar, the value of output will be "Every moment is a fresh beginning."

For advanced use-cases, you might want to differentiate between the label (the text shown in the selection dialog), with the actual value of the item:

matches:
- trigger: ":quote"
replace: "{{output}}"
vars:
- name: output
type: choice
params:
values:
- label: "Every moment is a fresh beginning."
id: "bar"
- label: "Everything you can imagine is real."
id: "foo"
- label: "Whatever you do, do it well."
id: "foobar"

In this case, typing :quote and selecting "Every moment is a fresh beginning" will insert bar instead.

Random Extension

The Random Extension can be used to write non-deterministic replacement texts. In other words, you can specify a set of possible expansions for a match and Espanso will choose a random one, useful to avoid repetitions.

You can use this feature by declaring a variable of type random and then passing a number of choices as a parameter:

- trigger: ":quote"
replace: "{{output}}"
vars:
- name: output
type: random
params:
choices:
- "Every moment is a fresh beginning."
- "Everything you can imagine is real."
- "Whatever you do, do it well."

In this case, typing :quote will expand randomly to one of the tree quotes.

Clipboard Extension

The Clipboard Extension enables to include the current clipboard content in a match.

For example, let's imagine you want to create the ultimate HTML link shortcut:

- trigger: ":a"
replace: "<a href='{{clipb}}' />$|$</a>"
vars:
- name: "clipb"
type: "clipboard"

If you now copy a link in the clipboard (for example by selecting it and then CTRL+C) and then type :a, you'll see the following replacement appear:

<a href='YOUR_COPIED_LINK'></a>

Echo Extension

The Echo extension makes it easy to create variables containing a fixed value. For example:

- trigger: ":greet"
replace: "Hello {{myname}}"
vars:
- name: myname
type: echo
params:
echo: "John"

In this case, typing :greet gets expanded to Hello John.

This extension is particularly useful to define common global variables, such as:

global_vars:
- name: myname
type: echo
params:
echo: "John"

matches:
- trigger: ":greet"
replace: "Hello {{myname}}"

- trigger: ":sig"
replace: "Best regards, {{myname}}"

Script Extension

There will be tasks for which espanso was not designed for. For those cases, espanso offers the Script Extension, that enables you to call a script, written in any language, and use its output in a match.

To better understand this feature, let's dive into an example:

We want to use the output of a Python script as an expansion. Let's create the script.py file, place it anywhere you want and paste the following code:

print("Hello from python")

Now take note of the path of the script, and add the following match to the espanso configuration:

- trigger: ":pyscript"
replace: "{{output}}"
vars:
- name: output
type: script
params:
args:
- python
- /path/to/your/script.py

or

args: [python, /path/to/your/script.py]

If you now try to type :pyscript anywhere, you should see Hello from python appear.

You can do the same thing with any programming language, just change the args array accordingly.

script location

The current best-practice when creating Script matches is to create a scripts directory in the espanso directory (at the same level of the match and config directory) and store the scripts there.

That way, you can use the %CONFIG% wildcard to automatically replace the config directory with the correct path, such as:

- trigger: ":pyscript"
replace: "{{output}}"
vars:
- name: output
type: script
params:
args:
- python
- "%CONFIG%/scripts/script.py"
- parameter_1
- parameter_2

This makes it easier to create matches that work across many machines.

Additionally, scripts can be included inline within the expansion by changing the args:, e.g.:

          args:
- python
- -c
- |
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

but be aware that Windows has an 8191 character limit on these.

a note about performance

Because of the execution time, you should limit yourself to fast-running scripts to avoid any lag. :::

Useful Environment Variables

When triggering the shell command, Espanso also injects a few useful Environment Variables that you can use:

  • CONFIG: Points to the path of the espanso config directory
  • All the values of the previously evaluated match variables. For more information, check out the Variables section.

UTF-8 characters

By default, some scripts and shell expansions don't return UTF-8 characters, which can interfere with those generating foreign-language letters. A solution for Python scripts is to include the following in the script:

import sys
sys.stdout.reconfigure(encoding='utf-8')

For PowerShell:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Shell Extension

The Shell Extension is similar to the Script Extension, but instead of executing a script, it executes shell commands. This offers a lot of flexibility on Unix systems thanks to the bash shell (and thanks to PowerShell/WSL support also on Windows).

Let's say you regularly send your IP address to your coworkers. You can setup a match to fetch your public IP from ipify.

Note: this example uses the curl command, usually preinstalled on most Unix systems.

- trigger: ":ip"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "curl 'https://api.ipify.org'"

Now every time you type :ip, it gets expanded to your public IP address!

Like the Script extension, the Shell extension can call external shell scripts and run inline multiline code.

Choosing the Shell

The shell extension supports many different shells out of the box. By default it uses:

  • Powershell on Windows
  • bash on Linux
  • sh on macOS

You can also specify different shells by using the shell param. For example, let's say we want to use bash on Windows through the Windows Subsystem for Linux. We would use:

- trigger: ":ip"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "curl 'https://api.ipify.org'"
shell: wsl

Other possible values for the shell parameter are:

  • On Windows: cmd, powershell, wsl
  • On macOS: sh, bash
  • On Linux: sh, bash

Note that Windows cmd doesn't work with multiline inline code.

Bash pipes

This extension also supports bash pipes as your shell does, such as:

- trigger: ":localip"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "ip a | grep 'inet 192' | awk '{ print $2 }'"

Trimming the output

It's very common for commands to have outputs that also spawn a newline at the end. By default a trim option is enabled to remove any excess spaces/newlines. You can optionally disable the trim option:

- trigger: ":localip"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "ip a | grep 'inet 192' | awk '{ print $2 }'"
trim: false

Useful Environment Variables

When triggering the shell command, espanso also injects a few useful Environment Variables that you can use:

  • CONFIG: Points to the path of the espanso config directory
  • All the values of the previously evaluated match variables. For more information, check out the Variables section.

Using Linux commands on Windows

As you might have understood from previous sections, Espanso supports the Windows Subsystem for Linux through the shell: wsl parameter. This allows Windows users to execute Linux commands from their machine.

Debugging

Sometimes it's useful to understand what get's executed exactly, what are the return codes and error messages returned by the command. In order to do that, you can use the debug: true option:

- trigger: ":localip"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "ip a | grep 'inet 192' | awk '{ print $2 }'"
debug: true

At this point, after triggering a match, the logs will be populated with useful information. Too see them, use the espanso log command.

Form Extension

Espanso's forms are implemented as extensions under the hood, and therefore you can use them as you would do with any other extension.

Espanso Form

Besides regular expansions, you can also pass the values inserted inside the form to other extensions, such as scripts, opening up a world of possibilities.

For more information, please visit the Forms section.

Donate

Hi! I'm Federico, the creator of espanso. I develop espanso in my (little) spare time and I decided to make it open source because I thought it could be useful to many people.

If you liked the project, please consider making a small donation, it really helps :)

A special thanks goes to all the wonderful people who supported espanso along the way

Together, we will make espanso the first universal text expander, open to everyone.

Contributing

Espanso is open source and hosted on GitHub.

Star

If you find a bug or have an idea for a new feature, please open an issue on GitHub.