Skip to content

cjoke/CopilotChat.nvim

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Copilot Chat for Neovim

Prerequisite Documentation pre-commit.ci status Discord

All Contributors

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.

Prerequisites

Ensure you have the following installed:

  • Python 3.10 or later.
  • Python3 provider: You can check if it's enabled by running :echo has('python3') in Neovim. If it returns 1, then the Python3 provider is enabled.
  • rplugin: This plugin uses the remote plugin system of Neovim. Make sure you have it enabled.

Authentication

It will prompt you with instructions on your first start. If you already have Copilot.vim or Copilot.lua, it will work automatically.

Installation

Lazy.nvim

  1. pip install python-dotenv requests pynvim==0.5.0 prompt-toolkit
  2. pip install tiktoken (optional for displaying prompt token counts)
  3. Put it in your lazy setup
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",
      }
    },
  },
}
  1. Run command :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': '*'}},
     \ ])
  1. Restart neovim

Vim-Plug

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.

Manual

  1. Put the files in the right place
$ git clone https://github.com/CopilotC-Nvim/CopilotChat.nvim
$ cd CopilotChat.nvim
$ cp -r --backup=nil rplugin ~/.config/nvim/
  1. Install dependencies
$ pip install -r requirements.txt
  1. Add to you configuration
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>
  1. Open up Neovim and run :UpdateRemotePlugins
  2. Restart Neovim

Usage

Configuration

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.

Chat with Github Copilot

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChat followed by your question. For example, :CopilotChat What does this code do?

Chat Demo

Code Explanation

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChatExplain.

Explain Code Demo

Generate Tests

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChatTests.

Generate tests

Troubleshoot and Fix Diagnostic

  1. Place your cursor on the line with the diagnostic message.
  2. Run the command :CopilotChatFixDiagnostic.

Fix diagnostic

Token count & Fold with visual mode

  1. Select some code using visual mode.
  2. Run the command :CopilotChatVisual with your question.

Fold Demo

In-place Chat Popup

  1. Select some code using visual mode.
  2. Run the command :CopilotChatInPlace and type your prompt. For example, What does this code do?
  3. Press Enter to send your question to Github Copilot.
  4. Press q to quit. There is help text at the bottom of the screen. You can also press ? to toggle the help text.

In-place Demo

Toggle Vertical Split with :CopilotChatVsplitToggle

Toggle

Chat with Copilot with all contents of InFocus buffer

  1. Run the command :CopilotChatBuffer and type your prompt. For example, What does this code do?
  2. Press Enter to send your question to Github Copilot.
  3. Copilot will pull the content of the infocus buffer and chat with you.

Tips

Quick chat with your buffer

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",
      },

Chat with buffer

Integration with 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",
      },
    }
  }
  1. Select help actions base the diagnostic message under the cursor. Help action with Copilot Chat

  2. Select action base on user prompts. Select action base on user prompts

Integration with 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
      },
    },
  },
}

Layout

Debugging with :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.

Debug Info

How to setup with 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" },
    }
  },

Create a simple input for CopilotChat

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",
  },

Add same keybinds in both visual and normal mode

    {
      "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,
    },

Roadmap (Wishlist)

  • Use vector encodings to automatically select code
  • Treesitter integration for function definitions
  • General QOL improvements

Development

Installing Pre-commit Tool

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.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

gptlang
gptlang

πŸ’» πŸ“–
Dung Duc Huynh (Kaka)
Dung Duc Huynh (Kaka)

πŸ’» πŸ“–
Ahmed Haracic
Ahmed Haracic

πŸ’»
TrΓ­ Thiện Nguyα»…n
TrΓ­ Thiện Nguyα»…n

πŸ’»
He Zhizhou
He Zhizhou

πŸ’»
Guruprakash Rajakkannu
Guruprakash Rajakkannu

πŸ’»
kristofka
kristofka

πŸ’»
PostCyberPunk
PostCyberPunk

πŸ“–
Katsuhiko Nishimra
Katsuhiko Nishimra

πŸ’»
Erno Hopearuoho
Erno Hopearuoho

πŸ’»
Shaun Garwood
Shaun Garwood

πŸ’»
neutrinoA4
neutrinoA4

πŸ’» πŸ“–
Jack Muratore
Jack Muratore

πŸ’»
Adriel Velazquez
Adriel Velazquez

πŸ’» πŸ“–
Tomas Slusny
Tomas Slusny

πŸ’» πŸ“–
Nisal
Nisal

πŸ“–
Oleksandr Slynko
Oleksandr Slynko

πŸ“–

This project follows the all-contributors specification. Contributions of any kind are welcome!

Stargazers over time

Stargazers over time

About

Chat with GitHub Copilot in Neovim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 79.7%
  • Lua 19.8%
  • Makefile 0.5%