Introduction
GitHub Copilot is an AI assistant that helps you write code faster right inside Visual Studio. With just a few keystrokes, Copilot can generate code, add comments, fix errors, and even write unit tests for you.
One powerful feature in Visual Studio is the Copilot chat window, which you can open by pressing ALT + /. In this window, you can type your question in plain English or use special slash commands to ask Copilot to do specific tasks like /generate, /fix, or /doc.
In this article, we'll learn what these slash commands are and how they can make your coding easier and more productive.
In Visual Studio, ALT + / launches the CoPilot.
![Visual Studio]()
In this Window, you can type the Copilot command in plain English, or you can use slash commands. Slash commands are a shorter version to ask Copilot to generate content for you.
What are Copilot slash commands?
Slash commands are short cuts to give instructions to Copilot to create and review content.
You can see all slash commands by typing slash (/) in the Copilot window.
![Slash commands]()
- /doc: Add a documentation comment.
- /explain: Explain the code.
- /fix: Propose a fix for problems in the selected code.
- /help: Get help on Copilot chat.
- /generate: Generate code to answer your question.
- /tests: Create unit tests for the selected code.
Slash commands explained
Let’s look at the slash command in more detail and how to use them.
/generate: Generate code to answer your question
Suppose you’re working on a Python project and need to read data from a CSV file. You can use the /generate command to get relevant code snippets. Here’s how,
Type a question or a description of what you want to achieve. For instance.
How do I read a CSV file in Python?
Add /generate followed by relevant keywords.
/generate read CSV file Python
Copilot will provide code snippets related to reading CSV files in Python. It also creates a new file with .py extension. The code generated by Copilot looks like this.
import csv
def read_csv_file(file_path):
data = []
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
data.append(row)
return data
# Example usage
file_path = "../../../../AppData/Local/Temp/2yzt2jkr.csv"
data = read_csv_file(file_path)
print(data)
/explain: Explain the selected code
You can use the /explain code which is like writing a documentation. You may wan to use the same command to write a detailed blog about your code.
![Selected code]()
Hit ENTER.
Copilot writes detailed explanation of the selected code.
![Explanation]()
Copilot also suggest the next steps you want to add in this code.
![Suggest]()
/doc: Add a documentation comment.
This command add comments the code.
Select the code above and type /doc command in the Copilot command window and Hit ENTER.
![Doc]()
The Copilot adds the following comments to the selected code.
import csv
def read_csv_file(file_path):
"""
Read a CSV file and return the data as a list of rows.
Args:
file_path (str): The path to the CSV file.
Returns:
list: The data from the CSV file as a list of rows.
"""
data = []
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
data.append(row)
return data
# Example usage
file_path = "../../../../AppData/Local/Temp/2yzt2jkr.csv"
data = read_csv_file(file_path)
print(data)
/fix: Propose a fix for problems in the selected code.
Let’s say there is an error in the code like spelling mistake or wrong variables, you can use /fix slash command for that.
![Fix slash command]()
The Copilot will show the fixed code that you may accept to fix the file.
![Fixed code]()
/tests: Create unit tests for the selected code
Copilot generates test cases based on your selected code. Let’s select out previous code and ask it to generate test cases.
![Test cases]()
Copilot generates a tests file that looks like this.
import unittest
from unittest.mock import patch
from io import StringIO
from read_csv_file import read_csv_file
class ReadCSVFileTestCase(unittest.TestCase):
def test_read_csv_file(self):
file_path = "../../../../AppData/Local/Temp/test.csv"
expected_data = [['1', 'John', 'Doe'], ['2', 'Jane', 'Smith'], ['3', 'Bob', 'Johnson']]
with patch('builtins.open', return_value=StringIO('1,John,Doe\n2,Jane,Smith\n3,Bob,Johnson\n')) as mock_file:
data = read_csv_file(file_path)
self.assertEqual(data, expected_data)
mock_file.assert_called_once_with(file_path, 'r')
def test_read_csv_file_empty_file(self):
file_path = "../../../../AppData/Local/Temp/empty.csv"
expected_data = []
with patch('builtins.open', return_value=StringIO('')) as mock_file:
data = read_csv_file(file_path)
self.assertEqual(data, expected_data)
mock_file.assert_called_once_with(file_path, 'r')
def test_read_csv_file_invalid_file(self):
file_path = "../../../../AppData/Local/Temp/invalid.csv"
with patch('builtins.open', side_effect=FileNotFoundError) as mock_file:
with self.assertRaises(FileNotFoundError):
read_csv_file(file_path)
mock_file.assert_called_once_with(file_path, 'r')
if __name__ == '__main__':
unittest.main()
/optimize: Improve code performance
The /optimize slash command analyzes the existing code—whether it's the entire file or just a selected portion—and suggests optimizations. Use the following syntax to execute the /optimize slash command.
![Optimize slash]()
After completion, Copilot also explains what optimization has done in the selected code. It also gives you options to create a new file or preview and replace the selected code.
![New file]()
Reference: scope Copilot results to a particular file or entire solution
All the slash commands can be applied to selected code, a particular file, or even entire solution.
To select a particular file or entire solution, you use # symbol that load all available files and ‘Solution” option.
![Solution]()
If you select or type #Solution, the command works for the entire solution. For example, /optimize #Solution below will optimize the entire solution.
![Entire solution]()
The following command will optimize only Program.cs in CopilotEBookSamples folder.
![Program]()
Conclusion
GitHub Copilot’s slash commands in Visual Studio are like shortcuts that help you code smarter and faster. Whether you want to generate new code, fix errors, add comments, or create tests, Copilot is ready to assist with just a simple command.
By using commands like /generate, /explain, and /tests, you can save time and focus more on building great software. So the next time you're stuck or want to speed things up, just hit ALT + / and let Copilot help you out.
Download Copilot eBook: Copilot Handbook for Students and Developers