Copilot Chat functionality without having to leave Vim.
Nvim folks will be able to use CopilotChat.nvim for a similar experience.
Using Vundle, Pathogen vim-plug, Vim 8+ packages, or any other plugin manager.
Add into .vimrc configuration.
call vundle#begin()
Plugin 'DanBradbury/copilot-chat.vim'
call vundle#end()
filetype plugin indent onClone repository.
git clone https://github.com/DanBradbury/copilot-chat.vim.git ~/.vim/bundleAdd into .vimrc configuration.
call pathogen#infect()
syntax on
filetype plugin indent onAdd into .vimrc configuration.
call plug#begin()
Plug 'DanBradbury/copilot-chat.vim'
call plug#end()
filetype plugin indent onClone repository.
git clone https://github.com/DanBradbury/copilot-chat.vim.git ~/.vim/pack/plugins/startAdd into .vimrc configuration.
filetype plugin indent on- After installing the plugin, the first time you launch vim you'll be presented with the device registration page in your default browser.
- Follow the steps on the page and paste the Device Code when prompted.
- Once completed press
<Enter>back in vim to complete the registration process. - Start chatting with Copilot (
:CopilotChatOpen,:CopilotChat simple question, etc) - 🎉🎉🎉
| Command | Description |
|---|---|
:CopilotChat <input> |
Launches a new Copilot chat with your input as the initial prompt |
:CopilotChatOpen |
Opens a new Copilot chat window (default vsplit right) |
:CopilotChatFocus |
Focuses the currently active chat window |
:CopilotChatReset |
Resets the current chat window |
:CopilotChatConfig |
Open config.json for default settings when opening a new CopilotChat window |
:CopilotChatModels |
View available models / select active model |
:CopilotChatSave <name>? |
Save chat history (uses timestamp if no name provided) |
:CopilotChatLoad <name>? |
Load chat history (shows list of saved histories if no name provided) |
:CopilotChatList |
List all saved chat histories |
:CopilotChatSetActive <bufnr>? |
Sets the active chat window to the buffer number provided (default is the current buffer) |
:CopilotChatUsage |
Show Copilot usage stats |
| Key | Description |
|---|---|
<Plug>CopilotChatAddSelection |
Copies selected text into active char buffer |
| Location | Insert | Normal | Visual | Action |
|---|---|---|---|---|
| Chat window | - | <CR> |
- | Submit current prompt |
| Models selection popup | - | <CR> |
<Space> |
- |
The plugin avoids adding any default vim key mappings to prevent conflict with other plugins and the users' own mappings.
However, to easily work with the Copilot Chat plugin, the user might want to setup his own vim key mappings. See example configuration below:
" Open a new Copilot Chat window
nnoremap <leader>cc :CopilotChatOpen<CR>
" Add visual selection to Copilot window
vmap <leader>a <Plug>CopilotChatAddSelectionThe plugin includes autocomplete macros, specifically designed to enhance productivity when working with file references.
- Typing
/tab allin the chat window will automatically expand into a list of all open tabs (excluding the current buffer) with their filenames prefixed by#file:. - The filenames are displayed in their relative path format, making it easier to reference files in your project.
- If no other tabs are found, the message
No other tabs foundwill be inserted instead.
- When typing
#file:in the chat window, the plugin provides an autocomplete menu for file paths. - The autocomplete intelligently suggests files based on:
- Files tracked in the current Git repository (if inside a Git project).
- All files in the current working directory (if not in a Git project).
- The suggestions exclude directories and only include files that match the text typed after
#file:. - Example:
- Typing
#file:src/will show a list of files in thesrc/directory. - Selecting a file from the menu will insert its full path.
- Typing
:CopilotChatModels brings up a popup menu for of all the available models for you to choose from. Press <Enter> or <Space> to select the highlighted model. New chats will use the selected model.
By default, this is configured to <Leader>a when in visual mode.
Save and restore your chat conversations with Copilot:
- Use
:CopilotChatSave <name>to save the current chat history - If no name is provided, a timestamp will be used automatically
- History files are stored in
~/.vim/copilot-chat/history/as JSON files
- Use
:CopilotChatLoad <name>to load a previously saved chat - If no name is provided, a list of available chat histories will be shown
- You can also view all saved histories with
:CopilotChatList
Copilot Chat supports custom prompt templates that can be quickly accessed during chat sessions. Templates allow you to save frequently used prompts and invoke them with a simple syntax.
- In the chat window, start a line with
> PROMPT_NAME - The
PROMPT_NAMEwill be automatically replaced with the template content before sending to Copilot - Example:
> explainwould expand to the full explanation template
- Open the config with
:CopilotChatConfig - Add prompts to the
promptsobject inconfig.json:
{
"model": "gpt-4",
"prompts": {
"explain": "Explain how this code works in detail:",
"refactor": "Suggest improvements and refactoring for this code:",
"docs": "Generate documentation for this code:"
}
}> explain
function validateUser() {
// code to validate
}
This will send the full template text + your code to Copilot.
You can customize the behavior of Copilot Chat by setting global variables in your vimrc.
| Variable | Description |
|---|---|
g:copilot_chat_window_position |
Controls the split direction when opening a chat window. Valid values: 'right' (default), 'left', 'top', 'bottom'. Example: let g:copilot_chat_window_position = 'bottom'. Takes precedence over config.json. |
g:copilot_chat_disable_mappings |
Set to 1 to disable the plugin's default mappings in the chat window. |
g:copilot_chat_create_on_add_selection |
Set to 1 to create a new chat window when adding a visual selection if no chat window is open. If set to 0, no action is taken when no active chat window exists (default: 1). |
g:copilot_chat_jump_to_chat_on_add_selection |
Set to 1 to jump to the chat window when adding a visual selection. If set to 0, the selection is added to the active chat window, but the user remains in the current window (default: 1). |
g:copilot_reuse_active_chat |
Set to 1 to reuse the active chat window when opening a new chat window. If set to 0, a new chat window is always created. Adding selection is always appended to the active chat window, regardless of this setting (default: 1). |
g:copilot_chat_data_dir |
Set to the directory where the plugin stores its data. By default, this is ~/.vim/copilot-chat/. |
g:copilot_chat_open_on_toggle |
Set to 0 to prevent a new chat window from opening when toggling the chat window. |
g:copilot_list_chat_buffer |
By default, copilot-chat buffers are not listed. Set to 1 to change this behavior. |
g:copilot_chat_message_history_limit |
Maximum number of messages to send to the API (default: 20). Limits context to improve performance with long chat histories. Set to a higher value if you need more context, or lower for better performance. |
g:copilot_chat_syntax_debounce_ms |
Debounce delay in milliseconds for syntax highlighting (default: 300). Lower values update highlighting faster but use more CPU. Higher values improve performance but delay syntax updates. |
g:copilot_chat_file_cache_timeout |
Cache timeout in seconds for file completion (default: 5). Lower values show new files faster but make more system calls. Higher values improve performance but delay showing new files. |
For example, to always open chats in a horizontal split at the bottom:
let g:copilot_chat_window_position = 'bottom'For better performance with long chat sessions, you can adjust these settings:
" Send only the last 10 messages for faster responses
let g:copilot_chat_message_history_limit = 10
" Increase debounce delay for slower machines (reduces CPU usage)
let g:copilot_chat_syntax_debounce_ms = 500
" Longer cache timeout for large projects (reduces system calls)
let g:copilot_chat_file_cache_timeout = 10The following error message means the logged in account does not have Copilot activated:
Resource not accessible by integration
Please see the contribution guide for more information.