Try in VS Code!

Tips and tricks for Copilot in VS Code

This article provides tips and tricks to optimize your development experience for using Copilot in Visual Studio Code.

Checklist for using Copilot in VS Code

Use the following checklist to get the most out of Copilot:

  1. Choose the right tool. Use the tool that's optimized for editing, asking questions, or staying in the flow of writing code.

  2. Personalize Copilot. Use custom instructions to get code suggestions that match your style and coding practices.

  3. Write effective prompts and provide context. Get the most relevant responses.

  4. Index your workspace. Receive accurate responses to questions about your codebase.

  5. Choose your AI model. Choose between models for fast coding or planning/reasoning.

  6. Reuse prompts. Save time by saving and reusing task-specific prompts across your team.

Choose the right Copilot tool

Depending on your task, you can choose between different Copilot tools.

ToolUse case
Code completionsStreamline coding while staying in the flow.
Receive inline suggestions for code snippets, variable names, and functions as you write them in the editor.
ChatHave an ongoing chat conversation for brainstorming design ideas or getting code suggestions, optionally calling on domain-specific chat participants.
Choose to apply specific code suggestions to your codebase.
EditsUse natural language to start a coding editing session.
Automatically apply large code changes across multiple files in your workspace.
Agent modeImplement high-level requirements by starting an agentic coding flow.
Copilot autonomously invokes multiple tools to plan and implement the code changes and tasks that are needed.

Personalize Copilot with instructions files

When Copilot generates code or answers questions, it tries to match your coding practices and preferences such as which libraries you use or how you name your variables. However, it might not always have enough context to do this effectively. For example, if you work with a specific framework version, you need to provide additional context in your prompts.

To enhance AI responses, you can use instructions files to provide contextual details about your team's coding practices, tools, or project specifics. You can then attach these instructions to your chat prompt, or have them applied automatically.

To enable instructions files for your workspace:

  1. Run the Chat: New Instructions File command from the Command Palette.

    This command creates a .instructions.md file in ./instructions folder.

  2. Add your instructions in Markdown format to the file. For example:

    # Custom instructions for Copilot
    
    ## Project context
    This project is a web application built with React and Node.js.
    
    ## Indentation
    We use tabs, not spaces.
    
    ## Coding style
    Use camelCase for variable names and prefer arrow functions over traditional function expressions.
    
    ## Testing
    We use Jest for unit testing and Playwright for end-to-end testing.
    
  3. Optionally, add a glob pattern to the applyTo metadata field to specify which files the instructions apply to.

    ---
    applyTo: "**/*.ts"
    ---
    Coding practices for TypeScript files.
    ...
    

Get more details about using instructions files in VS Code.

Prompt engineering

You can enhance the quality of Copilot's responses by using effective prompts. A well-crafted prompt can help Copilot understand your requirements better and generate more relevant code suggestions.

  • Start general, then get specific.

    Generate a Calculator class.
    Add methods for addition, subtraction, multiplication, division, and factorial.
    Don't use any external libraries and don't use recursion.
    
  • Give examples of what you want.

    Generate a function that takes a string and returns the number of vowels in it.
    Example:
    findVowels("hello") returns 2
    findVowels("sky") returns 0
    
  • Break down complex tasks into simpler tasks.

    Instead of asking Copilot to generate a meal planner app, break it down into smaller tasks:

    • Generate a function that takes a list of ingredients and returns a list of recipes.
    • Generate a function that takes a list of recipes and returns a shopping list.
    • Generate a function that takes a list of recipes and returns a meal plan for the week.
  • Provide the right context, such as code selections, files, terminal output, and more.

    Example, use the #codebase variable to refer to the entire codebase:

    Where is the database connection string used in #codebase?
    
  • Iterate on your prompts.

    Provide follow-up prompts to refine or modify the response. For example:

    • "Write a function to calculate the factorial of a number."
    • "Don't use recursion and optimize by using caching."
    • "Use meaningful variable names."
  • Keep chat history relevant.

    Copilot uses history of the conversation to provide context. Remove past questions and responses from the history if they're not relevant. Or, start a new session if you want to change the context.

