Sub topic 7
Edit code with inline chat
Inline chat lets you talk to Copilot without leaving the editor. You select code, or place the cursor on a line, describe a change, and Copilot proposes an edit in place. This chapter covers how to open it, how to write requests that work, how to review the result, and what goes wrong.
Prerequisites
This chapter assumes you have already done the following:
- Installed the GitHub Copilot extension and signed in with GitHub (earlier chapters).
- Opened a folder in VS Code and edited a file in at least one programming language.
- Used the Chat view at least once, so you know what a chat prompt and a response look like.
What inline chat is
Inline chat is a small prompt box that appears inside the editor, next to your code. It is tied to the file and the position where you opened it. The Chat view, by contrast, is a side panel for longer conversations about a project.
Use this rule of thumb:
| Situation | Better tool |
|---|---|
| Change or explain a few lines in the file you are looking at | Inline chat |
| Ask a general question or discuss design | Chat view |
| Change many files in one request | Edit or Agent mode in the Chat view |
| Finish the line you are typing | Inline suggestions |
The main benefit is scope. Because you select the code first, Copilot knows exactly which lines you mean, and the edit stays small enough to review at a glance.
Open inline chat
You can open inline chat in three ways:
- Press
Ctrl+Ion Windows and Linux, orCmd+Ion macOS. - Right-click in the editor, open the Copilot submenu, and choose the inline chat entry.
- Open the Command Palette and run the command Inline Chat: Start Inline Chat.
If the shortcut does nothing, another extension or a custom keybinding may have taken it. Open Keyboard Shortcuts from the Command Palette and search for inlineChat.start to see what is bound.
In some VS Code versions, pressing the shortcut on an empty line behaves slightly differently from pressing it on a selection. On an empty line, Copilot treats your request as "write new code here". On a selection, it treats your request as "change this code".
The editing workflow
The steps are the same for most requests.
- Select the code you want to change. If you select nothing, Copilot uses the cursor position and the surrounding lines.
- Open inline chat.
- Type a request in plain language and press
Enter. - Read the proposed change. VS Code shows it as a diff, with removed lines and added lines marked.
- Choose Accept to keep the change or Discard to throw it away. You can also type a follow-up in the same box to refine it.
Nothing is final until you accept. After you accept, the edit becomes a normal change in your file, and you can undo it with Ctrl+Z or Cmd+Z like any other edit.
A worked example
Suppose you have this Python function. It works, but it has no documentation and no input checks.
def average(numbers):
return sum(numbers) / len(numbers)Select the whole function, open inline chat, and type:
Add a docstring and raise ValueError if the list is empty.Copilot proposes a diff similar to this result:
def average(numbers):
"""Return the arithmetic mean of a list of numbers.
Raises:
ValueError: If the list is empty.
"""
if not numbers:
raise ValueError("numbers must not be empty")
return sum(numbers) / len(numbers)Your output will differ in wording, because the model does not give the same text every time. Check three things before accepting:
- The empty-list check happens before the division, so the error is raised instead of a
ZeroDivisionError. - The function name and signature are unchanged, so existing callers still work.
- Nothing outside the selected function was modified.
If the docstring style does not match your project, send a follow-up such as Use Google-style docstrings with Args and Returns sections. Copilot revises its own proposal instead of starting over.
Write requests that work
Inline chat responds to the clarity of the instruction. These habits help most.
- Name the change, not the goal. "Extract the validation into a helper function" works better than "make this cleaner".
- State constraints. Say "do not change the function signature" or "keep it synchronous" when those matter.
- Ask for one thing at a time. A request with five unrelated changes produces a diff that is hard to review. Split it into five short requests.
- Select tightly. A selection of 10 lines gives a focused edit. A selection of 400 lines invites changes you did not intend.
- Mention the target style. For example, "use list comprehension" or "use
async/awaitinstead of promises".
Some typical requests:
| Goal | Example request |
|---|---|
| Rename and tidy | Rename `d` to `delta_seconds` and update all uses in this function. |
| Fix a bug | This loop skips the last element. Fix it. |
| Add types | Add type hints to the parameters and return value. |
| Write tests | Write pytest tests for the selected function. |
| Simplify | Replace the nested ifs with early returns. |
| Explain | Explain what this regular expression matches. |
Slash commands and context references
Inline chat supports short commands that begin with /. Type / in the prompt box to see the list in your version. Common ones include:
/explainexplains the selected code./fixproposes a fix for a problem in the selection, such as a compiler error or a failing line./testsgenerates tests for the selection./docadds documentation comments.
The exact list can vary between VS Code and extension versions, so trust the list that appears in your editor over any written list, including this one.
You can also attach extra context with a # reference, the same way you do in the Chat view. For example:
Rewrite this function to use the helper in #file:utils.tsThe #file reference adds that file's contents to the request, so Copilot can call a helper that is not in the current file. Without it, Copilot only sees the current file and your selection, and it may invent a helper with a similar name.
Using diagnostics
When VS Code underlines an error or warning, you can often start inline chat from the lightbulb or the quick-fix menu and choose a Copilot option. Copilot receives the diagnostic message together with the line, so the request is already specific. This is usually faster than copying the error text into a prompt yourself.
Treat the result as a suggestion. A fix that silences the error may still be wrong. For example, adding a type cast makes a type error disappear without fixing the data that caused it.
Review before you accept
Every accepted edit becomes your responsibility. A short review routine prevents most problems.
- Read the diff line by line, not just the summary.
- Look for changes outside what you asked for, such as renamed variables or removed comments.
- Check that imports exist. Copilot sometimes uses a library you have not installed.
- Run the code or the tests. Compare behavior, not just appearance.
- Use the source control diff after accepting. The Source Control view shows exactly what changed in the file, which is a second check.
Edge cases and common errors
The edit touches more than you wanted
If the diff changes code outside your selection, discard it and reselect a smaller range. Add a constraint such as "only change the selected lines".
Nothing happens after pressing Enter
Check these in order:
- Confirm you are signed in. The Copilot icon in the status bar shows a warning if you are not.
- Confirm Copilot is enabled for the file's language. The per-language setting from the earlier settings chapter can turn Copilot off for plaintext or Markdown, which affects what you can do in those files.
- Check your network connection. A proxy or firewall that blocks Copilot's service causes requests to hang or fail.
- Open the Output panel and choose the GitHub Copilot Chat channel to see error messages.
The response refuses or returns no code
Copilot may decline a request that conflicts with its content filters, and it returns no edit for a request that is too vague. Rewrite the request to say what change you want. If the file is covered by a content exclusion rule set by your organization, Copilot will not use that file as context. This is intended behavior, not a fault.
The change is subtly wrong
The model predicts likely code. It does not execute your program. Typical subtle errors include off-by-one boundaries, wrong default values, and calls to methods that do not exist in your library version. Ask Copilot to explain its change with a follow-up like Why did you change the loop bound?. If the explanation does not hold up, discard the edit.
Inline chat versus Edit mode
Inline chat edits the place you pointed at. Edit mode in the Chat view edits a set of files you choose, and Agent mode can decide on its own which files to change and can run commands. If your request needs a change in several places, such as renaming a function used across a project, inline chat is the wrong tool. Move to Edit or Agent mode, which the next chapter covers.
Putting it together
A practical routine for a small change looks like this:
- Select the smallest block that contains the change.
- Open inline chat and write a one-sentence request with any constraints.
- Read the diff and send a follow-up if it is close.
- Accept, then run the tests.
- Review the file in Source Control before you commit.
Exercises
Exercise
You select a 12-line function and ask inline chat to "add error handling". The diff also renames two variables and reformats unrelated lines. What should you do, and how would you change the request?
Show answer
Discard the edit, because it changes code you did not ask about. Send a narrower request that names the change and adds a constraint, for example: "Add a try/except around the file read and raise a clear error message. Do not rename variables or change formatting." If the selection includes lines that do not need changes, reselect only the block that does.
Exercise
A teammate says they will use inline chat to rename a function that is called from 40 files. Why is this a poor fit, and which tool is better?
Show answer
Inline chat only edits the file and selection where you opened it, so it cannot update callers in other files. Edit mode, where you choose the files, or Agent mode, which can find the usages itself, is better. For a plain rename, VS Code's built-in Rename Symbol command (F2) is also reliable, because it uses the language server instead of a model's prediction.
Exercise
Inline chat proposes a fix that makes a type error disappear by casting a value to any. List two checks you would run before accepting it.
Show answer
First, read the diff to confirm the cast does not hide a real mismatch, such as data of the wrong shape arriving from an API. Second, run the tests or the type checker and exercise the code path with realistic data. You could also ask a follow-up such as "Fix this without using any" to get a typed solution.