Automate Formatting with VS Code Copilot Hooks

VS Code Copilot hooks run shell commands like Prettier at agent lifecycle events, such as post-tool use, to auto-format code after AI edits without manual work.

Hooks Execute Commands at Agent Lifecycle Points

Hooks in VS Code Copilot let you trigger custom shell commands during specific agent session events, like session start, user prompt submission, or post-tool use. This automates workflows, enforces security policies, validates operations, and integrates external tools. For formatting, target the post-tool use event to run commands after the agent edits files, ensuring code stays clean automatically.

The official VS Code docs highlight running Prettier as a key example: it formats files right after edits, preventing unformatted code from persisting. Lifecycle events dictate invocation timing—post-tool use fits formatters perfectly since it follows agent modifications.

Create and Test a User-Level Prettier Hook

Generate hooks via Copilot by prompting it in agent customizations: request a user-level Copilot hook using post-tool use with a shell script for Prettier. Copilot creates the config, typically something like:

{
  "hook": {
    "postToolUse": {
      "command": "prettier --write ${file}"
    }
  }
}

Reload the VS Code window after generation. Test by asking the agent to edit a file, like rewording a README paragraph. The agent changes content (e.g., lines 7, 18, 20 unformatted), then the hook invokes Prettier, auto-formatting everything.

Remove unnecessary options like timeouts from the generated config for simplicity—docs don't require them. This setup handles background cleanup reliably, so you focus on prompts, not formatting.

Trade-offs and Extensions

Hooks shine for quiet automation but tie to agent sessions, so they're Copilot-specific. For broader use, explore community customizations in awesome-copilot repos. Common extensions: linting, git commits, or security scans at other lifecycle points. Start with formatters to see immediate gains in code hygiene, then layer on validations to catch issues early.

Summarized by x-ai/grok-4.1-fast via openrouter

4024 input / 1304 output tokens in 17086ms

© 2026 Edge