When you use GitHub Copilot inside VS Code notebooks, code suggestions appear differently than in regular editor files. Notebook cells behave as independent code blocks, which changes how Copilot reads context and generates completions. This article explains the exact rules Copilot follows when suggesting code inside Jupyter or .ipynb cells, including how it uses cell order, variable scope, and markdown content. You will learn how to structure your notebook to get better suggestions and avoid common pitfalls like broken completions or missing imports.
Key Takeaways: Copilot Suggestions in VS Code Notebook Cells
- Cell-level context: Copilot reads only the current cell and preceding cells in execution order, not the entire file
- Markdown cells as hints: Writing a clear comment or markdown description above a code cell improves suggestion relevance
- No cross-file context in notebooks: Copilot does not pull context from other open files when suggesting inside a notebook cell
How Copilot Reads Context in Notebook Cells
Copilot in VS Code notebooks works differently from its behavior in standard .py or .js files. In a regular file, Copilot sees the entire file as one continuous stream of text. In a notebook, each cell is an isolated unit. Copilot treats the current cell as the primary context source. It also reads the content of cells that appear earlier in the notebook execution order. This means cell order matters more than visual position on the screen.
When you place the cursor in a code cell and begin typing, Copilot generates suggestions based on three sources:
- Current cell content: Anything already typed in the active cell, including comments and variable names
- Previous code cells: Code from cells above the current cell in the notebook, even if they are collapsed or scrolled out of view
- Immediately preceding markdown cells: Markdown text directly above the code cell, which Copilot treats as a natural language prompt
Copilot does not look at cells below the current cell. It also ignores cells that are hidden or collapsed if they appear after the active cell. This forward-only context model matches how execution flows in a Jupyter notebook.
Execution Order vs Visual Order
In a notebook, cells can be reordered by dragging them. Copilot uses the visual order of cells as they appear from top to bottom. If you move a cell up, Copilot treats it as earlier context. If you run cells out of order, Copilot still bases suggestions on the visual order, not the runtime execution history. This can lead to suggestions that reference variables not yet defined if you skip cells during execution.
Markdown Cells as Natural Language Prompts
Markdown cells above a code cell serve as high-level instructions for Copilot. For example, a markdown cell that says “Load the CSV file and clean missing values” will cause Copilot to generate code for reading a CSV and handling NaN values. This works best when the markdown text is concise and uses keywords that match common library functions. Avoid vague descriptions like “do some data processing” because Copilot will generate generic code that may not match your intent.
Steps to Get Better Copilot Suggestions in Notebooks
- Write a markdown cell above each code cell
Type a short description of what the code cell should accomplish. Use action verbs like “load”, “filter”, “merge”, or “visualize”. Keep the description under two sentences for best results. - Define all imports and variables in the first code cell
Place import statements and global variable definitions in the earliest cell. Copilot uses this context for all subsequent cells. If you spread imports across multiple cells, Copilot may not see them when generating suggestions in later cells. - Type a comment or function signature before the code
Start the code cell with a comment line that describes the next block of code. For example,# Calculate the mean of each column. Copilot uses this comment as a prompt and generates the corresponding pandas operation. - Keep cells focused on one task
Do not combine data loading, transformation, and plotting in a single cell. A long cell reduces Copilot’s accuracy because it has to guess where one task ends and another begins. Split tasks into separate cells with their own markdown descriptions. - Press Tab or Enter to accept a suggestion
When Copilot shows a ghost text suggestion, press Tab to accept the full suggestion or Ctrl+RightArrow to accept one word at a time. Pressing Enter inserts a new line instead of accepting the suggestion. If you want to accept the suggestion and move to the next line, press Tab then Enter. - Use the Copilot inline chat for cell-specific prompts
Press Ctrl+I in a code cell to open the inline chat. Type a request like “add error handling for file not found” and Copilot will generate code that replaces or augments the current cell content. This bypasses the context limitations of ghost text suggestions.
Common Issues with Copilot in Notebooks
Copilot Suggests Code That References Undefined Variables
This happens when the current cell relies on a variable defined in a cell that appears later in the notebook. Copilot does not see cells below the current one. To fix this, move the variable definition to a cell above the current cell. Alternatively, define the variable inside the current cell before using it.
Copilot Does Not Suggest Anything in a Cell
Copilot may remain silent if the cell is completely empty and there is no preceding markdown cell. Type a comment or a partial line of code to trigger a suggestion. For example, typing import will often trigger a list of library completions. If Copilot still does not respond, check that the GitHub Copilot extension is enabled and that you have an active subscription.
Suggestions Ignore the Markdown Cell Above
Copilot reads markdown cells only if they directly precede the code cell with no other code cells in between. If there is a blank code cell between the markdown and the target cell, Copilot ignores the markdown. Remove any empty code cells between the markdown instruction and the code cell where you want the suggestion.
Copilot Repeats the Same Suggestion Across Multiple Cells
When cells have identical markdown descriptions or identical starting code, Copilot may generate the same suggestion repeatedly. Change the wording in the markdown cell or add a unique comment at the top of each code cell. For example, instead of “clean the data” in every cell, use “clean missing values from column A” in one cell and “clean duplicate rows” in the next.
Copilot Ghost Text vs Inline Chat in Notebooks
| Item | Ghost Text Suggestions | Inline Chat (Ctrl+I) |
|---|---|---|
| Trigger method | Automatic as you type | Manual shortcut or command palette |
| Context used | Current cell and cells above | Current cell only, plus user-provided prompt |
| Output | One-line or multi-line completion | Full code block that replaces cell content |
| Best for | Short completions, variable names, function calls | Complex transformations, error handling, new functions |
| Modification | Tab to accept, Ctrl+RightArrow for partial accept | Review and edit generated code before inserting |
Ghost text works well for repetitive or predictable code. Inline chat is better when you need a complete rewrite of a cell or when the markdown cell above is too vague. You can switch between both methods depending on the task.
Now you understand how Copilot reads cell context, markdown prompts, and execution order in VS Code notebooks. Structure your notebook with clear markdown descriptions and focused code cells to get relevant suggestions. Try using inline chat with Ctrl+I when ghost text fails to produce the correct output. For notebooks with many cells, group related cells under a single markdown heading to improve Copilot’s context awareness.