Setting up Github Copilot in Emacs - Robert Krahn (2024)

Quickstart

Either follow the instructions of zerolfx/copilot.el or use a ready-to-roll config provided at rksm/copilot-emacsd.

This is a short walkthrough describing my current Emacs-Copilot setup. Copilot is primarily advertised as a VSCode utility but actually it works really well in Emacs. Given the ease of adjustments (e.g. when to see completions) I would even argue Emacs might provide a better experience.

Setting up Github Copilot in Emacs - Robert Krahn (1)

Table of Contents

  • Changelog
  • Copilot Emacs packages
  • Restricting when to show completions
  • Customizing keys
    • Copilot-specific
    • Tab key
    • Ctrl-g / cancel

Changelog

  • 2023-03-20: zerolfx/copilot.el has been updated, so switching to only referencing it instead of my fork. Thank you Jason H.!

Intro & Disclaimer

Github Copilot is a tool that uses machine learning to generate text completions, primarily meant for source code, even though it works surprisingly well for all kinds of texts.It is a commercial Microsoft product, though it is currently free to use if you have a few public Github repositories that have garnered some stars.It is based on OpenAI’s Codex model which itself is derived from the GPT-3 large language model (LLM).So in some sense it is ChatGPT for your editor.

LLMs that are trained on open (source) data generally, and Copilot in particular, are controversial.Among other things, many people have expressed concerns about the questionable legal grounds that these tools are build on, e.g. there is currently a pending lawsuit focusing on potential violations of open source licenses.So let me say that I’m well aware of the controversy and I would suggest careful use of Copilot, in particular when used in code bases that are not your own.

That said, it can be an astonishingly useful tool.For example, when learning languages or frameworks or dealing with new libraries are APIs, it is surprisingly good at providing guidance that often saves you many trips to the documentation.Moreover, it is really good at detecting certain patterns in your code while also seeing variations and suggesting new code based on that — clever copy/paste on steroids.Also tests are a good area where Copilot can be helpful.

And of course check the generated code.That thing will make mistakes.It is surprisingly good at getting the syntax right (though sometimes it won’t correctly close parentheses) but be aware of logical issues, anti-patterns, or outdated code.

Copilot API & integration

Even though Copilot is primarily a VSCode utility, making it work in Emacs is fairly straightforward.In essence it is not much different than a language server.The VSCode extension is not open source but since it is implemented in JavaScript you can extract the vsix package as a zip file and get hold of the JS files. As far as I know, the copilot.vim plugin was the first non-VSCode integration that used that approach. The worker.js file that is part of the vsix extension can be started as a node.js process that will read JSON-RPC data from stdin. There are a number of commands that are implemented using this format, here is the current full list:

  • getCompletions
  • getCompletionsCycling
  • getPanelCompletions
  • getVersion
  • setEditorInfo
  • checkStatus
  • signInInitiate
  • signInConfirm
  • signOut
  • notifyShown
  • notifyAccepted
  • notifyRejected
  • telemetry/exception
  • testing/createContext
  • testing/alwaysAuth
  • testing/neverAuth
  • testing/useTestingToken
  • testing/setCompletionDocuments
  • testing/setPanelCompletionDocuments
  • testing/triggerShowMessageRequest
  • testing/getDocument
  • debug/verifyState
  • debug/verifyCertificate
  • debug/verifyWorkspaceState

An editor like Emacs or VIM can start the worker in a subprocess and then interact with, sending JSON messages and reading JSON responses back via stdout.

Given that VSCode uses the exact same interface and from everything I’ve tried so far, I think it is safe to assume that using Copilot with Emacs is in no way inferior.On the contrary, as shown below, customizing the way Copilot works is actually more convenient (at list if you know some elisp :).I’m particularly mentioning that because some folks seem to believe that this is not the case. Should I be wrong on this (now or later), please let me know. It’s annoying that not event the Copilot agent isn’t open source (without open source none of this would be possible!) but it is not that complicated to figure out what it does. Which in turn means that other clients can catch up.

Copilot Emacs packages

On Github I have found three Emacs packages that implement Copilot integration:

  • zerolfx/copilot.el seems by far the most popular one and is the one I’m using below. It provides completions through an overlay, similar to the VSCodes extension.
  • fkr-0/flight-attendant.el might be inactive, the last commit was a year ago. I have not tried it.
  • tyler-dodge/eglot-copilot/ seems to be actively developed but has no documentation. It uses eglot for managing the subprocess and counsel for completions.

Setup & Customizations

You can find a fully working config building on zerolfx/copilot.el that can also be run standalone at rksm/copilot-emacsd. In the following I explain some of the customizations.

zerolfx/copilot.el is not on MELPA so you have to install it through straight.el or other means (see the README).I typically add an Emacs package like that as a submodule to my config:

git submodule add https://github.com/zerolfx/copilot.elgit submodule update --init

The packages required for it to work are: s, dash, editorconfig (and I also use company, use-package and Emacs built-in cl package here as I find those very helpful).Install them like this:

(require 'cl)(let ((pkg-list '(use-package s dash editorconfig company))) (package-initialize) (when-let ((to-install (map-filter (lambda (pkg _) (not (package-installed-p pkg))) pkg-list))) (package-refresh-contents) (mapc (lambda (pkg) (package-install pkg)) pkg-list)))

Assuming the above git submodule was cloned into copilot.el/, we can now load that package:

(use-package copilot :load-path (lambda () (expand-file-name "copilot.el" user-emacs-directory)) ;; don't show in mode line :diminish)

You can now run M-x copilot-login to authenticate with your Github account (that needs to have a subscription to the Copilot product) followed by M-x global-copilot-mode to activate Copilot everywhere.

Restricting when to show completions

global-copilot-mode will sometimes be a bit too eager, so we disable in some modes completely:

(defun rk/no-copilot-mode () "Helper for `rk/no-copilot-modes'." (copilot-mode -1))(defvar rk/no-copilot-modes '(shell-mode inferior-python-mode eshell-mode term-mode vterm-mode comint-mode compilation-mode debugger-mode dired-mode-hook compilation-mode-hook flutter-mode-hook minibuffer-mode-hook) "Modes in which copilot is inconvenient.")(defun rk/copilot-disable-predicate () "When copilot should not automatically show completions." (or rk/copilot-manual-mode (member major-mode rk/no-copilot-modes) (company--active-p)))(add-to-list 'copilot-disable-predicates #'rk/copilot-disable-predicate)

Then, it is also convenient to have the overlays not appear automatically but on-demand:

(defvar rk/copilot-manual-mode nil "When `t' will only show completions when manually triggered, e.g. via M-C-<return>.")(defun rk/copilot-change-activation () "Switch between three activation modes:- automatic: copilot will automatically overlay completions- manual: you need to press a key (M-C-<return>) to trigger completions- off: copilot is completely disabled." (interactive) (if (and copilot-mode rk/copilot-manual-mode) (progn (message "deactivating copilot") (global-copilot-mode -1) (setq rk/copilot-manual-mode nil)) (if copilot-mode (progn (message "activating copilot manual mode") (setq rk/copilot-manual-mode t)) (message "activating copilot mode") (global-copilot-mode))))(define-key global-map (kbd "M-C-<escape>") #'rk/copilot-change-activation)

M-C-<escape> will now cycle between three states automatic, manual and off.

Customizing keys

Copilot-specific

I like my keybindings to be somewhat consistent and have assigned M-C-... (Alt + Control + some other key) to Copilot related commands:

(defun rk/copilot-complete-or-accept () "Command that either triggers a completion or accepts one if oneis available. Useful if you tend to hammer your keys like I do." (interactive) (if (copilot--overlay-visible) (progn (copilot-accept-completion) (open-line 1) (next-line)) (copilot-complete)))(define-key copilot-mode-map (kbd "M-C-<next>") #'copilot-next-completion)(define-key copilot-mode-map (kbd "M-C-<prior>") #'copilot-previous-completion)(define-key copilot-mode-map (kbd "M-C-<right>") #'copilot-accept-completion-by-word)(define-key copilot-mode-map (kbd "M-C-<down>") #'copilot-accept-completion-by-line)(define-key global-map (kbd "M-C-<return>") #'rk/copilot-complete-or-accept)

Tab key

If you also want to use the <tab> key for completion but still keep the normal functionality on it, do:

(defun rk/copilot-tab () "Tab command that will complet with copilot if a completion isavailable. Otherwise will try company, yasnippet or normaltab-indent." (interactive) (or (copilot-accept-completion) (company-yasnippet-or-completion) (indent-for-tab-command)))(define-key global-map (kbd "<tab>") #'rk/copilot-tab)

Ctrl-g / cancel

I like to cancel commands with C-g. This does not work out of the box with canceling Copilot completions, be we can do:

(defun rk/copilot-quit () "Run `copilot-clear-overlay' or `keyboard-quit'. If copilot iscleared, make sure the overlay doesn't come back too soon." (interactive) (condition-case err (when copilot--overlay (lexical-let ((pre-copilot-disable-predicates copilot-disable-predicates)) (setq copilot-disable-predicates (list (lambda () t))) (copilot-clear-overlay) (run-with-idle-timer 1.0 nil (lambda () (setq copilot-disable-predicates pre-copilot-disable-predicates))))) (error handler)))(advice-add 'keyboard-quit :before #'rk/copilot-quit)

And that should be it!

Thanks to all the awesome Emacs hackers who made all this possible!

Setting up Github Copilot in Emacs - Robert Krahn (2024)

FAQs

What is the role of Copilot in GitHub Copilot learning models? ›

GitHub Copilot is an AI coding assistant that helps you write code faster and with less effort, allowing you to focus more energy on problem solving and collaboration. GitHub Copilot has been proven to increase developer productivity and accelerate the pace of software development.

Is GitHub Copilot free? ›

GitHub Copilot is free for verified students and for maintainers of popular open-source projects on GitHub. If you're not a student or a maintainer of a popular open-source project, you can try GitHub Copilot for free with a one-time 30-day trial.

Is GitHub Copilot better than ChatGPT? ›

Having said this, for Contextual Coding, GitHub Copilot is the better choice, and we are leaning toward this AI tool when it comes to programming. GitHub Copilot also excels in projects requiring time efficiency and precise code accuracy. It's especially useful for managing complex codebases or repetitive coding tasks.

Is GitHub Copilot legal? ›

This means that when you use GitHub Copilot, you're backed by a contractual commitment: If any suggestion made by GitHub Copilot is challenged as infringing on third-party intellectual property (IP) rights and you have enabled the duplicate detection filter, our contractual terms are designed to shield you.

What are some of the risks of using GitHub Copilot? ›

Dependency Risk: Relying heavily on Copilot may lead to reduced problem-solving skills and overdependence on automated suggestions among developers, especially beginners. Code Quality Variability: While Copilot most often suggests syntactically correct code, the relevance and optimization of the suggestions can vary.

What IDE does GitHub Copilot support? ›

To use GitHub Copilot in JetBrains, you must have a compatible JetBrains IDE installed. GitHub Copilot is compatible with the following IDEs: IntelliJ IDEA (Ultimate, Community, Educational) Android Studio.

How can I activate Copilot? ›

To get started, open the Copilot app from the Start menu or use the Copilot icon if present on your Windows taskbar. If your device keyboard includes a dedicated Copilot key, you can also use it to launch Copilot.

What is the difference between Copilot and GitHub Copilot? ›

Scope of use: Microsoft Copilot is designed to increase the productivity of all users who use Microsoft products in their work. GitHub Copilot, on the other hand, is a tool dedicated to technical users, developers, who must carry out programming and development activities.

What are the limitations of GitHub Copilot? ›

One of the limitations of Copilot Chat is that it may generate code that appears to be valid but may not actually be semantically or syntactically correct or may not accurately reflect the intent of the developer.

Does Copilot use GPT-4? ›

Both ChatGPT-4 and CoPilot using GPT-4 and support full functionality of GPT-4 but they have different trained model.

Is GitHub Copilot worth the money? ›

"GitHub Copilot does represent the future of coding, being like a helpful teammate who can suggest little insights, fix some problems, and write some code." In other words, it's a great support tool, but only in the hands of skilled developers who don't rely on it too much.

Is GitHub owned by Microsoft? ›

Microsoft acquired GitHub, a popular code-repository service used by many developers and large companies, for $7.5 billion in stock. The deal, which heightened Microsoft's focus on open-source development, aimed to increase enterprise use of GitHub and bring Microsoft's developer tools and services to new audiences.

How do I get GitHub Copilot to generate code? ›

For example, in a Java file, create a class by typing class Test . GitHub Copilot will automatically suggest a class body in grayed text. To accept the suggestion, press Tab . GitHub Copilot will automatically suggest code.

How can I get access to GitHub Copilot? ›

Sign up for GitHub Copilot

If you don't already have access to GitHub Copilot, you can set up a free trial or subscription for GitHub Copilot Individual on your personal GitHub account. For more information, see "Subscription plans for GitHub Copilot."

References

Top Articles
Pechiney SA -- Company History
Peniche: Surfen, Sehenswürdigkeiten und das beeindruckende Drumherum
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Finger Lakes 1 Police Beat
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Metro By T Mobile Sign In
Restored Republic December 1 2022
12 30 Pacific Time
Free Stuff Craigslist Roanoke Va
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5565

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.