[!IMPORTANT] We are excited to announce that we are currently migrating the plugin to fully Lua in the canary branch.
[!NOTE] A new command,
CopilotChatBuffer
has been added. It allows you to chat with Copilot using the entire content of the buffer.
[!NOTE] A new command,
CopilotChatInPlace
has been introduced. It functions like the ChatGPT plugin. Please run “:UpdateRemotePlugins” command and restart Neovim before starting a chat with Copilot. To stay updated on our roadmap, please join our Discord community.
Ensure you have the following installed:
:echo has('python3')
in Neovim. If it returns 1
, then the Python3 provider is enabled.It will prompt you with instructions on your first start. If you already have Copilot.vim
or Copilot.lua
, it will work automatically.
pip install python-dotenv requests pynvim==0.5.0 prompt-toolkit
pip install tiktoken
(optional for displaying prompt token counts)return {
{
"CopilotC-Nvim/CopilotChat.nvim",
opts = {
show_help = "yes", -- Show help text for CopilotChatInPlace, default: yes
debug = false, -- Enable or disable debug mode, the log file will be in ~/.local/state/nvim/CopilotChat.nvim.log
disable_extra_info = 'no', -- Disable extra information (e.g: system prompt) in the response.
language = "English" -- Copilot answer language settings when using default prompts. Default language is English.
-- proxy = "socks5://127.0.0.1:3000", -- Proxies requests via https or socks.
-- temperature = 0.1,
},
build = function()
vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
end,
event = "VeryLazy",
keys = {
{ "<leader>ccb", ":CopilotChatBuffer ", desc = "CopilotChat - Chat with current buffer" },
{ "<leader>cce", "<cmd>CopilotChatExplain<cr>", desc = "CopilotChat - Explain code" },
{ "<leader>cct", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
{
"<leader>ccT",
"<cmd>CopilotChatVsplitToggle<cr>",
desc = "CopilotChat - Toggle Vsplit", -- Toggle vertical split
},
{
"<leader>ccv",
":CopilotChatVisual ",
mode = "x",
desc = "CopilotChat - Open in vertical split",
},
{
"<leader>ccx",
":CopilotChatInPlace<cr>",
mode = "x",
desc = "CopilotChat - Run in-place code",
},
{
"<leader>ccf",
"<cmd>CopilotChatFixDiagnostic<cr>", -- Get a fix for the diagnostic message under the cursor.
desc = "CopilotChat - Fix diagnostic",
},
{
"<leader>ccr",
"<cmd>CopilotChatReset<cr>", -- Reset chat history and clear buffer.
desc = "CopilotChat - Reset chat history and clear buffer",
}
},
},
}
:UpdateRemotePlugins
, then inspect the file ~/.local/share/nvim/rplugin.vim
for additional details. You will notice that the commands have been registered.For example:
" python3 plugins
call remote#host#RegisterPlugin('python3', '/Users/huynhdung/.local/share/nvim/lazy/CopilotChat.nvim/rplugin/python3/CopilotChat', [
\ {'sync': v:false, 'name': 'CopilotChatBuffer', 'type': 'command', 'opts': {'nargs': '1'}},
\ {'sync': v:false, 'name': 'CopilotChat', 'type': 'command', 'opts': {'nargs': '1'}},
\ {'sync': v:false, 'name': 'CopilotChatReset', 'type': 'command', 'opts': {}},
\ {'sync': v:false, 'name': 'CopilotChatVisual', 'type': 'command', 'opts': {'nargs': '1', 'range': ''}},
\ {'sync': v:false, 'name': 'CopilotChatVsplitToggle', 'type': 'command', 'opts': {}},
\ {'sync': v:false, 'name': 'CopilotChatInPlace', 'type': 'command', 'opts': {'nargs': '*', 'range': ''}},
\ {'sync': v:false, 'name': 'CopilotChatAutocmd', 'type': 'command', 'opts': {'nargs': '*'}},
\ {'sync': v:false, 'name': 'CopilotChatMapping', 'type': 'command', 'opts': {'nargs': '*'}},
\ ])
neovim
Similar to the lazy setup, you can use the following configuration:
Plug 'CopilotC-Nvim/CopilotChat.nvim'
call plug#end()
lua << EOF
local copilot_chat = require("CopilotChat")
copilot_chat.setup({
debug = true,
show_help = "yes",
prompts = {
Explain = "Explain how it works by Japanese language.",
Review = "Review the following code and provide concise suggestions.",
Tests = "Briefly explain how the selected code works, then generate unit tests.",
Refactor = "Refactor the code to improve clarity and readability.",
},
build = function()
vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
end,
event = "VeryLazy",
})
EOF
nnoremap <leader>ccb <cmd>CopilotChatBuffer<cr>
nnoremap <leader>cce <cmd>CopilotChatExplain<cr>
nnoremap <leader>cct <cmd>CopilotChatTests<cr>
xnoremap <leader>ccv :CopilotChatVisual<cr>
xnoremap <leader>ccx :CopilotChatInPlace<cr>
Credit to @treyhunner and @nekowasabi for the configuration.
$ git clone https://github.com/CopilotC-Nvim/CopilotChat.nvim
$ cd CopilotChat.nvim
$ cp -r --backup=nil rplugin ~/.config/nvim/
$ pip install -r requirements.txt
local copilot_chat = require("CopilotChat")
-- REQUIRED
copilot_chat:setup({})
-- REQUIRED
-- Setup keymap
nnoremap <leader>ccb <cmd>CopilotChatBuffer<cr>
nnoremap <leader>cce <cmd>CopilotChatExplain<cr>
nnoremap <leader>cct <cmd>CopilotChatTests<cr>
xnoremap <leader>ccv :CopilotChatVisual<cr>
xnoremap <leader>ccx :CopilotChatInPlace<cr>
:UpdateRemotePlugins
You have the ability to tailor this plugin to your specific needs using the configuration options outlined below:
{
debug = false, -- Enable or disable debug mode
show_help = 'yes', -- Show help text for CopilotChatInPlace
disable_extra_info = 'no', -- Disable extra information in the response
hide_system_prompt = 'yes', -- Hide system prompts in the response
clear_chat_on_new_prompt = 'no', -- If yes then clear chat history on new prompt
proxy = '', -- Proxies requests via https or socks
prompts = { -- Set dynamic prompts for CopilotChat commands
Explain = 'Explain how it works.',
Tests = 'Briefly explain how the selected code works, then generate unit tests.',
}
}
You have the capability to expand the prompts to create more versatile commands:
return {
"CopilotC-Nvim/CopilotChat.nvim",
opts = {
debug = true,
show_help = "yes",
prompts = {
Explain = "Explain how it works.",
Review = "Review the following code and provide concise suggestions.",
Tests = "Briefly explain how the selected code works, then generate unit tests.",
Refactor = "Refactor the code to improve clarity and readability.",
},
},
build = function()
vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
end,
event = "VeryLazy",
keys = {
{ "<leader>ccb", "<cmd>CopilotChatBuffer<cr>", desc = "CopilotChat - Chat with current buffer" },
{ "<leader>cce", "<cmd>CopilotChatExplain<cr>", desc = "CopilotChat - Explain code" },
{ "<leader>cct", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
{ "<leader>ccr", "<cmd>CopilotChatReview<cr>", desc = "CopilotChat - Review code" },
{ "<leader>ccR", "<cmd>CopilotChatRefactor<cr>", desc = "CopilotChat - Refactor code" },
}
}
For further reference, you can view @jellydn’s configuration.
y
command.:CopilotChat
followed by your question. For example, :CopilotChat What does this code do?
y
command.:CopilotChatExplain
.y
command.:CopilotChatTests
.:CopilotChatFixDiagnostic
.:CopilotChatVisual
with your question.:CopilotChatInPlace
and type your prompt. For example, What does this code do?
Enter
to send your question to Github Copilot.q
to quit. There is help text at the bottom of the screen. You can also press ?
to toggle the help text.:CopilotChatVsplitToggle
:CopilotChatBuffer
and type your prompt. For example, What does this code do?
Enter
to send your question to Github Copilot.To chat with Copilot using the entire content of the buffer, you can add the following configuration to your keymap:
-- Quick chat with Copilot
{
"<leader>ccq",
function()
local input = vim.fn.input("Quick Chat: ")
if input ~= "" then
vim.cmd("CopilotChatBuffer " .. input)
end
end,
desc = "CopilotChat - Quick chat",
},
telescope.nvim
To integrate CopilotChat with Telescope, you can add the following configuration to your keymap:
{
"CopilotC-Nvim/CopilotChat.nvim",
event = "VeryLazy",
dependencies = {
{ "nvim-telescope/telescope.nvim" }, -- Use telescope for help actions
{ "nvim-lua/plenary.nvim" },
},
keys = {
-- Show help actions with telescope
{
"<leader>cch",
function()
require("CopilotChat.code_actions").show_help_actions()
end,
desc = "CopilotChat - Help actions",
},
-- Show prompts actions with telescope
{
"<leader>ccp",
function()
require("CopilotChat.code_actions").show_prompt_actions()
end,
desc = "CopilotChat - Help actions",
},
{
"<leader>ccp",
":lua require('CopilotChat.code_actions').show_prompt_actions(true)<CR>",
mode = "x",
desc = "CopilotChat - Prompt actions",
},
}
}
edgy.nvim
Consider integrating this plugin with edgy.nvim
. This will allow you to create a chat window on the right side of your screen, occupying 40% of the width, as illustrated below.
{
"folke/edgy.nvim",
event = "VeryLazy",
opts = {
-- Refer to my configuration here https://github.com/jellydn/lazy-nvim-ide/blob/main/lua/plugins/extras/edgy.lua
right = {
{
title = "CopilotChat.nvim", -- Title of the window
ft = "copilot-chat", -- This is custom file type from CopilotChat.nvim
size = { width = 0.4 }, -- Width of the window
},
},
},
}
:messages
and :CopilotChatDebugInfo
If you encounter any issues, you can run the command :messages
to inspect the log. You can also run the command :CopilotChatDebugInfo
to inspect the debug information.
which-key.nvim
A special thanks to @ecosse3 for the configuration of which-key.
{
"CopilotC-Nvim/CopilotChat.nvim",
event = "VeryLazy",
opts = {
prompts = {
Explain = "Explain how it works.",
Review = "Review the following code and provide concise suggestions.",
Tests = "Briefly explain how the selected code works, then generate unit tests.",
Refactor = "Refactor the code to improve clarity and readability.",
},
},
build = function()
vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
end,
config = function()
local present, wk = pcall(require, "which-key")
if not present then
return
end
wk.register({
c = {
c = {
name = "Copilot Chat",
}
}
}, {
mode = "n",
prefix = "<leader>",
silent = true,
noremap = true,
nowait = false,
})
end,
keys = {
{ "<leader>ccc", ":CopilotChat ", desc = "CopilotChat - Prompt" },
{ "<leader>cce", ":CopilotChatExplain ", desc = "CopilotChat - Explain code" },
{ "<leader>cct", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
{ "<leader>ccr", "<cmd>CopilotChatReview<cr>", desc = "CopilotChat - Review code" },
{ "<leader>ccR", "<cmd>CopilotChatRefactor<cr>", desc = "CopilotChat - Refactor code" },
}
},
Follow the example below to create a simple input for CopilotChat.
-- Create input for CopilotChat
{
"<leader>cci",
function()
local input = vim.fn.input("Ask Copilot: ")
if input ~= "" then
vim.cmd("CopilotChat " .. input)
end
end,
desc = "CopilotChat - Ask input",
},
{
"CopilotC-Nvim/CopilotChat.nvim",
keys =
function()
local keybinds={
--add your custom keybinds here
}
-- change prompt and keybinds as per your need
local my_prompts = {
{prompt = "In Neovim.",desc = "Neovim",key = "n"},
{prompt = "Help with this",desc = "Help",key = "h"},
{prompt = "Simplify and improve readablilty",desc = "Simplify",key = "s"},
{prompt = "Optimize the code to improve performance and readablilty.",desc = "Optimize",key = "o"},
{prompt = "Find possible errors and fix them for me",desc = "Fix",key = "f"},
{prompt = "Explain in detail",desc = "Explain",key = "e"},
{prompt = "Write a shell script",desc = "Shell",key = "S"},
}
-- you can change <leader>cc to your desired keybind prefix
for _,v in pairs(my_prompts) do
table.insert(keybinds,{ "<leader>cc"..v.key, ":CopilotChatVisual "..v.prompt.."<cr>", mode = "x", desc = "CopilotChat - "..v.desc })
table.insert(keybinds,{ "<leader>cc"..v.key, "<cmd>CopilotChat "..v.prompt.."<cr>", desc = "CopilotChat - "..v.desc })
end
return keybinds
end,
},
For development, you can use the provided Makefile command to install the pre-commit tool:
make install-pre-commit
This will install the pre-commit tool and the pre-commit hooks.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind are welcome!