Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctrl+C (<C-c>) does not clear virtual text #272

Open
stefanpartheym opened this issue Dec 19, 2024 · 1 comment
Open

Ctrl+C (<C-c>) does not clear virtual text #272

stefanpartheym opened this issue Dec 19, 2024 · 1 comment

Comments

@stefanpartheym
Copy link

Hey guys,

I mainly use C-c (or Ctrl+C) to switch from insert mode back to normal mode rather than Esc.
This causes an annoying behaviour with the virtual text: codeium.nvim does not clear the virtual text when hitting C-c. This leaves the suggsted virtual text in the buffer while being in normal mode and only disappears again when switching into insert mode and hitting Esc.

Is there a way to make codeium.nvim also clear virtual text when hitting C-c?

Thanks!

@stefanpartheym
Copy link
Author

stefanpartheym commented Dec 19, 2024

Ok, this is not a bug, or an issue with codeium.nvim but rather how C-c is handled by neovim.
This explains why the virtual text is not cleared on C-c (see neovim docs):

InsertLeave			Just after leaving Insert mode.  Also when
				using CTRL-O i_CTRL-O.  But not for i_CTRL-C.

InsertLeave is used in codeium.nvim to clear the virtual text when leaving insert mode.

Basically, there are two ways to fix this:

  1. Remap C-c to Esc (easy, but might not be what you want)
  2. Trigger a custom Autocmd-Event on C-c (more sophisticated, but Ecs and C-c stay different (as it is intended))

Remap C-c to Esc

map("i", "<C-c>", "<Esc>", { desc = "Map Ctrl+C to Esc" })

Trigger a custom Autocmd-Event on C-c

return {
  "Exafunction/codeium.nvim",
  dependencies = {
    "nvim-lua/plenary.nvim",
    "hrsh7th/nvim-cmp",
  },
  config = function()
    local virtual_text = require("codeium.virtual_text")
    -- Get codeium's augroup
    local augroup = vim.api.nvim_create_augroup("codeium_virtual_text", { clear = false })
    -- Hook into User.InsertLeaveCtrlC to clear virtual text.
    vim.api.nvim_create_autocmd("User", {
      group = augroup,
      pattern = "InsertLeaveCtrlC",
      callback = function()
        virtual_text.clear()
      end,
    })
    -- Trigger autocmd `User.InsertLeaveCtrlC` when <C-c> is pressed
    vim.api.nvim_set_keymap(
      "i",
      "<C-c>",
      [[<Esc>:doautocmd User InsertLeaveCtrlC<CR>]],
      { noremap = true, silent = true }
    )

    require("codeium").setup({
      virtual_text = {
        enabled = true,
        -- ...
        },
      },
    })
  end,
}

Conclusion

I bet I'm not the only one using C-c excessively, so you may want to add this to the docs, so users are not confused as much.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant