Espanso: A Wonder Tool for Writers


Espanso is an open source cross-platform text expander based on the ‘Rust’ language. This tool has some amazing capabilities that make your writing effective, such as preventing typing the same text repeatedly. This article will show you how to install and work with Espanso.

You may have noticed when writing emails that you write the same chunks of text over and over. This could be the greetings at the beginning of the email or the signature at the end. Those parts hardly change. The emails you receive from others, be it your service provider or the shopping portal you have visited in the past, are also very generic in nature.

Most of the emails you receive in your personal accounts consist of marketing emails from the sites and services you use or visit. In all of these emails, the text is almost identical and generic except for the name of the person it is addressed to.

Some of these texts are produced by the text expander — it is a tool that can produce a chunk of text based on the text input. In simple words, a text expander produces expanded text from a single or finite number of words.

Who needs Espanso?
Espanso is useful for everyone, but people who write a lot of reports everyday may find this tool an invaluable one, because it saves a lot of time and improves the quality of text. Especially when you’re writing a lot of text involving technical jargon and you use those terms frequently, Espanso can be a great help.

You can also use emojis, images and snippets in your text without using any complicated keystroke combination, or copying and pasting such items in your text. They can be incorporated neatly with very few simple lines of code. Espanso is easily portable to many machines — all you have to do is copy the files (for example, source docs such as images, snippets, etc, as well as your configuration files) so that you can use the existing configurations in the new machine straightaway.

Installing Espanso
In this article, you will see how to install Espanso in a Debian based Linux machine. Espanso offers prebuilt packages for Debian and Arch based systems. For Debian based systems, you can use the Snap packaging system or the .deb file format to install.

For the rest of the operating systems you can use the manual installation process, which is explained in the Espanso website. After you’re done installing Espanso, you need to install modulo, which is required to create forms that you will see later in the article. Once the installation is complete, you can verify it by entering the following command in a terminal:

$ espanso start

If the installation is successful, you will see a notification that Espanso is running. Now type :espanso in any text field, and it will get replaced by the text ‘Hi there!’

It is important to know where the configuration file for Espanso is located. For that, enter the following command:

$espanso path

The above command will show the location of the Espanso files, namely:

  • Config: This shows the location of the configuration file, which contains the code for how Espanso will behave.
  • Packages: This location contains the additional packages, which will expand the capabilities of Espanso further.
  • Data: This shows the location of the other data files necessary for Espanso.

Understanding triggers and matches
Espanso is built on the core components ‘Triggers’ and ‘Matches’. Put in simple terms, in Espanso you can create a trigger and match, so that whenever Espanso meets a trigger the corresponding match is replaced. Let us understand this with the following example.

Imagine you’re sending an email to your superior containing a report of your day’s work. That email will contain the following parts:

  • Greetings to the receiver
  • Date of the report
  • Message in the body of the email
  • Signature of the sender

In the above list, the greetings to the receiver and signature of the sender are almost constant. The rest of the list is somewhat dynamic. Now let’s create a trigger and match for greetings to the receiver.

Open the default.yml file, which is located in the configuration folder of Espanso. The configuration file offers very few triggers and matches, but you can extend them as per your requirements. Before editing the default.yml file, you must understand that indentation is important. It is only through proper indentation that you can segregate the various triggers and matches. So let’s place the following lines in the bottom of the default.yml file:

- trigger: “:greet_will”
  replace: “Hello William Parker”

After that, Espanso has to reload the configuration so that new changes may take effect. This is achieved by using a file watcher. Once you save any changes you make in the configuration file, Espanso will automatically reload the configuration file.

Now if you type greet_will, it will be replaced by the words Hello William Parker.

Let’s now create a trigger and match for the signature part of the email. This time you will want to make the words or sentences appear in separate lines:

- trigger: “:sig”
   replace: “Nate Alexander,\nDeputy Manager\nQuality Assurance Department”

The above code will produce the following output:

Nate Alexander,
Deputy Manager
Quality Assurance Department

The text inside the replace part can be of any length. This offers flexibility to use the chunks of text of variable sizes as per the need.

Till now, you have worked with the static parts of the email. Now let’s take up the dynamic parts of the email.

In our email example above, you may wish to add the date of the report you’re about to send. The date can be written as July 30, 2021. Instead of that, you can create a trigger named :present_date as follows:

- trigger: “:present_date”
  replace: “{{mydate}}” 
      - name: mydate
   type: date
                  format: “%B %d, %Y

Compared to the previous code examples, this one will be lengthy. Let’s break it down into parts so that you can understand better.

1) – trigger: “:present_date”: This defines a trigger named :present_date.

