| name | macro-triggers |
| description | Trigger syntax for Macros .csx files: events, commands, filters, and header edits |
| domain | visual-studio-macros |
| confidence | high |
| source | derived from docs/triggers.md and CSharpCodeGenerator.EmitHeader |
Macro triggers
Add // @trigger lines to the top comment block so a macro runs automatically on IDE events or commands.
When to use this skill
Use this skill when the user wants a macro to run on save, build, solution open, debugger events, or before/after a named Visual Studio command.
Do not use this skill for VSIX event handlers, MEF listeners, command filters, or other extensibility plumbing.
Trigger directive syntax
A trigger is always a top-of-file comment line:
Rules:
- Put trigger lines in the header comment block before any executable code
- Multiple trigger lines are OR-combined; any one can start the macro
- If you omit triggers, the macro is effectively manual
Event triggers
Event triggers use {Category}.{EventName}:
Common categories in this repo include Build, Document, Solution, Debugger, Selection, Window, and Shell.
Practical example:
#load ".intellisense/Macros.Intellisense.csx"
await ExecuteCommandAsync("Edit.FormatDocument");
await VS.StatusBar.ShowMessageAsync("Formatted after save");
Command triggers
Command triggers attach to named VS commands:
Use BeforeCommand to intervene before execution and AfterCommand to react after execution.
#load ".intellisense/Macros.Intellisense.csx"
if (DTE.Solution.SolutionBuild.BuildState == EnvDTE.vsBuildState.vsBuildStateInProgress)
{
await VS.MessageBox.ShowWarningAsync("Macros", "Cannot save during a build.");
Trigger.CancelCommand();
}
when filters
Add when key=value after the trigger name to narrow when it fires.
Common filters:
filename=*.cs
success=false
project=*Tests*
exception=*NullReferenceException*
Filter rules:
- Multiple
key=value pairs are AND-combined
filename is a glob, not a regex
success is a boolean
- Unrecognized keys are ignored by the trigger system
How to add a trigger to an existing macro
Existing file:
#load ".intellisense/Macros.Intellisense.csx"
await ExecuteCommandAsync("Edit.FormatDocument");
Add a trigger line inside the header block:
#load ".intellisense/Macros.Intellisense.csx"
await ExecuteCommandAsync("Edit.FormatDocument");
Do not move the trigger below #load or below executable statements.
Generated EXAMPLE lines
Generated macros include examples like these:
To activate one, remove only the // EXAMPLE: prefix.
Good trigger recipes
Good defaults for Copilot
- Put
// @trigger ... in the header block
- Use
Document.Saved or Document.Opened for document automation
- Use
BeforeCommand only when cancellation/interception matters
- Use
AfterCommand for logging or follow-up work
- Add
when filters instead of branching later when the condition is simple