The Best AI Plugin for Neovim

Alvaro LealAlvaro Leal
7 min read

As a Neovim user, you probably know the value of a great code completion plugin. AI-powered code assistants can significantly boost your productivity by offering context-aware suggestions, autocompletion, and even refactoring recommendations. I recently explored several AI plugins for Neovim, and while there are quite a few interesting options, I found one that clearly stood out: Codeium.

Let me walk you through my evaluation process and explain why Codeium is the best AI plugin for Neovim compared to other popular options like the ChatGPT plugin and GitHub Copilot.

Evaluating AI Plugins for Neovim

When evaluating AI plugins for Neovim, I focused on several key factors:

  • Cost: Is the plugin free, or does it require a subscription?

  • Ease of use: How smoothly does the plugin integrate with Neovim's workflow?

  • Flexibility: Can you iterate through different suggestions?

  • Compatibility: Does the plugin play nicely with other Neovim features, like the built-in Language Server Protocol (LSP) autocompletion?

The Contenders: ChatGPT, GitHub Copilot, and Codeium

1. ChatGPT Plugin

The ChatGPT plugin is an interesting option that integrates OpenAI's powerful language model directly into Neovim. However, there is a significant downside: it requires an OpenAI API key, which isn't free. The cost can quickly add up, especially if you're a heavy user. Additionally, while the ChatGPT plugin offers impressive suggestions, it lacks some of the seamless integration that other plugins provide, especially when it comes to iterating through multiple suggestions in real-time.

2. GitHub Copilot

GitHub Copilot is another popular choice that uses OpenAI's Codex model to provide code suggestions directly in your editor. However, Copilot also comes with a cost. It requires a subscription, and while it offers powerful autocompletion, there are a few drawbacks:

  • Lack of Iteration Control: Copilot's completion system does not provide an easy way to iterate over different suggestions. You get one suggestion at a time, and if you don't like it, you must hit undo or try triggering it again.

  • Proprietary Nature: Copilot's integration is somewhat closed, and you have less flexibility in terms of configuration and customization.

Overall, while Copilot is a strong tool, its subscription requirement and limited control over suggestions make it less appealing for Neovim users who want a free, flexible solution.

The Winner: Codeium

After testing the options, I found that Codeium was the clear winner among AI plugins for Neovim. Here's why:

1. It's Free!

First and foremost, Codeium is completely free. You don't need to buy an API key or pay for a subscription to use it. This is a huge advantage over both the ChatGPT plugin and GitHub Copilot, making it a much more accessible option for everyone, from hobbyists to professional developers.

2. Flexible Suggestions

Codeium allows you to iterate over multiple suggestions seamlessly. Unlike GitHub Copilot, which only shows one suggestion at a time, Codeium provides a list of suggestions that you can quickly navigate through. This makes it much easier to find the best suggestion for your code without disrupting your flow.

3. Integration with Neovim's Ghost Text

Codeium makes use of Neovim's ghost text feature, which allows code suggestions to appear in a subtle, non-intrusive manner. This is a big deal because it means that Codeium's suggestions do not collide with the LSP (Language Server Protocol) autocompletion menu. Instead, they work harmoniously together, allowing you to enjoy both Codeium's AI-powered suggestions and Neovim's native LSP completions without any conflicts.

4. Easy to Install and Configure

Codeium is also straightforward to install and configure in Neovim. Whether you're using packer.nvim, vim-plug, or another plugin manager, the setup process is simple, and the configuration options are flexible enough to suit different workflows.

How to Get Started with Codeium in Neovim

