Add/manage snippets in Espanso

Mr. Panini's solution for expansion snippets is great. But sometimes you want to share snippets between macOS and Windows: enter Espanso.

There was a previous mention here, that listed the advantages. One disadvantage mentioned was the fact that you have to edit a configuration file.

As a nice learning project and for possible replacement of TextExpander 5.1.4 I'd like to investigate if I can replace TE with a combination of Espanso for the snippets and Keyboard Maestro for the nice menu item structure (to insert log in combination, boiler plate texts for correspondence etc.)

Let's begin.

Espanso's snippets are stored here:

~/Library/Application Support/espanso/match/base.yml

A short test learns that snippets are recognized when added to the end of this file:

# espanso match file

# For a complete introduction, visit the official docs at: https://espanso.org/docs/

# You can use this file to define the base matches (aka snippets)
# that will be available in every application when using espanso.

# Matches are substitution rules: when you type the "trigger" string
# it gets replaced by the "replace" string.
matches:
  # Simple text replacement
  - trigger: ":espanso"
    replace: "Hi there!"

  # NOTE: espanso uses YAML to define matches, so pay attention to the indentation!

  # But matches can also be dynamic:

  # Print the current date
  - trigger: ":date"
    replace: "{{mydate}}"
    vars:
      - name: mydate
        type: date
        params:
          format: "%m/%d/%Y"

  # Print the output of a shell command
  - trigger: ":shell"
    replace: "{{output}}"
    vars:
      - name: output
        type: shell
        params:
          cmd: "echo 'Hello from your shell'"

  # And much more! For more information, visit the docs: https://espanso.org/docs/
  
  - trigger: "xtst"
    replace: "Hi there, this is a test!"

So it will be easy to add new snippets via a simple Keyboard Maestro dialogue box with two input boxes and two buttons. I'll work on that later:


Add snippet to Espanso.kmmacros (2.0 KB)

For the management feature I was thinking about a Prompt with List. But how to get the existing snippets out of the base.yml file? I'll probably need your help there :).

That page will not be accessible to all, so I shall repeat myself a little here. :slight_smile:

I mentioned an aspect of that that did hold some appeal for me while I was considering Espanso...

Of course, the use of plain text files also means that the data is totally accessible to KM macros. I have not yet found the need to create macros, and for now, I am quite happy just editing the .yml files. I shall roll my sleeves up when I feel the need to!

To create a new entry, I use the following Espanso text expansion:

    replace: "\n  -$|$ trigger: \nreplace: \n"

That creates a new entry and places the cursor after "trigger: " (that sort of thing would be easy to achieve in a KM macro too).

I would start with a for each action using lines in collection.

Each expansion will begin with a line that starts with - trigger:

Other lines (for replace:, word: or whatever) could then be handled line by line, until the next trigger line.

1 Like