2023年9月9日 星期六

[Vim] Using GitHub Copilot

    GitHub Copilot    Vim    Bash 


Introduction


copilot.vim is the Vim plugin to let us use GitHub Copilot in Vim.


Prerequisites


Node.js
Vim 9.0.0185


Setup


First enable GitHub Copilot to your GitHub account here.

Install the plugin: copilot.vim and enable it by

:Copilot auth


It will ask you to enter a OTP in https://github.com/login/device.
After the authentication, type :Copilot setup and a message will show that you've logged in.

Now type :Copilot version to check the version and :Copilot enable to get started.


Features


Panel of Suggestion

Select a text which contains your prompt, and then use the following command to show a list of solutions.

:Copilot panel


Ghost Text auto-completion


GitHub Copilot will show the auto-complete text, code or scripts based on the context of current working file. The auto-complete text will be shown in grey color, type TAB to complete it. 


For more details and default hotkeys, see :h copilot or :Copilot help.
I recorded a video to show how to use it in Vim and bash Vi mode.





Options


Disable by file type

Sometimes you want to disable it by default for some file types, you can write an AutoCommand like this (to disable it on markdown file).  

autocmd! BufNewFile,BufRead *.md exe 'Copilot disable'


copilot.vim has the global variable: "g:copilot_filetypes" that we can tell it to enable/disable GitHub Copilot on certain file types. See :h g:copilot_filetypes for details. Here is an example in my vimrc.

let g:copilot_filetypes = { 'xml': v:false, 'markdown': v:false, 'log': v:false, }


Or you can disable it in all filetypes(*) and only enable it in some filetypes by...

let g:copilot_filetypes = { '*': v:false, 'python': v:true, }