Sub topic 5
Work with inline suggestions
Inline suggestions are the completions that GitHub Copilot draws in your editor as grey "ghost text" while you type. This chapter covers how they are triggered, how to accept all or part of one, how to cycle through alternatives, and how to turn them off per language. It also covers the errors learners hit most often.
Prerequisites
This chapter assumes you have:
- Installed the GitHub Copilot extension and signed in with GitHub (the two previous chapters).
- Basic use of VS Code: opening a folder, creating files, and opening the Command Palette.
- Some experience editing code in at least one language.
How inline suggestions work
When you pause typing, the extension sends context to the Copilot service. The context includes the text around your cursor and other relevant code from the open file and from other open tabs. The service returns a proposed completion, and VS Code shows it as grey text after the cursor.
Three points explain most of the behavior you will see:
- The suggestion is a proposal. Nothing is written to your file until you accept it.
- The suggestion depends on context. A descriptive comment, a clear function name, and consistent surrounding code all improve it.
- The model is not deterministic. The same prompt can produce different suggestions on different tries, and a suggestion can be wrong even when it looks right.
Trigger a suggestion
Suggestions usually appear on their own as you type. A reliable way to trigger one is to write a comment that states what you want, then start the function signature. Create a file named fib.py and type this:
# Return the n-th Fibonacci number using iteration
def fibonacci(n):After you press Enter at the end of the def line, Copilot typically proposes a function body in grey text. The exact body varies. A typical result looks like this:
# Return the n-th Fibonacci number using iteration
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return aIf nothing appears, you can ask for a suggestion directly. Open the Command Palette and run Trigger Inline Suggestion. In the default keybindings this is also bound to Alt+\ on Windows and Linux and Option+\ on macOS. Check Keyboard Shortcuts in VS Code if the key does nothing, because bindings can differ by version and layout.
What makes a prompt for a suggestion good
- Name things clearly.
calculate_shipping_cost(weight_kg, zone)gives more to work with thancalc(a, b). - Write the comment first, then the signature. The comment states intent and the signature fixes the shape.
- Keep related files open. Copilot can draw on open tabs, so a matching type definition in another tab can improve the result.
- Add a type hint or a docstring when the intent is not obvious from the name.
Accept, reject, and cycle
Keep these shortcuts at hand. They are the defaults on Windows and Linux. On macOS, Alt is Option.
| Action | Shortcut |
|---|---|
| Accept the whole suggestion | `Tab` |
| Accept the next word | `Ctrl+Right Arrow` (`Cmd+Right Arrow` on macOS) |
| Dismiss the suggestion | `Esc` |
| Show the next alternative | `Alt+]` |
| Show the previous alternative | `Alt+[` |
| Trigger a suggestion manually | `Alt+\` |
Hover over a suggestion to see a small toolbar with the same actions. The toolbar also has a button that opens a panel of several alternatives.
Accept part of a suggestion
A long suggestion is often right at the start and wrong at the end. Accepting word by word lets you keep the good part. Press Ctrl+Right Arrow repeatedly to take one word at a time, then type your own ending. The remaining grey text disappears as soon as you type something that does not match it.
Cycle through alternatives
Copilot can produce more than one candidate. Press Alt+] to move to the next one and Alt+[ to move back. When more than one candidate exists, the hover toolbar shows a position such as 1/2. If you see no counter, the service returned only one candidate for that position. Typing a few more characters often produces a fresh request.
Review before you accept
Treat every accepted suggestion as code written by someone you have not worked with before. Read it, run it, and test edge cases. In the Fibonacci example, check what happens for n = 0, for negative numbers, and for non-integers. The suggested body above returns 0 for n = 0, but it does not reject negative input. You decide whether that matters.
Control suggestions per language
You can turn inline suggestions on or off for specific languages. Open the Command Palette, run Preferences: Open User Settings (JSON), and add this:
{
"github.copilot.enable": {
"*": true,
"plaintext": false,
"markdown": false
}
}The key "*" is the default for every language. The other keys are VS Code language identifiers, which you can find in the language picker at the bottom right of the window. This setting keeps suggestions on for code but off for plain text and Markdown, where they are often more distracting than useful.
You can also use the Copilot icon in the status bar. Selecting it opens a menu that lets you disable completions globally or for the language of the current file. The menu writes to the same setting, so the two methods stay in sync. A later chapter covers these settings in more depth.
Common errors
No suggestion appears
Work through these checks in order:
- Look at the Copilot icon in the status bar. A crossed-out or warning icon means you are signed out, the subscription is not active, or completions are disabled for this file.
- Confirm that the language of the current file is not set to
falseingithub.copilot.enable. - Make sure the file is not excluded by a content exclusion rule set by your organization. Excluded files get no suggestions.
- Check the GitHub Copilot output channel (View, Output, then pick it from the dropdown) for network or authentication errors.
- Reload the window with Developer: Reload Window if the extension seems stuck.
Suggestions appear but are poor
- The file has no context. Add a comment, a clear name, or a type signature.
- The file is very large. Context is limited, so code far from the cursor may not be considered.
- A related file is closed. Open the file that defines the types or helpers you want Copilot to use.
- The suggestion repeats an existing pattern you do not want. Dismiss it and write the first line yourself.
Tab inserts a tab instead of accepting
Tab accepts a suggestion only while one is visible and the editor has focus. If you pressed Esc earlier, or the grey text vanished, Tab behaves normally. Another extension can also claim the key. Open Keyboard Shortcuts and search for Tab to look for conflicts.
Suggestions interfere with the built-in autocomplete list
VS Code's own IntelliSense list and Copilot's ghost text can compete. If the IntelliSense dropdown is open, Tab selects its highlighted item instead of the Copilot suggestion. Press Esc to close the list, or keep typing until the ghost text is the only thing showing.
Habits that work well
- Write a short comment before each non-obvious block. It improves both the suggestion and the final code.
- Accept in small pieces when the suggestion is long. Word-by-word accepting costs a few keystrokes and avoids rewriting a bad ending.
- Run your tests after accepting. Suggestions can compile and still be wrong.
- Turn suggestions off for files where you want full control, such as Markdown notes, using the language setting above.
Exercises
Exercise
You accepted the first half of a long suggestion by pressing Ctrl+Right Arrow several times, and now you want a different second half. What is the quickest way to get alternatives, and what happens to the remaining grey text if you type your own characters that differ from it?
Show answer
Press Alt+] (Option+] on macOS) to cycle to another candidate if the hover toolbar shows more than one, or press Alt+\ to request a new suggestion. If you type characters that do not match the remaining grey text, that text disappears and Copilot may offer a new suggestion based on what you typed.
Exercise
Write the settings.json snippet that keeps Copilot suggestions on everywhere except in Markdown files and plain text files. Then name one reason the setting might appear to have no effect.
Show answer
{"github.copilot.enable": {"*": true, "plaintext": false, "markdown": false}}. The setting may appear to have no effect because a workspace setting in .vscode/settings.json overrides the user setting for the same key.
Exercise
You open a Python file and no suggestions appear, even after running Trigger Inline Suggestion. List three things you would check, in order.
Show answer
First, check the Copilot status bar icon for a sign-in or subscription problem. Second, confirm that "python" is not set to false in github.copilot.enable at user or workspace level. Third, open the GitHub Copilot output channel to look for network or authentication errors, and check whether a content exclusion rule applies to the file. Reloading the window is a reasonable fourth step.