codecompanion.nvim

✨ AI-powered coding, seamlessly in Neovim. Supports Anthropic, Copilot, Gemini, Ollama and OpenAI LLMs

MIT License

Stars
684
Committers
21

πŸ’œ Sponsors

Thank you to the following people:

✨ Features

  • πŸ’¬ Copilot Chat meets Zed AI, in Neovim
  • πŸ”Œ Support for Anthropic, Copilot, Gemini, Ollama and OpenAI LLMs (or bring your own!)
  • πŸš€ Inline transformations, code creation and refactoring
  • πŸ€– Variables, Slash Commands, Agents/Tools and Workflows to improve LLM output
  • ✨ Built in prompt library for common tasks like advice on LSP errors and code explanations
  • πŸ—οΈ Create your own custom prompts, Variables and Slash Commands
  • πŸ“š Have multiple chats open at the same time
  • πŸ’ͺ Async execution for fast performance

πŸ“Έ Screenshots

⚑ Requirements

  • The curl library
  • Neovim 0.10.0 or greater
  • (Optional) An API key for your chosen LLM

πŸ“¦ Installation

Install the plugin with your preferred package manager:

Lazy.nvim

{
  "olimorris/codecompanion.nvim",
  dependencies = {
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
    "hrsh7th/nvim-cmp", -- Optional: For using slash commands and variables in the chat buffer
    "nvim-telescope/telescope.nvim", -- Optional: For using slash commands
    { "stevearc/dressing.nvim", opts = {} }, -- Optional: Improves `vim.ui.select`
  },
  config = true
}

Packer

use({
  "olimorris/codecompanion.nvim",
  config = function()
    require("codecompanion").setup()
  end,
  requires = {
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
    "hrsh7th/nvim-cmp", -- Optional: For using slash commands and variables in the chat buffer
    "nvim-telescope/telescope.nvim", -- Optional: For using slash commands
    "stevearc/dressing.nvim" -- Optional: Improves `vim.ui.select`
  }
})

vim-plug

call plug#begin()

Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'hrsh7th/nvim-cmp', " Optional: For using slash commands and variables in the chat buffer
Plug 'nvim-telescope/telescope.nvim', " Optional: For using slash commands
Plug 'stevearc/dressing.nvim' " Optional: Improves `vim.ui.select`
Plug 'olimorris/codecompanion.nvim'

call plug#end()

lua << EOF
  require("codecompanion").setup()
EOF

[!IMPORTANT] The plugin requires the markdown Tree-sitter parser to be installed with :TSInstall markdown

Telescope.nvim is a suggested inclusion in order to leverage Slash Commands. However other providers are available. Please refer to the Chat Buffer section for more information.

πŸš€ Quickstart

[!NOTE] Okay, okay...it's not quite a quickstart as you'll need to configure an adapter first.

Chat Buffer

Run :CodeCompanionChat to open the chat buffer. Type your prompt and press <CR>. Or, run :CodeCompanionChat why are Lua and Neovim so perfect together? to send a prompt directly to the chat buffer. Toggle the chat buffer with :CodeCompanionChat Toggle.

You can add context from your code base by using Variables and Slash Commands in the chat buffer.

Variables, accessed via #, contain data about the present state of Neovim:

  • #buffer - Shares the current buffer's code. You can also specify line numbers with #buffer:8-20
  • #lsp - Shares LSP information and code for the current buffer
  • #viewport - Shares the buffers and lines that you see in the Neovim viewport

Slash commands, accessed via /, run commands to insert additional context into the chat buffer:

  • /buffer - Insert open buffers
  • /fetch - Insert URL contents
  • /file - Insert a file
  • /help - Insert content from help tags
  • /now - Insert the current date and time
  • /symbols - Insert symbols for the active buffer
  • /terminal - Insert terminal output

Tools, accessed via @, allow the LLM to function as an agent and carry out actions:

  • @code_runner - The LLM will run code for you in a Docker container
  • @editor - The LLM will edit code in a Neovim buffer
  • @rag - The LLM will browse and search the internet for real-time information to supplement its response

[!TIP] Press ? in the chat buffer to reveal the keymaps and options that are available.

Inline Assistant

[!NOTE] The diff provider was selected as mini_pick in the video above