Get more details about prompt engineering.

Find practical examples of prompts to use with Copilot in the Copilot documentation.

Provide the right context and tools

Enrich your prompts with relevant context to get more accurate and relevant responses in chat. Withe the right tools, you can boost your developer productivity.

  • In agent mode, select the tools button to configure the tools you want to use or explicitly add then to your prompt.
  • Use #codebase to let Copilot find the right files automatically by performing a code search.
  • Use the #fetch tool to fetch content from a web page or use #Repo to perform a code search on a repository.
  • Reference files, folders, or symbols in your prompt by using #<file name>, #<folder name>, or #<symbol>.
  • Drag and drop files, folders, or editor tabs onto the chat prompt.
  • Add problems, test failures, or terminal output to your chat prompt for scenario-specific context.
  • Add images or screenshots to your prompt to let Copilot analyze the image.
  • In agent mode, prompt to preview your app to directly open it with the built-in simple browser.

When you use agent mode, Copilot autonomously finds the relevant files and context for you.

Get more details about adding context to chat prompts.

Reusable prompts

Prompt files enable you to save a prompt for a specific task with its context and instructions in a Markdown file. You can then attach and reuse that prompt in chat. If you store the prompt in your workspace, you can also share it with your team.

To create a reusable prompt:

  1. Create a prompt file with the Chat: New Prompt File command in the Command Palette.

    This command creates a .prompt.md file in the ./prompts folder at the root of your workspace.

  2. Describe your prompt and relevant context in Markdown format.

    For example, use this prompt to generate a new React form component.

    Your goal is to generate a new React form component.
    
    Ask for the form name and fields if not provided.
    
    Requirements for the form:
    * Use form design system components: [design-system/Form.md](../docs/design-system/Form.md)
    * Use `react-hook-form` for form state management:
    * Always define TypeScript types for your form data
    * Prefer *uncontrolled* components using register
    * Use `defaultValues` to prevent unnecessary rerenders
    * Use `yup` for validation:
    * Create reusable validation schemas in separate files
    * Use TypeScript types to ensure type safety
    * Customize UX-friendly validation rules
    
  3. Optionally, add metadata about how to run the prompt in chat. Use the mode field to specify the chat mode, and the tools field to specify which agent mode tools to use.

    ---
    mode: 'agent'
    tools: ['Repo', 'codebase']
    description: 'Generate a new React form component'
    ---
    Your goal is to generate a new React form component based on the templates in #Repo contoso/react-templates.
    
    Requirements for the form:
    * Use form design system components: [design-system/Form.md](../docs/design-system/Form.md)
    * Use `react-hook-form` for form state management:
    * Always define TypeScript types for your form data
    
  4. Run the command by typing /, followed by the prompt file name in the chat input field.

    For example, type /new-react-form to run the prompt file named new-react-form.prompt.md.

Get started with prompt files.

Choose your AI model

Copilot offers different AI models to choose from. Some models are optimized for fast coding tasks, while others are better suited for slower planning and reasoning tasks.

Model typeModels
Fast coding
  • GPT-4o
  • Claude Sonnet 3.5
  • Claude Sonnet 3.7
  • Gemini 2.0 Flash
Reasoning/planning
  • Claude Sonnet 3.7 Thinking
  • o1
  • o3-mini

Choose the model that best fits your needs by using the model picker in the chat input field.

Learn more about AI models for Copilot Chat in the Copilot documentation.

Workspace indexing

Copilot uses an index to quickly and accurately search your codebase for relevant code snippets. This index can either be maintained by or stored locally on your machine.

For repositories, you can use a remote index of your workspace, based on code search. This allows Copilot to search your entire codebase very quickly, even if the codebase is very large.

Get more details about workspace indexing.