How to Set Up AI-Powered Coding on Neovim

Jun Takemura · November 13, 2024

I’ve manually coded for a while, not even using IDE, due to two major reasons. First my focus was pentesting so I didn’t write much code besides occasional scripting. Second, I thought relying on auto suggestions might hinder my learning experience. However I’ve started to write more code and also I think what matters the most is actually understanding the code. Even if I don’t use AI if I just copy and paste without thinking it doesn’t help me learn. At first I thought of using some kind of LSP (Language Server Protocol) but it occured to me now’s the good time to try AI.

In this article, I’ll walk you through setting up an AI-powered coding environment in Neovim. We’ll use lazy.nvim, a modern plugin manager for Neovim, and NeoCodeium for AI-based code completions, similar to GitHub Copilot but with free options.

Install latest Neovim

If you’re using Neovim 0.8.0 or higher, you can skip this step.

Here’s a quick way to do it using an AppImage:

wget https://github.com/neovim/neovim/releases/latest/download/nvim.appimage -O nvim
chmod u+x nvim
sudo mv nvim /usr/local/bin/

After installing, verify the version:

nvim --version

Set Up lazy.nvim

lazy.nvim is a plugin manager for Neovim that only loads plugins when needed, which keeps startup times fast.

Install lazy.nvim by cloning it to Neovim’s plugin directory:

git clone https://github.com/folke/lazy.nvim.git ~/.local/share/nvim/lazy/lazy.nvim

Configure lazy.nvim: Open Neovim’s configuration file and set it up. Create or open ~/.config/nvim/init.lua:

nvim ~/.config/nvim/init.lua

Add this code to load lazy.nvim:

vim.opt.rtp:prepend("~/.local/share/nvim/lazy/lazy.nvim")

You can also use vim script with init.vim though lua is faster and more powerful.

Install NeoCodeium for AI-Powered code completions

Add NeoCodeium to lazy.nvim:

require("lazy").setup({
    {
        "monkoose/neocodeium",
        event = "VeryLazy",
        config = function()
            local neocodeium = require("neocodeium")
            neocodeium.setup()
            vim.keymap.set("i", "<C-f>", neocodeium.accept)
        end
    }
})

This configuration tells lazy.nvim to load NeoCodeium only when you’re editing (making it “very lazy”) and sets up a keybinding () to accept AI suggestions in insert mode. You can change the key binding to wanted if you prefer. For example, for Alt-f.

Just run Neovim and lazy.nvim will detect and install NeoCodeium automatically.

Twitter, Facebook