Post

Setup Github Copilot with VIM

Pre-requisites

  • Install the latest version of Vim (9.0.0185 or newer).
  • Install NodeJS (v20.2.0 or newer).

Installing Latest Vim Version

Clone the Vim repository:

1
2
cd $HOME
git clone https://github.com/vim/vim.git

Change to the Vim source directory:

1
cd $HOME/vim/src

Configure make to use all available cores:

1
2
export NB_CORES="$(grep -c '^processor' /proc/cpuinfo)"
export MAKEFLAGS="-j$((NB_CORES+1)) -l${NB_CORES}"

Build Vim:

1
make

Run the Vim test suite:

1
make test

Install Vim:

1
sudo make install

Cleanup:

1
2
cd $HOME
rm -rf $HOME/vim

Verify Vim version:

1
vim --version

Install NodeJS with NVM

If you already have NodeJS installed, you can skip this step.

Install Node Version Manager (NVM):

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Verify NVM version:

1
nvm --version

List available NodeJS versions:

1
nvm list-remote

Install NodeJS:

1
nvm install v20.2.0

Verify NodeJS version:

1
node -v

Set default NodeJS version:

1
nvm alias default 20.2.0

Use default NodeJS version:

1
nvm use default

Verify NodeJS version:

1
node -v

Install Copilot Vim Plugin

Dowload the Copilot Vim plugin:

1
git clone https://github.com/github/copilot.vim.git $HOME/.vim/pack/github/start/copilot.vim

Create a vimrc config file, if it doesn’t already exist:

1
vim $HOME/.vimrc

Add the following to the vimrc config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
" Fix backspace issues
set backspace=indent,eol,start

" Adds line numbers to editor gutter
set number

" Disables vi compatibility that could cause errors
set nocompatible

" Automatic filetype detection
filetype on

" The below line enables copilot
" Enable plugins and load plugin for the detected file type.

" Enable copilot
filetype plugin on

" Load filetype specific indentation
filetype indent on

" Syntax highlighting on
syntax on

Some optional configurations you can add to your vim config file:

Enable Copilot for specific filetypes, be sure to specify the filetype and not the file extension:

1
2
3
4
5
" Enable Copilot for specific filetypes
" See: https://github.com/github/copilot.vim/blob/1a55183ef9347d6f420406a3746474b6b9fb9ef5/doc/copilot.txt#L46
let g:copilot_filetypes = {
        \ 'python': v:true
\}

Disable Copilot for files larger than 100kb:

1
2
3
4
5
6
" Disable Copilot for files larger than 100kb
autocmd BufReadPre *
    \ let f=getfsize(expand("<afile>"))
    \ | if f > 100000 || f == -2
    \ | let b:copilot_enabled = v:false
    \ | endif

You should be able to save and exit Vim now.

Setup Copilot

You will need to open Vim using your new vimrc config file. You can do this by running the following command:

1
vim

Once Vim is open, you can start the Copilot setup process by running the following command:

1
:Copilot Setup

This will open a browser window and prompt you to login to Github. Once you login, you will be prompted to authorize Copilot. Once you authorize Copilot, you will be given a code to paste into Vim. Paste the code into Vim and press enter. You should see a message that says “Copilot is ready to use!”.

You can check the status of Copilot by running the following command:

1
:Copilot Status

Using Copilot

Copilot will automatically start suggesting code as you type. To accept a suggestion, press Ctrl + Space. To reject a suggestion, press Ctrl + E. To undo a suggestion, press Ctrl + Z.

Copilot Commands

CommandDescription
:Copilot SetupSetup Copilot
:Copilot EnableEnable Copilot
:Copilot DisableDisable Copilot
:Copilot StatusCheck Copilot status
:Copilot ClearCacheClear Copilot cache

References

This post is licensed under CC BY 4.0 by the author.