2025-06-14 09:15:11 +05:30
--gitHub Copilot Chat configuration - Latest v3+ setup
-- Comprehensive configuration with all features and working keymaps
2025-05-29 19:04:44 +05:30
-- https://github.com/CopilotC-Nvim/CopilotChat.nvim
return {
" CopilotC-Nvim/CopilotChat.nvim " ,
dependencies = {
2025-06-14 09:15:11 +05:30
{ " github/copilot.vim " } , -- or zbirenbaum/copilot.lua
{ " nvim-lua/plenary.nvim " , branch = " master " } , -- for curl, log and async functions
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
build = " make tiktoken " , -- Only on MacOS or Linux
event = " VeryLazy " ,
2025-05-29 19:04:44 +05:30
config = function ( )
local chat = require ( " CopilotChat " )
local select = require ( " CopilotChat.select " )
2025-06-14 09:15:11 +05:30
-- Setup CopilotChat with comprehensive configuration
2025-05-29 19:04:44 +05:30
chat.setup ( {
2025-06-14 09:15:11 +05:30
-- Model configuration
model = ' gpt-4.1 ' , -- Default model to use, see ':CopilotChatModels' for available models
agent = ' copilot ' , -- Default agent to use, see ':CopilotChatAgents' for available agents
context = nil , -- Default context or array of contexts to use
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Temperature for GPT responses (0.1 = more focused, 1.0 = more creative)
temperature = 0.1 ,
-- Window configuration
window = {
layout = ' vertical ' , -- 'vertical', 'horizontal', 'float', 'replace'
width = 0.5 , -- fractional width of parent
height = 0.5 , -- fractional height of parent
-- Options for floating windows
relative = ' editor ' , -- 'editor', 'win', 'cursor', 'mouse'
border = ' rounded ' , -- 'none', 'single', 'double', 'rounded', 'solid', 'shadow'
title = ' Copilot Chat ' ,
footer = nil ,
zindex = 1 ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- UI settings
show_help = true , -- Shows help message as virtual lines when waiting for user input
highlight_selection = true , -- Highlight selection in source buffer
highlight_headers = true , -- Highlight headers in chat
auto_follow_cursor = true , -- Auto-follow cursor in chat
auto_insert_mode = false , -- Automatically enter insert mode when opening window
insert_at_end = false , -- Move cursor to end of buffer when inserting text
clear_chat_on_new_prompt = false , -- Clears chat on every new prompt
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Chat features
chat_autocomplete = true , -- Enable chat autocompletion
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Default selection (uses visual selection or falls back to buffer)
selection = function ( source )
return select.visual ( source ) or select.buffer ( source )
end ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Custom prompts for various coding tasks
2025-05-29 19:04:44 +05:30
prompts = {
2025-06-14 09:15:11 +05:30
-- Code explanation
2025-05-29 19:04:44 +05:30
Explain = {
2025-06-14 09:15:11 +05:30
prompt = ' Write an explanation for the selected code as paragraphs of text. ' ,
system_prompt = ' COPILOT_EXPLAIN ' ,
} ,
-- Code review
Review = {
prompt = ' Review the selected code. ' ,
system_prompt = ' COPILOT_REVIEW ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Bug fixes
Fix = {
prompt = ' There is a problem in this code. Identify the issues and rewrite the code with fixes. Explain what was wrong and how your changes address the problems. ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Code optimization
2025-05-29 19:04:44 +05:30
Optimize = {
2025-06-14 09:15:11 +05:30
prompt = ' Optimize the selected code to improve performance and readability. Explain your optimization strategy and the benefits of your changes. ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Documentation generation
Docs = {
prompt = ' Please add documentation comments to the selected code. ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Test generation
2025-05-29 19:04:44 +05:30
Tests = {
2025-06-14 09:15:11 +05:30
prompt = ' Please generate tests for my code. ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Commit message generation
Commit = {
prompt = ' Write commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block. ' ,
context = ' git:staged ' ,
2025-05-29 19:04:44 +05:30
} ,
2025-06-14 09:15:11 +05:30
-- Custom advanced prompts
Refactor = {
prompt = ' Please refactor the following code to improve its structure and readability. Explain the changes you made and why they improve the code. ' ,
} ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
BestPractices = {
prompt = ' Review this code for best practices and suggest improvements. Focus on code quality, maintainability, and adherence to language-specific conventions. ' ,
} ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
Security = {
prompt = ' Analyze this code for potential security vulnerabilities and suggest fixes. Consider common security issues like injection attacks, authentication, authorization, and data validation. ' ,
} ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
Performance = {
prompt = ' Analyze this code for performance issues and suggest optimizations. Consider algorithmic complexity, memory usage, and language-specific performance patterns. ' ,
} ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Context-aware prompts
ImplementFeature = {
prompt = ' Based on the selected code, implement the following feature: ' ,
mapping = ' <leader>cif ' ,
description = ' Implement a new feature based on existing code ' ,
} ,
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
ExplainError = {
prompt = ' Explain this error and provide a solution: ' ,
mapping = ' <leader>cee ' ,
description = ' Explain error and provide solution ' ,
} ,
} ,
-- Custom mappings for chat buffer
mappings = {
complete = {
insert = ' <Tab> ' ,
} ,
close = {
normal = ' q ' ,
insert = ' <C-c> ' ,
} ,
reset = {
normal = ' <C-l> ' ,
insert = ' <C-l> ' ,
} ,
submit_prompt = {
normal = ' <CR> ' ,
insert = ' <C-s> ' ,
} ,
toggle_sticky = {
normal = ' grr ' ,
} ,
clear_stickies = {
normal = ' grx ' ,
} ,
accept_diff = {
normal = ' <C-y> ' ,
insert = ' <C-y> ' ,
} ,
jump_to_diff = {
normal = ' gj ' ,
} ,
quickfix_answers = {
normal = ' gqa ' ,
} ,
quickfix_diffs = {
normal = ' gqd ' ,
} ,
yank_diff = {
normal = ' gy ' ,
register = ' " ' ,
} ,
show_diff = {
normal = ' gd ' ,
full_diff = false ,
} ,
show_info = {
normal = ' gi ' ,
} ,
show_context = {
normal = ' gc ' ,
} ,
show_help = {
normal = ' gh ' ,
} ,
} ,
2025-05-29 19:04:44 +05:30
} )
2025-06-14 09:15:11 +05:30
-- Global keymaps for CopilotChat
-- Core commands
vim.keymap . set ( " n " , " <leader>cc " , function ( ) chat.toggle ( ) end , { desc = " Toggle Copilot Chat " } )
vim.keymap . set ( " n " , " <leader>co " , function ( ) chat.open ( ) end , { desc = " Open Copilot Chat " } )
vim.keymap . set ( " n " , " <leader>cx " , function ( ) chat.close ( ) end , { desc = " Close Copilot Chat " } )
vim.keymap . set ( " n " , " <leader>cr " , function ( ) chat.reset ( ) end , { desc = " Reset Copilot Chat " } )
vim.keymap . set ( " n " , " <leader>cs " , function ( ) chat.stop ( ) end , { desc = " Stop Copilot Chat " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Prompt-based commands - these work with current selection
vim.keymap . set ( { " n " , " v " } , " <leader>cce " , " <cmd>CopilotChatExplain<cr> " , { desc = " Explain code " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccr " , " <cmd>CopilotChatReview<cr> " , { desc = " Review code " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccf " , " <cmd>CopilotChatFix<cr> " , { desc = " Fix code " } )
vim.keymap . set ( { " n " , " v " } , " <leader>cco " , " <cmd>CopilotChatOptimize<cr> " , { desc = " Optimize code " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccd " , " <cmd>CopilotChatDocs<cr> " , { desc = " Generate docs " } )
vim.keymap . set ( { " n " , " v " } , " <leader>cct " , " <cmd>CopilotChatTests<cr> " , { desc = " Generate tests " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccrf " , " <cmd>CopilotChatRefactor<cr> " , { desc = " Refactor code " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccb " , " <cmd>CopilotChatBestPractices<cr> " , { desc = " Best practices " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccs " , " <cmd>CopilotChatSecurity<cr> " , { desc = " Security review " } )
vim.keymap . set ( { " n " , " v " } , " <leader>ccp " , " <cmd>CopilotChatPerformance<cr> " , { desc = " Performance review " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Git integration
vim.keymap . set ( " n " , " <leader>ccg " , " <cmd>CopilotChatCommit<cr> " , { desc = " Generate commit message " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Advanced features
vim.keymap . set ( " n " , " <leader>ccm " , " <cmd>CopilotChatModels<cr> " , { desc = " Select model " } )
vim.keymap . set ( " n " , " <leader>cca " , " <cmd>CopilotChatAgents<cr> " , { desc = " Select agent " } )
vim.keymap . set ( " n " , " <leader>ccp " , " <cmd>CopilotChatPrompts<cr> " , { desc = " Select prompt " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Chat history
vim.keymap . set ( " n " , " <leader>ccl " , function ( )
vim.ui . input ( { prompt = " Load chat (name): " } , function ( name )
if name then
chat.load ( name )
2025-05-29 19:04:44 +05:30
end
end )
2025-06-14 09:15:11 +05:30
end , { desc = " Load chat history " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
vim.keymap . set ( " n " , " <leader>ccS " , function ( )
vim.ui . input ( { prompt = " Save chat (name): " } , function ( name )
if name then
chat.save ( name )
end
end )
end , { desc = " Save chat history " } )
2025-05-29 19:04:44 +05:30
2025-06-14 09:15:11 +05:30
-- Custom prompts with input
vim.keymap . set ( { " n " , " v " } , " <leader>cci " , function ( )
2025-05-29 19:04:44 +05:30
vim.ui . input ( { prompt = " Ask Copilot: " } , function ( input )
if input then
chat.ask ( input )
end
end )
2025-06-14 09:15:11 +05:30
end , { desc = " Ask custom question " } )
-- Context-specific commands
vim.keymap . set ( " n " , " <leader>ccbf " , function ( )
chat.ask ( " Explain what this file does and its main purpose. " , {
selection = select.buffer ,
} )
end , { desc = " Explain buffer " } )
vim.keymap . set ( " n " , " <leader>ccgd " , function ( )
chat.ask ( " Explain these git changes. " , {
context = " git " ,
} )
end , { desc = " Explain git diff " } )
-- Line-specific command
vim.keymap . set ( " n " , " <leader>ccl " , function ( )
chat.ask ( " Explain this line of code in detail. " , {
selection = select.line ,
} )
end , { desc = " Explain current line " } )
-- Quick actions for selected text
vim.keymap . set ( " v " , " <leader>cq " , function ( )
chat.ask ( " Quick question about this code: " .. vim.fn . input ( " Question: " ) , {
selection = select.visual ,
} )
end , { desc = " Quick question about selection " } )
-- Buffer auto-commands for chat window
vim.api . nvim_create_autocmd ( " BufEnter " , {
pattern = " copilot-* " ,
callback = function ( )
-- Set buffer-local options for better UX
vim.opt_local . relativenumber = false
vim.opt_local . number = false
vim.opt_local . cursorline = false
vim.opt_local . signcolumn = " no "
vim.opt_local . foldcolumn = " 0 "
end ,
} )
-- Notification on chat completion
vim.api . nvim_create_autocmd ( " User " , {
pattern = " CopilotChatComplete " ,
callback = function ( )
vim.notify ( " Copilot Chat response complete " , vim.log . levels.INFO )
end ,
} )
2025-05-29 19:04:44 +05:30
end ,
}