Skip to main content

GitHub Copilot

Introduction

Prompting

  • Use cases:
    • Authoring code:
      • Start with a detailed comment at the top of the file, describing the purpose of the file, the libraries you want to use, the high-level inputs/outputs, including examples
      • Add additional comments for each function
    • Generating unit tests for existing code
      • Copilot is great at generating comprehensive unit tests
    • Generating comments to document existing code
    • Generating dictionaries with lookup data
      • provide an example or two and it will suggest how to continue with the list
    • Generating regular expressions
    • Explaining code:
      • Need to update code written in a language you’re less familiar with? Highlight it and ask Copilot Chat to explain what it’s doing
  • Work in reverse • Visual Studio Code 📺
    • Try coding in reverse order by calling the functions and variables you wish existed and then prompting Copilot to define those for you now that it sees how you plan to use them
  • Prompting Copilot • Visual Studio Code 📺
    • Provide context by…
      • opening all relevant files
      • clearly defining your preferences via comments
      • pasting an example (e.g. example code from a specific version of a library) to show Copilot the code style and syntax you’re looking for
  • VS Code Tips — Incrementally accepting Copilot suggestions • Visual Studio Code 📺

VS Code shortcuts

  • ctrl-enter: open the completions panel (all suggestions) in a split (try using this more often)
    • opt-[]: navigate through suggestions (improve this)
    • ctrl-/: accept the suggestion under your cursor

Copilot chat

  • Use cases:
    • Explaining a codebase (e.g. one you just inherited) with /explain or @workspace
      • e.g. @workspace /explain what does this block/file/directory/codebase do?
    • Making messy code more tidy
      • e.g. “Can you refactor and decompose this code into functions?”
      • to reduce misunderstandings, try refactoring one function at a time
    • Making fragile code more rubust
    • Adding type hints
    • Documenting code with /doc
      • e.g. adding comments, docstrings
    • Debugging a block of code with a problem
    • Answer specific how-to-code questions (e.g. a language feature, a coding concept, a function’s behavior)
    • Translate code from one language to another
      • e.g. converting bash to python
  • Open inline with cmd-i
  • Open in side panel with cmd-ctrl-i (if more space or a longer conversation would be helpful)
  • Use slash commands:
    • /new = advice (only) about how to scaffold new code
      • @workspace /new = create a new project based on your description
    • @workspace /newNotebook = create a new Jupyter notebook that does what you want
    • @workspace /fix = propose a fix for bugs in the selected code
    • /tests = generate tests for the selected code
      • e.g. /tests generate unit tests with pytest
    • /doc = add documentation (e.g. comments) to the selected code block
      • e.g. /doc add google style docstrings
    • @workspace = agent that has access to your project files (useful when asking for explanations of more project code that you could select)
      • @workspace /explain = explain what the selected code is doing
      • e.g. @workspace how is login implemented in this project?
    • @terminal = agent that provides terminal commands to accomplish your task
    • @vscode = agent that tells you how to configure/use VS Code the way you want to
    • context commands with #:
      • e.g. #file:README.md how do I run this?
  • VS Code Speech extension:
    • Amazing!
    • I’m using c-v to toggle copilot’s listening mode
    • Much faster than typing a question when spelling is unlikely to be a problem (e.g. with project/library specific naming)
  • Resources:

Other Copilot features

  • code by commenting
  • comments starting with “q:” will be answered with an “a:”
  • open copilot preview sidebar with ctrl+enter while GitHub is making a suggestion to view all solution options (is there also a keyboard shortcut for cycling inline suggestions?)

Copilot X features

  • copilot sidebar in vs code
    • use your codebase as primary context
    • highlight code block you want to prompt about (e.g. “write unit tests…”)
  • coding by voice in vs code
    • saying declarative things (”convert to JSON”) rather than imperative word-by-word transcriptions of what to write
  • copilot for docs
  • copilot in PR description box
  • copilot in terminal
    • declaratively describe what you want it to do and it will do it for you (e.g. using the right UNIX tools)

Links:

Links:

GitHub Copilot CLI

  • GitHub Copilot CLI: AI for the Command-Line • Elijah Manor 📺
  • GitHub Copilot CLI: First Look • Vishwas Gopinas explains how with natural language processing and contextual awareness, GitHub Copilot CLI can translate your requests into shell commands, Git commands, or GitHub CLI commands, allowing you to bypass the need for memorizing arcane commands and flags. 📖

Inbox