DEV Community

Cover image for GitHub Copilot: Use prompt files for Visual Studio
Karen Payne
Karen Payne

Posted on

GitHub Copilot: Use prompt files for Visual Studio

Introduction

With GitHub Copilot active in Microsoft Visual Studio, learn how to create reusable prompts that can be used in all projects in a Visual Studio Solution, done using prompt files.

Prompt files

Prompt files contain prompts that a developer would normally type into the GitHub Chat window.

Benefits of using prompt files

When a developer repeatedly asks the same question multiple times or needs a set of actions performed using Copilot agents. Using prompt files allow developers to reference a prompt in the GitHub Chat window and press enter.

Creating prompt files

  1. Open the root folder of a Visual Studio solution.
  2. Under the folder .github create a new folder prompts, see figure 1.
  3. Create a new file DescriptiveName.prompt.md where DescriptiveName describes the contents of the file. For example BogusCustomers.prompt.md. See figure 2.

Figure 1

The following uses File Explorer Visual Studio extension,

Shows prompts folder in Visual Studio

Figure 2

In the case of BogusCustomers.prompt.md the task is to use Bogus NuGet package for creating a list of Customer.

public class Customer
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly BirthDay { get; set; }
    public string Email { get; set; }
    public Gender? Gender { get; set; }
    public override string ToString() => $"{Id,-3}{FirstName,-7}{LastName,-10} ~ ";
}

public enum Gender
{
    Male,
    Female
}
Enter fullscreen mode Exit fullscreen mode

In the project, Bogus has been installed already. Open BogusCustomers.prompt.md and enter the following.

1. Under the folder Classes create a new class named BogusCustomer.
1. Create a method named GenerateCustomers that returns a List of Customer using Bogus using Randomizer.Seed = new Random(338).
Enter fullscreen mode Exit fullscreen mode

Use a prompt file

Open the Copilot chat window, click the plus sign for references.

Shows chat window

A context menu is displayed, select Prompts.

Shows context menu pointing to Prompts item

All prompt files are displayed; here we will pick BogusCustomers.prompt.md.

Shows selecting BogusCustomers.prompt.md file

To execute, click the airplane.

Clicking the airplane to execute the prompt file

Or type #prompt: to get a list of prompts.

Manually requesting for prompt files

Copilot executing your instructions

🟢 If the instructions can be carried out, during the processing phase, Copilot displays details and will then show proposed changes with two buttons, one button to keep and another button to deny. If more than one file has changes, there will be keep and undo buttons for each file.

Shows Copilot asking permissions

🔴 If the instructions cannot be carried out, Copilot may simply display an endless spinner, which means there is nothing to follow through with. In this case, to cancel the operation.

Example of not being able to finish processing a request. In an ASP.NET Core project, the prompt file contains instructions to install Serilog along with instructions to configure SeriLog. In this case, either install Serilog using add dependencies or separate instructions into two prompt files, execute one at a time.

Create a descriptive markdown file

Names of prompt files are clear at time of creation while as time passes the prompt file names may not be clear. Consider creating a markdown file that describes each prompt file as shown below.

Markdown file

Referencing external source code

The intent is to write a prompt to use from a NuGet package. First, make sure the package has been installed in the project.

In the prompt, rather than reference a method, for instance, reference the path to the reference, followed by the method.

Here we want to reference ValidateOnStart in the NuGet package ConsoleConfigurationLibrary. Note that the full path, including the namespace, is included.

Using ConsoleConfigurationLibrary.Classes.ApplicationValidation.cs  add ValidateOnStart to validate Models\ConnectionStrings class  properties MainConnection and SecondaryConnection in Main method of Program.cs and wrap in a try catch. 
Enter fullscreen mode Exit fullscreen mode

Security

By default, when committing to a repository prompt files are included in the commit. If prompt files contain information that should not be public mark them to be not included in the commit.

Documentation

Microsoft documentation for using prompt files.

Source code

  • Has several sample prompt files under .github\prompts folder
  • Under Solution folder Using Prompt files projects were used to test

Source code

Summary

Prompt files provide a quick way to perform re-prompting without the need to retype prompts.

Top comments (0)