Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active February 4, 2025 14:16
Show Gist options
  • Save smeech/b87ca281c654ee5d6dd959125e584fc9 to your computer and use it in GitHub Desktop.
Save smeech/b87ca281c654ee5d6dd959125e584fc9 to your computer and use it in GitHub Desktop.
[Espanso Search] Scripts to find and list Espanso trigger/regex values. #espanso #bash #python #pwsh
# PowerShell script to search for Espanso triggers in YAML files
# Usage:
# .\search_espanso.ps1 ":test"
# OR
# pwsh -File "C:\path\to\search_espanso.ps1" ":test"
param(
[string]$searchWord
)
if ($IsWindows) {
$ConfigDir = [System.Environment]::GetFolderPath("ApplicationData") + "\espanso\match"
} else {
$ConfigDir = "$env:HOME/.config/espanso/match"
}
if (-not (Test-Path $ConfigDir)) {
Write-Host "Error: Directory $ConfigDir does not exist."
exit 1
}
# Simplified regex pattern to match trigger, triggers, and regex keys without quoted values
$pattern = "^\s{2}-\s*(trigger|triggers|regex):\s*([^\s]+)"
Get-ChildItem -Path $ConfigDir -Recurse -Filter "*.yml" | ForEach-Object {
$filePath = $_.FullName
if ($filePath -match "\.yml~$") { return }
try {
Get-Content -Path $filePath | ForEach-Object {
if ($_ -match $pattern -and $_ -match [regex]::Escape($searchWord)) {
$relativePath = $filePath.Substring($ConfigDir.Length).TrimStart("\","/")
Write-Host ("${relativePath}: $_")
}
}
} catch {
Write-Host "Error reading `${filePath}`: $_"
}
}
#!/usr/bin/env python
import os, re, sys
def search_espanso_triggers(search_word, config_dir):
if not os.path.exists(config_dir):
print("Error: Directory {} does not exist.".format(config_dir))
return
pattern = re.compile(r'^ -\s*(trigger|triggers|regex):\s*("[^"]*"|\'[^\']*\'|[^"\']*)')
for root, _, files in os.walk(config_dir):
for file in files:
if file.endswith(".yml") and not file.endswith(".yml~"):
file_path = os.path.join(root, file)
try:
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
match = pattern.match(line)
if match and search_word in line:
print("{}: {}".format(os.path.relpath(file_path, config_dir), line.strip()))
except Exception as e:
print("Error reading {}: {}".format(file_path, e))
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python search_espanso.py <word>")
sys.exit(1)
search_word = sys.argv[1]
config_dir = os.path.expanduser("~/.config/espanso/match")
search_espanso_triggers(search_word, config_dir)
@smeech
Copy link
Author

smeech commented Feb 4, 2025

All developed in conversation with ChatGPT, so apologies for the style!
I've not tested them on Windows, so I'll be interested in any comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment