Skip to content

[Bug]: Selected text was pasted twice when using the CodeCompanionChat Add hotkey #2650

@reybits

Description

@reybits

Pre-submission checklist

  • I have read the documentation
  • I have updated the plugin and all dependencies to the latest versions
  • I have searched for existing issues and discussions
  • My issue is not a minor or cosmetic quirk (e.g., formatting, spacing, or other non-functional details)

Neovim version (nvim -v)

v0.11.5

Operating system/version

macOS 26.2 (25C56)

Adapter and model

copilot

Describe the bug

The bug occurs even with a minimal.lua configuration that only adds keymaps for normal and visual selection modes.

Variant 1:

keys = {
    { "<leader>aa", "<cmd>CodeCompanionChat Toggle<cr>", mode = "n", desc = "Toggle Chat" },
    { "<leader>aa", "<cmd>CodeCompanionChat Add<cr>", mode = "x", desc = "Add Selection to Chat" },
},

Variant 2:

config = function()
    require("codecompanion").setup(opts)
    vim.keymap.set("n", "<leader>aa", "<cmd>CodeCompanionChat Toggle<cr>", { desc = "Toggle Chat" })
    vim.keymap.set("x", "<leader>aa", "<cmd>CodeCompanionChat Add<cr>", { desc = "Add Selection to Chat" })
end

The bug appears in both cases.

Problem:

If there are no chats currently existing (neither an opened chat window nor a hidden chat), the selected text from the buffer is sent twice when using the visual selection key map.
This is similar to the issue described in PR #2402.

Note:

If command line is used instead of <space>aa, then everything works correctly:

:'<,'>CodeCompanionChat Add

Steps to reproduce

  1. Edit minimal.lua as described above, or get mine provided below.
  2. Select any text and press <space>aa.

Here are the results - two code blocks and the first one without a newline character:

## Me


```lua
vim.g.mapleader = " "
vim.g.maplocalleader = " "
```Here is some code from /Users/andrey/configs/config-nvim/tmp/minimal.lua:

```lua
vim.g.mapleader = " "
vim.g.maplocalleader = " "
```

Expected behavior

I expect the selected text to be pasted once.

## Me


Here is some code from /Users/andrey/configs/config-nvim/tmp/minimal.lua:

```lua
vim.g.mapleader = " "
vim.g.maplocalleader = " "
```

Screenshots or recordings (optional)

No response

minimal.lua file

---@diagnostic disable: missing-fields

--[[
NOTE: Set the config path to enable the copilot adapter to work.
It will search the following paths for a token:
  - "$CODECOMPANION_TOKEN_PATH/github-copilot/hosts.json"
  - "$CODECOMPANION_TOKEN_PATH/github-copilot/apps.json"
--]]
vim.env["CODECOMPANION_TOKEN_PATH"] = vim.fn.expand("~/.config")

vim.g.mapleader = " "
vim.g.maplocalleader = " "

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

-- Your CodeCompanion setup
local plugins = {
  {
    "olimorris/codecompanion.nvim",
    dependencies = {
      { "nvim-lua/plenary.nvim" },
      {
        "nvim-treesitter/nvim-treesitter",
        lazy = false,
        build = ":TSUpdate",
      },
    },
    opts = {
      --Refer to: https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua
      interactions = {
        --NOTE: Change the adapter as required
        chat = { adapter = "copilot" },
        inline = { adapter = "copilot" },
      },
      opts = {
        log_level = "DEBUG",
      },
    },
    config = function(opts)
	    require("codecompanion").setup(opts)
       vim.keymap.set("n", "<leader>aa", "<cmd>CodeCompanionChat Toggle<cr>", { desc = "Toggle Chat" })
       vim.keymap.set("x", "<leader>aa", "<cmd>CodeCompanionChat Add<cr>", { desc = "Add Selection to Chat" })
    end
  },
}

-- Leaving this comment in to see if the issue author notices ;-)
-- This is so I can tell if they've really tested with their own minimal.lua file

require("lazy.minit").repro({ spec = plugins })

-- CONFIGURE PLUGINS HERE -----------------------------------------------------

-- Setup Tree-sitter
-- NOTE: Please restart Neovim to ensure parsers are loaded correctly
require("nvim-treesitter")
  .install({
    "lua",
    "markdown",
    "markdown_inline",
    "yaml",
  }, { summary = true, max_jobs = 10 })
  :wait(1800000)

Log output (optional)

Nothing unusual.

[DEBUG] 2026-01-11 22:02:38
[Subscription] Stopping

Minimal reproduction confirmation

  • Yes, I have tested and provided a minimal.lua file that reproduces the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededreviewed-by-AIThe CodeCompanion agent reviewed this PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions