Module 2
Interacting with Copilot
This unit explores the various ways to maximize your interaction with GitHub Copilot in your development environment. By understanding the features and capabilities of this service, you’ll learn how to use it effectively.
The following sections describe the different ways to trigger and use GitHub Copilot, along with examples and shortcuts to help you get the most out of it.
Inline Suggestions
The most immediate form of assistance from Copilot is inline suggestions. As you type, Copilot analyzes your code and context to offer real-time code completions. This feature predicts the next words you intend to write and displays suggestions subtly and non-intrusively.
Copilot’s suggestions appear as grayed-out text ahead of your cursor.
- To accept a suggestion, press the Tab key or the Right Arrow key.
- To reject a suggestion, keep typing or press Esc.
Inline suggestions are especially useful for repetitive tasks or when you need quick boilerplate code.
Example:
Command Palette
The command palette provides quick access to various Copilot functions, allowing you to perform complex tasks with just a few keystrokes.
- Open the command palette with Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac).
- Type Copilot to view available commands.
- Select actions like Explain this or Generate unit tests for assistance.
Copilot Chat
Copilot Chat is an interactive feature that lets you communicate with Copilot using natural language. You can ask questions or request code snippets, and Copilot responds based on your context.
- Open the Copilot chat panel in your IDE.
- Enter questions or requests in natural language and review Copilot’s response.
Example prompt:
“How do I implement binary search in Python?”
Copilot might respond with:
Inline Conversations
Inline conversations allow you to interact with Copilot contextually, directly within your code editor. You can use this feature to request changes or explanations without switching context.
- Place your cursor where you need help.
- Open an inline conversation with Ctrl+I (Windows/Linux) or Cmd+I (Mac).
- Ask questions or request specific changes at that code location.
This helps you focus on a particular section of your code and receive targeted guidance.
Code Comments
Copilot uses natural language processing to convert comments into code. You can describe the functionality you want in a comment, and when you press Enter, Copilot generates code based on your description.
Example:
Copilot might suggest:
This approach is great for quickly writing simple functions.
Multiple Suggestions
For more complex code snippets, Copilot can offer multiple alternatives.
- When Copilot suggests something, look for the lightbulb icon.
- To switch between options, click the icon or use Alt+] (Windows/Linux) or Option+] (Mac).
Multiple suggestions help you explore different coding approaches and choose the most suitable one.
Explanations
Understanding existing code is crucial, especially in large projects. To get explanations for code snippets, use the Explain this feature.
- Select a block of code.
- Right-click and choose Copilot: Explain this from the context menu.
- Read Copilot’s explanation of the selected code.
This is useful for learning and reviewing code written by others.
Automated Test Generation
Unit tests are essential for ensuring code quality and reliability. Copilot can save you time by generating unit tests for your functions or classes.
- Select a function or class.
- Use the command palette to choose Copilot: Generate unit tests.
- Review the test cases Copilot suggests.
Example:
Automated test generation helps maintain code integrity and catch bugs early in the development process.
Reminder: Copilot learns from context. The more structured and well-commented your code is, the more accurate and relevant Copilot’s assistance will be. The more you interact with Copilot, the better it understands your coding style and preferences.