Run :CodeCompanion <your prompt> to call the inline assistant. The assistant will evaluate the prompt and either write code or open a chat buffer. You can also make a visual selection and call the assistant.

The assistant has knowledge of your last conversation from a chat buffer. A prompt such as :CodeCompanion add the new function here will see the assistant add a code block directly into the current buffer.

For convenience, you can call prompts from the prompt library via the assistant such as :'<,'>CodeCompanion /buffer what does this file do?. The prompt library comes with the following defaults:

  • /buffer - Send the current buffer to the LLM alongside a prompt
  • /commit - Generate a commit message
  • /explain - Explain how selected code in a buffer works
  • /fix - Fix the selected code
  • /lsp - Explain the LSP diagnostics for the selected code
  • /tests - Generate unit tests for selected code

There are keymaps available to accept or reject edits from the LLM in the inline assistant section.

Action Palette

Run :CodeCompanionActions to open the action palette, which gives you access to all functionality of the plugin. By default the plugin uses vim.ui.select however you can change the provider by altering the display.action_palette.provider config value to be telescope or mini_pick. You can also call the Telescope extension with :Telescope codecompanion.

[!NOTE] Some actions and prompts will only be visible if you're in Visual mode.

List of commands

The plugin has three core commands:

  • CodeCompanion - Open the inline assistant
  • CodeCompanionChat - Open a chat buffer
  • CodeCompanionActions - Open the Action Palette

However there are multiple options available:

  • CodeCompanion <your prompt> - Prompt the inline assistant
  • CodeCompanion /<prompt library> - Use the prompt library with the inline assistant e.g. /commit
  • CodeCompanionChat <prompt> - Send a prompt to the LLM via a chat buffer
  • CodeCompanionChat <adapter> - Open a chat buffer with a specific adapter
  • CodeCompanionChat Toggle - Toggle a chat buffer
  • CodeCompanionChat Add - Add visually selected chat to the current chat buffer

Suggested workflow

For an optimum workflow, I recommend the following keymaps:

vim.api.nvim_set_keymap("n", "<C-a>", "<cmd>CodeCompanionActions<cr>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("v", "<C-a>", "<cmd>CodeCompanionActions<cr>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<LocalLeader>a", "<cmd>CodeCompanionChat Toggle<cr>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("v", "<LocalLeader>a", "<cmd>CodeCompanionChat Toggle<cr>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("v", "ga", "<cmd>CodeCompanionChat Add<cr>", { noremap = true, silent = true })

-- Expand 'cc' into 'CodeCompanion' in the command line
vim.cmd([[cab cc CodeCompanion]])

[!NOTE] You can also assign prompts from the library to specific mappings. See the prompt library section for more information.

βš™οΈ Configuration

Before configuring the plugin, it's important to understand how it's structured.

The plugin uses adapters to connect to LLMs. Out of the box, the plugin supports:

  • Anthropic (anthropic) - Requires an API key and supports prompt caching
  • Copilot (copilot) - Requires a token which is created via :Copilot setup in Copilot.vim
  • Gemini (gemini) - Requires an API key
  • Ollama (ollama) - Both local and remotely hosted
  • OpenAI (openai) - Requires an API key

The plugin utilises objects called Strategies. These are the different ways that a user can interact with the plugin. The chat strategy harnesses a buffer to allow direct conversation with the LLM. The inline strategy allows for output from the LLM to be written directly into a pre-existing Neovim buffer.

The plugin allows you to specify adapters for each strategy and also for each prompt library entry.

πŸ› οΈ Changing the Defaults

The default config can be found in the config.lua file and the defaults can be changed by calling the setup function:

require("codecompanion").setup({
  display = {
    diff = {
      provider = "mini_diff",
    },
  },
  opts = {
    log_level = "DEBUG",
  },
})

Please refer to the adapter section below in order to configure adapters.

Changing the System Prompt

The default system prompt has been carefully curated to deliver responses which are similar to GitHub Copilot Chat, no matter which LLM you use. That is, you'll receive responses which are terse, professional and with expertise in coding. However, you can modify the opts.system_prompt table in the config to suit your needs. You can also set it as a function which can receive the current chat buffer's adapter as a parameter, giving you the option of setting system prompts that are LLM or model specific:

require("codecompanion").setup({
  opts = {
    ---@param adapter CodeCompanion.Adapter
    ---@return string
    system_prompt = function(adapter)
      if adapter.schema.model.default == "llama3.1:latest" then
        return "My custom system prompt"
      end
      return "My default system prompt"
    end
  }
})

πŸ”Œ Adapters

Please refer to your chosen adapter to understand its configuration. You will need to set an API key for non-locally hosted LLMs.

[!TIP] To create your own adapter or better understand how they work, please refer to the ADAPTERS guide.

Changing the Default Adapter

To specify a different adapter to the default (openai), simply change the strategies.* table:

require("codecompanion").setup({
  strategies = {
    chat = {
      adapter = "anthropic",
    },
    inline = {
      adapter = "copilot",
    },
    agent = {
      adapter = "anthropic",
    },
  },
})

Setting an API Key

require("codecompanion").setup({
  adapters = {
    anthropic = function()
      return require("codecompanion.adapters").extend("anthropic", {
        env = {
          api_key = "MY_OTHER_ANTHROPIC_KEY"
        },
      })
    end,
  },
})

In the example above, we're using the base of the Anthropic adapter but changing the name of the default API key which it uses.

Setting an API Key Using a Command

Having API keys in plain text in your shell is not always safe. Thanks to this PR, you can run commands from within your config by prefixing them with cmd:. In the example below, we're using the 1Password CLI to read an OpenAI credential.

require("codecompanion").setup({
  adapters = {
    openai = function()
      return require("codecompanion.adapters").extend("openai", {
        env = {
          api_key = "cmd:op read op://personal/OpenAI/credential --no-newline",
        },
      })
    end,
  },
})

Using Ollama Remotely

To use Ollama remotely, change the URL in the env table, set an API key and pass it via an "Authorization" header:

require("codecompanion").setup({
  adapters = {
    ollama = function()
      return require("codecompanion.adapters").extend("ollama", {
        env = {
          url = "https://my_ollama_url",
          api_key = "OLLAMA_API_KEY",
        },
        headers = {
          ["Content-Type"] = "application/json",
          ["Authorization"] = "Bearer ${api_key}",
        },
        parameters = {
          sync = true,
        },
      })
    end,
  },
})

Connecting via a Proxy

You can also connect via a proxy:

require("codecompanion").setup({
  adapters = {
    opts = {
      allow_insecure = true, -- Use if required
      proxy = "socks5://127.0.0.1:9999"
    }
  },
})

Changing an Adapter's Default Model

A common ask is to change an adapter's default model. This can be done by altering the schema.model.default table:

require("codecompanion").setup({
  adapters = {
    anthropic = function()
      return require("codecompanion.adapters").extend("anthropic", {
        schema = {
          model = {
            default = "claude-3-opus-20240229",
          },
        },
      })
    end,
  },
})

Configuring Adapter Settings

LLMs have many settings such as model, temperature and max_tokens. In an adapter, these sit within a schema table and can be configured during setup:

require("codecompanion").setup({
  adapters = {
    llama3 = function()
      return require("codecompanion.adapters").extend("ollama", {
        name = "llama3", -- Give this adapter a different name to differentiate it from the default ollama adapter
        schema = {
          model = {
            default = "llama3:latest",
          },
          num_ctx = {
            default = 16384,
          },
          num_predict = {
            default = -1,
          },
        },
      })
    end,
  },
})

πŸ’‘ Advanced Usage

πŸ“‹ Prompt Library

The plugin comes with a number of pre-built prompts. As per the config, these can be called via keymaps or via the cmdline. These prompts have been carefully curated to mimic those in GitHub's Copilot Chat. Of course, you can create your own prompts and add them to the Action Palette or even to the slash command completion menu in the chat buffer. Please see the RECIPES guide for more information.

Using Keymaps

You can call a prompt from the library via a keymap using the prompt helper:

vim.api.nvim_set_keymap("v", "<LocalLeader>ce", "", {
  callback = function()
    require("codecompanion").prompt("explain")
  end,
  noremap = true,
  silent = true,
})

In the example above, we've set a visual keymap that will trigger the Explain prompt. Providing the short_name of the prompt as an argument to the helper (e.g. "commit") will resolve the strategy down to an action.

πŸ’¬ The Chat Buffer

The chat buffer is where you converse with an LLM from within Neovim. The chat buffer has been designed to be turn based, whereby you send a message and the LLM replies. Messages are segmented by H2 headers and once a message has been sent, it cannot be edited. You can also have multiple chat buffers open at the same.

The look and feel of the chat buffer can be customised as per the display.chat table in the config. You can also add additional Variables and Slash Commands which can then be referenced in the chat buffer.

Keymaps

When in the chat buffer, press ? to bring up a menu that lists the available keymaps, variables, slash commands and tools.

Settings

You can display your selected adapter's schema at the top of the buffer, if display.chat.show_settings is set to true. This allows you to vary the response from the LLM.

Slash Commands

As outlined in the Quickstart section, Slash Commands allow you to easily share additional context with your LLM from the chat buffer. Some of the commands also allow for multiple providers:

  • /buffer - Has a default provider (which leverages vim.ui.select) alongside telescope and fzf_lua providers
  • /files - Has telescope, mini_pick and fzf_lua providers

Please refer to the config to see how to change the default provider.

✏️ Inline Assistant

[!NOTE] If you've set opts.send_code = false in your config then the plugin will endeavour to ensure no code is sent to the LLM.

One of the challenges with inline editing is determining how the LLM's response should be handled in the buffer. If you've prompted the LLM to "create a table of 5 common text editors" then you may wish for the response to be placed at the cursor's position in the current buffer. However, if you asked the LLM to "refactor this function" then you'd expect the response to replace a visual selection. The plugin will use the inline LLM you've specified in your config to determine if the response should...

  • replace - replace a visual selection you've made
  • add - be added in the current buffer at the cursor position
  • new - be placed in a new buffer
  • chat - be placed in a chat buffer

By default, an inline assistant prompt will trigger the diff feature, showing differences between the original buffer and the changes from the LLM. This can be turned off in your config via the display.diff.provider table. You can also choose to accept or reject the LLM's suggestions with the following keymaps:

  • ga - Accept an inline edit
  • gr - Reject an inline edit

πŸ€– Agents / Tools

As outlined by Andrew Ng in Agentic Design Patterns Part 3, Tool Use, LLMs can act as agents by leveraging external tools. Andrew notes some common examples such as web searching or code execution that have obvious benefits when using LLMs.

In the plugin, tools are simply context that's given to an LLM via a system prompt. This gives it knowledge and a defined schema which it can include in its response for the plugin to parse, execute and feedback on. Essentially turning it into an Agent. Tools can be added as a participant to the chat buffer by using the @ key.

More information on how tools work and how you can create your own can be found in the TOOLS guide.

πŸ—ΊοΈ Workflows

[!WARNING] Workflows may result in the significant consumption of tokens if you're using an external LLM.

As outlined by Andrew Ng, agentic workflows have the ability to dramatically improve the output of an LLM. Infact, it's possible for older models like GPT 3.5 to outperform newer models (using traditional zero-shot inference). Andrew discussed how an agentic workflow can be utilised via multiple prompts that invoke the LLM to self reflect. Implementing Andrew's advice, the plugin supports this notion via the use of workflows. At various stages of a pre-defined workflow, the plugin will automatically prompt the LLM without any input or triggering required from the user.

Currently, the plugin comes with the following workflows:

  • Adding a new feature
  • Refactoring code

Of course you can add new workflows by following the RECIPES guide.

🍭 Extras

Highlight Groups

The plugin sets the following highlight groups during setup:

  • CodeCompanionChatHeader - The headers in the chat buffer
  • CodeCompanionChatSeparator - Separator between headings in the chat buffer
  • CodeCompanionChatTokens - Virtual text in the chat buffer showing the token count
  • CodeCompanionChatTool - Tools in the chat buffer
  • CodeCompanionChatVariable - Variables in the chat buffer
  • CodeCompanionVirtualText - All other virtual text in the plugin

Events/Hooks

The plugin fires many events during its lifecycle:

  • CodeCompanionChatClosed - Fired after a chat has been closed
  • CodeCompanionChatAdapter - Fired after the adapter has been set in the chat
  • CodeCompanionChatModel - Fired after the model has been set in the chat
  • CodeCompanionToolAdded - Fired when a tool has been added to a chat
  • CodeCompanionAgentStarted - Fired when an agent has been initiated in the chat
  • CodeCompanionAgentFinished - Fired when an agent has finished all tool executions
  • CodeCompanionInlineStarted - Fired at the start of the Inline strategy
  • CodeCompanionInlineFinished - Fired at the end of the Inline strategy
  • CodeCompanionRequestStarted - Fired at the start of any API request
  • CodeCompanionRequestFinished - Fired at the end of any API request
  • CodeCompanionDiffAttached - Fired when in Diff mode
  • CodeCompanionDiffDetached - Fired when exiting Diff mode

[!TIP] Some events are sent with a data payload which can be leveraged.

Events can be hooked into as follows:

local group = vim.api.nvim_create_augroup("CodeCompanionHooks", {})

vim.api.nvim_create_autocmd({ "User" }, {
  pattern = "CodeCompanionInline*",
  group = group,
  callback = function(request)
    if request.match == "CodeCompanionInlineFinished" then
      -- Format the buffer after the inline request has completed
      require("conform").format({ bufnr = request.buf })
    end
  end,
})

Statuslines

You can incorporate a visual indication to show when the plugin is communicating with an LLM in your Neovim configuration. Below are examples for two popular statusline plugins.

lualine.nvim:

local M = require("lualine.component"):extend()

M.processing = false
M.spinner_index = 1

local spinner_symbols = {
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
}
local spinner_symbols_len = 10

-- Initializer
function M:init(options)
  M.super.init(self, options)

  local group = vim.api.nvim_create_augroup("CodeCompanionHooks", {})

  vim.api.nvim_create_autocmd({ "User" }, {
    pattern = "CodeCompanionRequest*",
    group = group,
    callback = function(request)
      if request.match == "CodeCompanionRequestStarted" then
        self.processing = true
      elseif request.match == "CodeCompanionRequestFinished" then
        self.processing = false
      end
    end,
  })
end

-- Function that runs every time statusline is updated
function M:update_status()
  if self.processing then
    self.spinner_index = (self.spinner_index % spinner_symbols_len) + 1
    return spinner_symbols[self.spinner_index]
  else
    return nil
  end
end

return M

heirline.nvim:

local CodeCompanion = {
  static = {
    processing = false,
  },
  update = {
    "User",
    pattern = "CodeCompanionRequest*",
    callback = function(self, args)
      if args.match == "CodeCompanionRequestStarted" then
        self.processing = true
      elseif args.match == "CodeCompanionRequestFinished" then
        self.processing = false
      end
      vim.cmd("redrawstatus")
    end,
  },
  {
    condition = function(self)
      return self.processing
    end,
    provider = " ",
    hl = { fg = "yellow" },
  },
}

Legendary.nvim

The plugin also supports the amazing legendary.nvim plugin. Simply enable it in your config:

require('legendary').setup({
  extensions = {
    codecompanion = true,
  },
})

Mini.Diff

If you're using mini.diff you can put an icon in the statusline to indicate which diff is currently in use in a buffer:

local function diff_source()
  local bufnr, diff_source, icon
  bufnr = vim.api.nvim_get_current_buf()
  diff_source = vim.b[bufnr].diffCompGit
  if not diff_source then
    return ""
  end
  if diff_source == "git" then
    icon = " "
  elseif diff_source == "codecompanion" then
    icon = " "
  end
  return string.format("%%#StatusLineLSP#%s", icon)
end

🧰 Troubleshooting

Before raising an issue, there are a number of steps you can take to troubleshoot a problem:

Checkhealth

Run :checkhealth codecompanion and check all dependencies are installed correctly. Also take note of the log file path.

Turn on logging

Update your config and turn debug logging on:

opts = {
  log_level = "DEBUG", -- or "TRACE"
}

and inspect the log file as per the location from the checkhealth command.

🎁 Contributing

I am open to contributions but they will be implemented at my discretion. Feel free to open up a discussion before embarking on a PR and please read the CONTRIBUTING.md guide.

πŸ‘ Acknowledgements

  • Steven Arcangeli for his genius creation of the chat buffer and his feedback early on
  • Manoel Campos for the xml2lua library that's used in the tools implementation
  • Dante.nvim for the beautifully simple diff implementation
  • Wtf.nvim for the LSP assistant action
  • CopilotChat.nvim for the rendering and usability of the chat
    buffer
  • Aerial.nvim for the Tree-sitter parsing which inspired the symbols Slash
    Command
Package Rankings
Top 6.5% on Proxy.golang.org
Related Projects