Last active
February 4, 2025 14:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a search word is provided | |
if [[ -z "$1" ]]; then | |
echo "Usage: $0 <word>" | |
exit 1 | |
fi | |
SEARCH_WORD="$1" | |
CONFIG_DIR="$HOME/.config/espanso/match" | |
# Recursively search for matching values in .yml files, ensuring correct indentation and removing base path | |
find "$CONFIG_DIR" -type f -name "*.yml" ! -name "*.yml~" -print0 | | |
xargs -0 grep -E "^ -\s*(trigger|triggers|regex):\s*(\"[^\"]*\"|'[^']*'|[^\"']*)" | | |
grep --color=auto "$SEARCH_WORD" | | |
sed "s|$CONFIG_DIR/||" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}`: $_" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.