Advanced GitHub Copilot Customization: Tailoring Your AI Assistant

GitHub Copilot can be significantly enhanced and personalized to better suit your development workflow by leveraging custom Markdown files within your Git repository’s .github folder. These files allow you to define specific behaviors and preferences for the AI assistant, categorized into three main types: Instructions, Prompts, and Chat Modes.

1. Custom Instructions for Guided AI Assistance

Instruction files provide Copilot with a set of guidelines and preferences, ensuring its suggestions and interactions align with your project’s standards and your personal coding style.

To implement this, create a file named copilot-instructions.md within your repository’s .github directory. Within this file, you can define various rules, such as general coding practices, preferred file change methodologies, workflow specifics, and coding standards.

Example copilot-instructions.md structure:

# Copilot Instructions

## General Guidelines
- Adhere to instructions provided in the instructions folder.
- File Changes: Only modify files with complete contextual understanding. Prioritize reviewing extensive code sections before making alterations.
- ...

## Workflow

...

## Coding Standard

...

Once in place, these instructions are automatically incorporated into Copilot’s chat context, influencing its responses and code generation.

For even more granular control, you can define path-specific instruction files (e.g., typescript.instructions.md) inside a .github/instructions subdirectory. By including a YAML front matter like:

---
applyTo: "**/*.ts,**/*.tsx"
---

You can ensure that these instructions are applied only to the specified file types, such as TypeScript files.

2. Reusable Prompt Templates

Prompt files enable the creation of reusable prompt templates, streamlining common tasks and ensuring consistency in how you interact with Copilot.

To create a prompt, place an xxx.prompt.md file (e.g., react-form.prompt.md) within the .github/prompts folder. These files can include metadata such as the desired AI model, tools Copilot should utilize, and a description of the prompt’s purpose.

Example react-form.prompt.md:

---
mode: 'agent'
model: GPT-4o
tools: ['githubRepo', 'codebase']
description: 'Generate a new React form component'
---
Your objective is to create a new React form component based on the templates found in #githubRepo contoso/react-templates.

If not provided, inquire about the form's name and its required fields.

Requirements:
* Utilize form design system components: [design-system/Form.md](../docs/design-system/Form.md)
* Implement `react-hook-form` for managing form state.
* Always define TypeScript types for your form data.
* Favor *uncontrolled* components using the `register` method.
* Employ `defaultValues` to prevent unnecessary re-renders.
* Incorporate `yup` for validation.
* Develop reusable validation schemas in separate files.
* Use TypeScript types to ensure robust type safety.
* Customize validation rules for an improved user experience.

To invoke a saved prompt, simply type / followed by the prompt’s filename in the Copilot chat window.

3. Custom Chat Modes for Specialized Tasks

Chat Modes allow you to define specialized interaction modes for Copilot, optimizing its behavior for different development tasks like planning, debugging, or code review.

To set up a custom chat mode, create an xxx.chatmode.md file (e.g., planning.chatmode.md) inside the .github/chatmodes directory. Similar to prompts, these files can specify the AI model, available tools, and detailed instructions for the mode.

Example planning.chatmode.md:

---
description: Generate an implementation plan for new features or refactoring existing code.
tools: ['codebase', 'fetch', 'findTestFiles', 'githubRepo', 'search', 'usages']
model: Claude Sonnet 4
---
# Planning mode instructions
You are now operating in planning mode. Your primary task is to generate a comprehensive implementation plan for either a new feature or for refactoring existing code.
Refrain from making any code edits; your sole responsibility is to produce a plan.

The plan should encompass the following sections:

* ***Overview*** – A concise description of the feature or task.
* ***Requirements*** – A comprehensive list of all necessary requirements.
* ***Implementation Steps*** – A detailed, step-by-step plan for implementation.
* ***Testing*** – A description of the tests required to verify the implementation.

You can then select your custom chat mode directly from the chat window interface, enabling Copilot to adopt the predefined persona and toolset for the task at hand.

Getting Started

You don’t need to craft these customization files from scratch. Resources like Awesome Copilot offer a wealth of templates that you can adapt and integrate into your projects as needed. These customization capabilities empower developers to fine-tune GitHub Copilot, making it an even more powerful and integrated part of their coding environment.

Further Reading:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed