What is GitHub Copilot?
GitHub Copilot is an AI-powered code completion tool that runs inside your editor. It suggests entire functions, generates boilerplate, writes tests, explains code, and helps with refactoring. Powered by large language models trained on public code, Copilot understands context from your open files, comments, function names, and surrounding code to provide relevant suggestions.
Copilot Features
- Inline Completions: Ghost text suggestions that appear as you type. Press
Tabto accept. - Copilot Chat: A sidebar chat for asking questions, explaining code, and generating code from descriptions.
- Inline Chat: Press
Cmd+Ito ask Copilot to modify selected code or generate new code at the cursor. - Code Review: Copilot can review your pull requests and suggest improvements.
- Terminal Integration: Ask Copilot to explain or generate terminal commands.
Keyboard Shortcuts
# VS Code Copilot Shortcuts (macOS)
Tab # Accept current suggestion
Escape # Dismiss suggestion
Alt+] # Next suggestion
Alt+[ # Previous suggestion
Cmd+I # Open inline chat
Ctrl+Shift+I # Open Copilot Chat panel
Cmd+Shift+Alt+Enter # Open Copilot completions panel (see all suggestions)
Effective Prompting Techniques
The quality of Copilot suggestions depends heavily on the context you provide. Here are proven techniques for getting better results:
1. Write Descriptive Comments First
// Fetch user data from the API, handle loading and error states,
// and cache the result for 5 minutes using the stale-while-revalidate pattern
async function useUser(userId: string) {
// Copilot generates the implementation based on the comment
}
2. Use Type Signatures to Guide Generation
// Define the types first - Copilot uses them for accurate suggestions
interface CartItem {
id: string;
name: string;
price: number;
quantity: number;
discount?: number;
}
interface CartSummary {
items: CartItem[];
subtotal: number;
tax: number;
discount: number;
total: number;
}
// Copilot now generates an accurate implementation because it understands the types
function calculateCartSummary(items: CartItem[], taxRate: number): CartSummary {
// Tab to accept Copilot's suggestion
}
3. Provide Examples
// Convert kebab-case to camelCase
// "hello-world" -> "helloWorld"
// "background-color" -> "backgroundColor"
// "border-top-left-radius" -> "borderTopLeftRadius"
function kebabToCamel(str: string): string {
// Copilot generates accurate implementation from examples
}
4. Name Functions Descriptively
// Good: Descriptive name gives Copilot context
function validateEmailAndReturnNormalized(email: string): string | null {
// Copilot knows to validate AND normalize
}
// Less effective: Vague name
function processInput(data: string): string | null {
// Copilot has less context about what to generate
}
Using Copilot Chat Effectively
# Chat slash commands in VS Code
/explain # Explain the selected code
/fix # Suggest a fix for the selected code
/tests # Generate tests for the selected code
/doc # Generate documentation for the selected code
/optimize # Suggest performance improvements
# Chat participants
@workspace # Ask about your entire project
@vscode # Ask about VS Code settings and features
@terminal # Ask about terminal commands
# Example prompts:
# "Write unit tests for the calculateCartSummary function using Vitest"
# "@workspace Where is the authentication middleware defined?"
# "/explain What does this regex do?"
# "/fix This function throws when the array is empty"
# "@terminal How do I find all files modified in the last 24 hours?"
Custom Instructions
Create a .github/copilot-instructions.md file in your repository to provide project-specific context to Copilot. This file is automatically included in Copilot Chat context.
# .github/copilot-instructions.md
## Project Context
This is a Next.js 15 application using the App Router, TypeScript, Tailwind CSS,
and Prisma ORM with PostgreSQL.
## Code Style
- Use functional components with TypeScript interfaces for props
- Use server components by default, add 'use client' only when needed
- Prefer named exports over default exports
- Use Zod for runtime validation of API inputs
- Use React Server Actions for form mutations
## Naming Conventions
- Components: PascalCase (UserProfile.tsx)
- Utilities: camelCase (formatDate.ts)
- API routes: REST conventions (GET, POST, PUT, DELETE)
- Database: snake_case for columns, PascalCase for Prisma models
## Testing
- Use Vitest for unit tests
- Use Playwright for E2E tests
- Tests go in __tests__ directories adjacent to the code they test
Copilot for Test Generation
// Select a function and use /tests or Cmd+I with "write tests"
// Copilot generates comprehensive test suites:
import { describe, it, expect } from 'vitest';
import { calculateCartSummary } from './cart';
describe('calculateCartSummary', () => {
it('calculates subtotal from item prices and quantities', () => {
const items = [
{ id: '1', name: 'Widget', price: 10, quantity: 2 },
{ id: '2', name: 'Gadget', price: 25, quantity: 1 },
];
const summary = calculateCartSummary(items, 0.1);
expect(summary.subtotal).toBe(45);
});
it('applies discounts correctly', () => {
const items = [
{ id: '1', name: 'Widget', price: 100, quantity: 1, discount: 20 },
];
const summary = calculateCartSummary(items, 0.1);
expect(summary.discount).toBe(20);
expect(summary.total).toBeLessThan(100);
});
it('handles empty cart', () => {
const summary = calculateCartSummary([], 0.1);
expect(summary.total).toBe(0);
expect(summary.items).toHaveLength(0);
});
});
Copilot Best Practices
- Review every suggestion: Copilot is not always right. Read and understand generated code before accepting it.
- Use it for boilerplate, not logic: Copilot excels at generating repetitive patterns. For complex business logic, write it yourself.
- Open relevant files: Copilot uses open editor tabs as context. Open related files to improve suggestions.
- Iterate with inline chat: If the first suggestion is not right, use
Cmd+Ito refine it with specific instructions. - Never commit secrets: Copilot might suggest hardcoded API keys from training data. Always use environment variables.