2) replace: {{mydate}}: In this line, instead of directly supplying the substitution, the
variable is declared to hold the dynamic content. In this case, it is ‘mydate’.

3) vars:

- name: mydate
type: date

The above block shows the definition and type of the variable. Type of variable in this context simply means what extension will be executed to produce output for the variable mydate. In our example, you are using the date extension:

4) params:

format: “%B %d, %Y”

The above code shows the parameters for the variable. In this example, you are using the parameter format, which will decide how the date shows up. There are many options for formatting the date, where you can customise it according to your needs.

Now if you type :present_date in any text area, you will get the following output:

July 29, 2021

But what if you want to treat a trigger word to be a normal one? For that, you can just use the backspace key to prevent the word substitution and preserve the trigger word just as it was typed.

If you want to use Espanso as an automatic correction tool you must be careful, because whatever you’re defining in the replace part should affect only the exact word in the ‘trigger’. For example, if you often type the name dodd as dod incorrectly, and you write the following code:

- trigger: “dod”
replace: “dodd”

…every time you type dod it will be replaced by the word dodd, just as you intended. But the problem here is the word dodge partially matches with your trigger; so when you try to type dodge after typing its first three letters, they will be replaced by the word dodd. And that is something not intended. This can be solved by adding a line to the above code as:

- trigger: “dod”
replace: “dodd”
word: true

The last line instructs that replacement should happen only if the trigger is a separate word. So if you’re using dod and dodge in the same sentence, only the word dod will be replaced as dodd, but the word dodge will remain as it is.

Another neat feature in Espanso is that be shown in the same case as the trigger word added in the text area.

For example, suppose you want the trigger word westros in the same case as it is being typed in (lower case, sentence case or upper case).

In the default.yml file, add the following lines:

- trigger: “westr”
replace: “westros”
propagate_case: true
word: true

The above code will replace the case of the word exactly as it is typed – for example, Westr, westr, ‘WESTR’ will be replaced as Westros, westros, WESTROS.

Espanso doesn’t limit itself to text alone. It can also insert an image from your triggers. To add an image in your text, insert the following code in your default.yml file:

- trigger: “pic_sam”
image_path: “/home/magimaiprakash/Pictures/sam.jpg”

Here, the only requirement is that you need to provide the location of the image. This will come in handy when you want to add personal photos or company logos, etc.

Shell commands
In Espanso, you can actually replace the shell command by specifying a trigger. For example, if you want to know the version of the Ruby programming language installed in your system, insert the following code in the configuration file:

- trigger: “ruby version”
replace: “{{output}}”
- name: output
type: shell
cmd: “ruby -v | awk ‘{print $2}”

The above code will produce the version of the Ruby interpreter:

ruby 2.7.2p137

Sometimes you may need to execute external scripts that may be required in your text. To do this, you can use the Script extension. A typical ‘Hello world’ program in Ruby can be executed by the following code:

- trigger: “hello_ruby”
replace: “{{output}}”
- name: output
type: script
- ruby
- “/home/kmp/.config/espanso/user/scripts/hello_ruby.rb”

This will produce the following output:

Ruby Says Hello

Espanso is capable of creating simple forms. These are extremely useful when you’re using static and dynamic elements. So, for example, if you want to send out a birthday wish, you can use forms to keep a constant birthday message with only the names changing.

A simple form for birthday wishes can be created with the following snippet:

- trigger: “:bday_greet”
form: |
Hey {{name}},
Happy Birthday!
Birthday wish form
Figure 1: Birthday wish form

After reloading the configuration, whenever you type bday_greet, a small form will open; you need to enter the name of a person, and it will produce the following output:

Hey Trudy,
Happy Birthday!

You can further extend a form by creating a drop-down list by using the choices option. This will come in handy when you have multiple words or sentences that you wish to use in your text:

- trigger: “:form”
form: |
type: choice

So now when you type :form, a small window will come up, where you can choose from the drop-down box as per your need.

Espanso form
Figure 2: Espanso form

If you prefer lists over the choices option, then you can modify the above code as follows:

- trigger: “:time_zone”
form: |
type: list

Espanso capabilities can be extended by adding a package using the following command:

$ espanso install package_name
Espanso list
Figure 3: Espanso list

This will fetch the requested package from the Espanso hub and install it on your system; after that you need to restart the Espanso service by using the command:

$ espanso restart

To uninstall a package, enter the following command in the terminal:

$ espanso uninstall package_name

Espanso is a very useful tool for working with a lot of text. One can easily reduce all the repetitive work currently being done. But it is still a work in progress and has many promising features in store.


Please enter your comment!
Please enter your name here