MA

· Muhammad Azamuddin

Setting up Github Copilot for Neovim NVchad


The reason I use Neovim (or Vim) is because I want to speed up my coding operation. Vim has allow me to convert my thinking into code quickly due to the modal editing.

I can literally, jump to any place and perform editing almost instantly as I’m thinking, I do not need to use mouse or do some extra steps do execute what is on my mind.

And then comes Github Copilot, a tiny tool that uses AI that promises to help programmers (not to replace them, at least for now).

I was reluctlant to give Copilot at first, but since the release of ChatGPT and they joins Microsoft, I’m beginning to change my mind.

I think Copilot might be the next step of “speeding up”, just like why I use Neovim in the first place.

Neovim combined with auto completion from Github Copilot should be magical, that’s what I think. That’s why I’ll give it a try.

I setup payment, install Github copilot plugin, and it should be good to go.

But it it’s not because there’s no configuration that comes with this plugin. So, I have options:

  1. change my <Tab> mapping to Github copilot, which means I need to use other keys for nvim-cmp auto completion.
  2. find a workaround to be able to remap the copilot accept function.

I first tried option 1 since it is the easiest one. But I do not find it comfortable because I’m using Moonlander Mark I keyboard, which I need to change layer to use arrow key for completion. Not so pleasant, tab tab is the way to go to pick item from autocompletion.

That means I have to try option 2, which I did, but I spent whole day trying to make this work. So many quirks and weird things. So I searched in github issue, reddit, until I finally found the solution.

This is why I want to write about it, so that people do not waste time like me.

After you install github/copilot.vim you need to configure in the your custom lua/custom/plugins.lua like this:

  {
    "github/copilot.vim",
    lazy = false,
    config = function()
      -- Mapping tab is already used by NvChad
      vim.g.copilot_no_tab_map = true;
      vim.g.copilot_assume_mapped = true;
      vim.g.copilot_tab_fallback = "";
      -- The mapping is set to other key, see custom/lua/mappings
      -- or run <leader>ch to see copilot mapping section
    end
  },

That code will make copilot doesn’t complain that the <Tab> is already used.

Then, we need to add the mapping in the lua/custom/mappings.lua

M.copilot = {
  i = {
    ["<C-l>"] = {
      function()
        vim.fn.feedkeys(vim.fn['copilot#Accept'](), '')
      end,
      "Copilot Accept",
      {replace_keycodes = true, nowait=true, silent=true, expr=true, noremap=true}
    }
  }
}

Seems easy and straightforward? Yeah, I hope you find it straightforward and do not waste so much hours like me trying to figure this out.

COMMENTS