Making Copilot Work in Emacs: Installation and Keybindings

I wanted to share my experience with installing and configuring Copilot for Emacs.

Copilot is a completion engine that uses Machine Learning to suggest code completions. I assume that you already have access to Copilot in your GitHub account. If you don’t, go and try it for free!

Here are some instructions that can help you set it up in your Emacs environment:

  1. First, ensure that your Emacs version is at least 27, and you have these packages installed: s, dash, editorconfig, use-package, quelpa-use-package, and quelpa. You can find them in melpa.

  2. Once you have quelpa installed, you can fetch Copilot from GitHub and install it with the following code:

(require 'quelpa)
(require 'use-package)
(require 'quelpa-use-package)

(use-package copilot
  :quelpa (copilot :fetcher github
                   :repo "zerolfx/copilot.el"
                   :branch "main"
                   :files ("dist" "*.el")))
  1. Then you might want to define some key bindings to make use of this package. I personally don’t use the functions copilot-next-completion and copilot-previous-completion since I can’t remember whether Copilot ever could switch between different completion variants. However, I do bind some other keys to make my Copilot experience smoother:
(define-key copilot-mode-map (kbd "M-C-<return>") #'copilot-accept-completion)
(define-key copilot-mode-map (kbd "C-<right>") #'copilot-accept-completion-by-word)
(define-key copilot-mode-map (kbd "C-<down>") #'copilot-accept-completion-by-line)
  1. If you need to use Copilot behind a network proxy, you can set your proxy settings with the following:
   '(:host <host-string> :port <port-number> :username <username-string> :password <password-string>))
  1. You’ll need to authenticate with your GitHub account that has a subscription to the Copilot product. To do this, run the command M-x copilot-login in Emacs and follow the prompts.

  2. Finally, don’t forget to activate Copilot by running the command M-x copilot-mode in chosen buffers, or activate it everywhere by adding (global-copilot-mode) to your .emacs file.

That’s it! With these steps, you should now be able to use Copilot in Emacs.

If you’re still having trouble getting Copilot to work in Emacs after following these steps, it could be because I’ve missed something important. In that case, I highly recommend checking out this more in-depth article by Robert Krahn: Setting up Github Copilot in Emacs | Robert Krahn. With a little patience and some tinkering, you’ll soon be enjoying the benefits of Copilot in Emacs.

Happy coding!

1 Like