If you're ready to try Codeium, here's a quick guide to get you started:

  1. Install Codeium: Add the following line to your plugin manager configuration. For example, if you use lazy.nvim, create a plugin file for Codeium (e.g.: lua/plugins/codeium.lua):

     local Plugin = {'Exafunction/codeium.vim'}
     -- Call :Codeium Auth after installation to get Token ID
    
  2. Enable the plugin on demand: I follow a minimalistic approach when it comes to Neovim plugins, as you can see in my book about Neovim, to avoid cluttering Neovim with too many plugins. That's why I've defined a key mapping to load the Codeium plugin only when you toggle it:

     Plugin.cmd = {'CodeiumToggle'}
    
     -- Toggle Codeium
     vim.keymap.set('n', '<leader><CR>', ':CodeiumToggle<CR>')
    
     function Plugin.config()
       -- Disabled by default
       vim.g.codeium_enabled = false
    
  3. Setup Codeium Chat: Codeium also provides a prompt through their web site, that you can use similarly to ChatGPT for elaborated questions, like creating a unit test for a function.

    As Codeium Chat is exposed by a function in this plugin, I prefer to create a user command to call it, so that it is easier to start the chat:

     function Plugin.config()
       -- 
    
       -- Codeium Chat
       vim.api.nvim_create_user_command('CodeiumChat', function(opts)
         vim.api.nvim_call_function("codeium#Chat", {})
       end,
       {})
    
  4. Key mappings: the main actions that you need when you are interacting with Codeium are iterating through suggestions, accept a suggestion and clean the suggestions. I use three key mappings that allow you to access these functionalities in a comfortable way while you are typing. But you could choose whatever key mappings that suits you better:

       -- Key bindings
       vim.g.codeium_no_map_tab = true
       vim.keymap.set('i', '<C-l>', function () return vim.fn['codeium#Accept']() end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-j>', function() return vim.fn['codeium#CycleCompletions'](1) end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-k>', function() return vim.fn['codeium#CycleCompletions'](-1) end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-d>', function() return vim.fn['codeium#Clear']() end, { expr = true, silent = true })
    
  5. Status line: One of the things that I like the most about the Codeium plugin is its ability to report information on the status line. That way you can see whether the Codeium plugin is activated or not. The total number of suggestions provided by Codeium. And the current suggestion that is being displayed.

     -- Function to wrap the codeium#GetStatusString Vimscript function
     function get_codeium_status()
       return vim.fn['codeium#GetStatusString']()
     end
    
     function Plugin.config()
       -- 
       -- Add Codeium status to the statusline
       vim.o.statusline = table.concat({
         "%f",                       -- Full file path
         " %h",                      -- Help flag
         " %m",                      -- Modified flag
         " %r",                      -- Readonly flag
         "%=",                       -- Right aligned
         " %y ",                     -- File type
         "%{&ff} ",                  -- File format
         " %p%%",                    -- File position percentage
         " %l:%c ",                  -- Line and column number
         " [Codeium:%{v:lua.get_codeium_status()}]", -- Codeium status
       })
     end
    
  6. Complete configuration: putting all these pieces together we end up having this configuration for the Codeium plugin:

     local Plugin = {'Exafunction/codeium.vim'}
     -- Call :Codeium Auth after installation to get Token ID
    
     Plugin.cmd = {'CodeiumToggle'}
    
     -- Toggle Codeium
     vim.keymap.set('n', '<leader><CR>', ':CodeiumToggle<CR>')
    
     -- Function to wrap the codeium#GetStatusString Vimscript function
     function get_codeium_status()
       return vim.fn['codeium#GetStatusString']()
     end
    
     function Plugin.config()
       -- Disabled by default
       vim.g.codeium_enabled = false
    
       -- Codeium Chat
       vim.api.nvim_create_user_command('CodeiumChat', function(opts)
         vim.api.nvim_call_function("codeium#Chat", {})
       end,
       {})
    
       -- Key bindings
       vim.g.codeium_no_map_tab = true
       vim.keymap.set('i', '<C-l>', function () return vim.fn['codeium#Accept']() end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-j>', function() return vim.fn['codeium#CycleCompletions'](1) end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-k>', function() return vim.fn['codeium#CycleCompletions'](-1) end, { expr = true, silent = true })
       vim.keymap.set('i', '<C-d>', function() return vim.fn['codeium#Clear']() end, { expr = true, silent = true })
    
       -- Add Codeium status to the statusline
       vim.o.statusline = table.concat({
         "%f",                       -- Full file path
         " %h",                      -- Help flag
         " %m",                      -- Modified flag
         " %r",                      -- Readonly flag
         "%=",                       -- Right aligned
         " %y ",                     -- File type
         "%{&ff} ",                  -- File format
         " %p%%",                    -- File position percentage
         " %l:%c ",                  -- Line and column number
         " [Codeium:%{v:lua.get_codeium_status()}]", -- Codeium status
       })
     end
    
     return Plugin
    

Conclusion

While there are several AI plugins available for Neovim, Codeium stands out as the best option. It's free, flexible, and integrates perfectly with Neovim's existing autocompletion features. Unlike other alternatives, such as the ChatGPT plugin or GitHub Copilot, Codeium doesn't require a subscription or API key, and it gives you full control over the suggestions it provides.

So, if you're looking for an AI-powered code assistant for Neovim, give Codeium a try—you won't be disappointed!

Happy coding!

0
Subscribe to my newsletter

Read articles from Alvaro Leal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alvaro Leal
Alvaro Leal

Hi, I'm Alvaro. I'm a software engineer working in the space industry. I've worked on many science missions for the European Space Agency like Bepi Colombo, Exomars and Juice. As well as on several satellite telecom projects. Currently I'm working on the Galileo Spacecraft Constellation Control Facility. I have been working on the Galileo program since 2018. It is a global navigation satellite system (GNSS) developed by the European Union (EU) and the European Space Agency (ESA). Its purpose is to provide accurate positioning, navigation, and timing information to users worldwide. In this site I publish articles about programming and software development.