Global Variables
Global variables let you define a value once and reuse it across all matches in a file. This avoids repetition and keeps your match files DRY.
Defining Global Variables
Add a global_vars section above the matches section:
global_vars:
- name: myname
type: shell
params:
cmd: "whoami"
matches:
- trigger: ":greet"
replace: "Hello {{myname}}!"
- trigger: ":sig"
replace: |
Best regards,
{{myname}}
Both :greet and :sig use the same {{myname}} variable.
Practical Examples
User Information
global_vars:
- name: hostname
type: shell
params:
cmd: "hostname -s"
- name: ip
type: shell
params:
cmd: "hostname -I | awk '{print $1}'"
matches:
- trigger: ":sysinfo"
replace: "Connected to {{hostname}} ({{ip}})"
Project Context
global_vars:
- name: project
type: shell
params:
cmd: "basename $(git rev-parse --show-toplevel 2>/dev/null || echo 'unknown')"
matches:
- trigger: ":branch"
replace: "{{project}}/$(git branch --show-current)"
- trigger: ":root"
replace: "$(git rev-parse --show-toplevel)"
Reusable Values
global_vars:
- name: date_fmt
type: date
params:
format: "%Y-%m-%d"
matches:
- trigger: ":today"
replace: "Today: {{date_fmt}}"
- trigger: ":report"
replace: "Report generated {{date_fmt}}"
Override Behavior
- Global variables are available to all matches in that file
- A match with its own
varscan override a global variable of the same name - Global variables from one file do not apply to other match files
What's Next
- Learn about Nested Matches for including one match inside another
- Explore Variable Chaining for complex workflows