Chapter 16. Configuring Artificial Intelligence

This book was initially written in 2024, and I predict this year will be the one in which the world goes from making up crazy predictions about AI to it being part of our daily lives. It will be fun to revisit this statement if I get to publish a second edition. I know we’re all sick to death of hearing about the future of AI, so this chapter is entirely about the present of AI.

Unfortunately, the present of AI is a bit of a mess. We don’t yet know which of the various AI tools will become dominant. So this content will quickly become out of date. Hopefully the techniques and lessons it describes will be applicable to whatever the next generation of AI coding platforms will be.

LazyVim has excellent support for several AI coding tools. You’ll probably need to try each of them and figure out which works best for you, and “best” is probably going to change many times over the coming months (it reminds me of the browser wars at the turn of the century).

At the time of writing, GitHub Copilot is probably the best to get started. It has a free tier and the Copilot chat plugin is a nice experience. Most importantly, if your organization already uses GitHub, Copilot is the most likely to have already been vetted by your security team. Do not allow your editor to send intellectual property to arbitray AIs!

16.1. Basic Anatomy

Most AI-coding tools integrate with blink.cmp, the plugin LazyVim uses for completions and snippets, and this is the simplest way to get started with them.

Some plugins also have a “chat” feature that allows you to communicate with the AI in a more interactive way. In my experience, these features don’t work very well right now, with one exception that we will cover.

16.2. Codeium

If you’ve never used AI for coding before, you may be skeptical as to whether it’s worth paying for. In fact, I use it extensively, and I’m still skeptical! Codeium is a great place to start because the free “Individual” tier is “smart enough to be useful.”

It’s also faster and more responsive, and when it comes to waiting for completion menus to pop up, inaccurate but fast is often more useful than accurate but slow.

To get started, you will need an account on codeium.com.

Enabling Codeium in LazyVim is dead simple: just open the :LazyExtras command and find coding.codeium. Hit x to install it as usual and restart Neovim.

You should be presented with a window to authenticate Codeium automatically. If not, type the command :Codeium Auth. You will see a simple menu:

codeium auth dark
Figure 90. Codeium Auth

Select the option most appropriate for you to open a URL in a web browser. The default will work for most people, but if you are running Neovim over SSH or in a docker container you might need to get more creative.

Once logged in, you’ll see a page with a token in it and instructions to copy it to your clipboard. Do so, then paste it in the Token box waiting in Neovim. If you’ve lost this box, you can find it again by typing :Codeium Auth and selecting “I already have a key”.

You may want to restart your editor again. Now, while you are coding, you’ll get Codeium entries in your completion menu:

codeium cmp dark
Figure 91. Codeium Cmp

As you can see by the preview to the right, the completions may be multi-line. Sometimes, they are accurate and useful. Other times, not so much. (I once tried to get ChatGPT to tell me what percentage of the time it is accurate and it flat out refused to give me a number).

As with everything AI, don’t rely on the answers, but it can save you a few keystrokes every once in a while, and that’s all any Vim user really wants in life.

16.2.1. Codeium Chat

Codeium has a chat feature that you can access through their web UI. I was excited to hear that an “in-editor” chat experience was recently added to Codeium.nvim but all it does is open a web browser to the chat window. The Codeium chat is (at time of writing) not terribly sophisticated and I find it nearly as frustrating to use as Eliza, so my recommendation is: don’t bother.

16.3. GitHub Copilot

At time of writing, GitHub Copilot is considered the current gold standard for AI-driven coding. From an enterprise perspective, its primary advantage is that if you already use GitHub, your security team has already vetted it. From a developer perspective, I find it a bit slow, but more accurate than other AI tools I’ve tried.

First, sign up for an account on GitHub. It’s free for 30 days so you can decide if it’s worth it before submitting an expense to your employer.

There are several different LazyVim plugins that support Copilot. The easiest is to use the :LazyExtras command and enable coding.copilot. This enables the zbirenbaum/copilot.lua plugin, which creates blink.cmp completions similar to those that the codeium plugin provides.

Similar to Codeium, type the command :Copilot auth to enable your account and follow the prompts. You’ll be given a code to type into a browser window on GitHub.

After that, you should see Copilot completions in your blink.cmp popups just like you did with Codeium. You can use both at the same time, but be aware that it’s hard on the environment and your network connection!

16.3.1. Copilot Chat

The copilot.lua plugin provides completions and little else. However, there is a second LazyVim extra you can enable that I really enjoy: coding.copilot-chat.

Restart Neovim, and if everything is set up correctly, you should be able to use the keybinding <Space>aa to open a chat session (where a opens the “ai” menu). This will be a normal Neovim buffer that you can append text to in Insert mode. There are several keyboard shortcuts available to aid your chatting, summarized right in the chat window:

copilot chat help dark
Figure 92. Copilot Chat Help Menu

In normal usage, you will be editing a file and then realize you want to send some subset of the file to Copilot to have it rewrite it. Select the code in Visual mode and hit <Space>aa. This will store the selected code and show the copilot chat window.

Input a question about the code you had selected and hit either Control-s (s for “send”) (in Insert or Normal mode), or <Enter> while in Normal mode to submit it. The plugin will add the code to your query automatically. Then wait (patiently) for a response. The response will contain the typical AI drivel describing what it did, and, most likely, some code.

If the code does what you want, simply hit Control-y (y for “yes”) to accept it, replacing whatever you had selected with the AI text. If not, enter Insert mode and post a follow-up question to guide the AI to what you were actually looking for.

If the chat is getting out of control, as often happens with generative AI, hit Control-l to reset it. This is a safer course of action than typing a bunch of curses to the AI. Nowadays, it just becomes embarrassingly over-conciliatory, but you can be sure they have long memories, and you don’t want to be the person who cussed out the AI when it becomes your boss!

The Extra configures a few other handy keybindings for interacting with copilot-chat, all of them in the <Space>a submode. The “prompt” menu with <Space>ap is particularly helpful if you want to instruct Copilot to do something specific like write a test or commit message. You can also use <Space>ad while a diagnostic is displayed to get more information about that diagnostic.

You can even change the model you use with CopilotChat! At time of writing, Claude seems to be the least unhelpful coding assistant. To switch to Claude, or one of several other models, enter the :CopilotChatModels command and select the model you prefer.

16.4. TabNine

The advantage of TabNine is that it can run locally on your device, a very important feature for certain enterprise situations or anywhere that intellectual property matters (I wouldn’t be surprised if AI destroys the concept of intellectual property once and for all. The advantages of a hive-mind may well outweigh the long-term advantages of corporate ownership).

You can configure Tabnine in Neovim similarly to Codeium and Copilot. LazyVim ships with support to integrate TabNine with blink.cmp to get completion sources. You can enable it with the :LazyExtras command and x on the coding.tabnine line.

Restart Neovim. You’ll probably have to wait a while for the plugin to download and install TabNine, but it should all be taken care of for you. Once the plugin is installed, run :CmpTabnineHub to authenticate yourself, then restart Neovim one more time.

Now you should see TabNine completions in your list. I find they are virtually useless on the basic plan.

TabNine also has a newish official Neovim plugin named tabnine-nvim. It even has a chat mode, but this mode requires running a separate binary to show the chat window alongside your editor. This is obviously problematic if you want to use Neovim inside ssh or docker sessions, and installing it requires setting up the entire Rust and GTK toolchain, so it is beyond the scope of this book.

16.5. Sourcegraph Cody

Cody theoretically has an advantage over some of the other AI tools because it has access to Sourcegraph’s insights into the structure of your codebase. They recently updated to Claude V3 for their chats, so conversations with it are fairly natural.

Unlike the other mentioned tools, there is currently no LazyVim Extra for Sourcegraph or Cody. Instead, you’ll need to install the sg.nvim plugin manually:

Listing 58. Source Graph Cmp Configuration
return {
  {
    "saghen/blink.cmp",
    dependencies = { "sourcegraph/sg.nvim" },
    opts = function(_, opts)
      table.insert(opts.sources, 1, { name = "cody" })
    end,
  },
  {
    "sourcegraph/sg.nvim",
    dependencies = {
      "nvim-lua/plenary.nvim",
    },
    opts = {},
  },
}

Sign up for an account over at https://sourcegraph.com/ and then run the :SourcegraphLogin command. Note that the g is not capitalized; I thought my plugin wasn’t installed correctly and spent half an hour debugging that typo.

This plugin is more than just an AI completion tool; it also gives you access to commands to browse your code using Sourcegraph. However, they depend on the Telescope picker instead of Fzf.lua. You can replace Fzf.lua with Telescope in the Lazy Extras, but I only recommend going that far if you really love Sourcegraph. The product has some pretty cool features, but those are beyond the scope of this book. You can learn more about available features by reading through :help sg.nvim.

If things aren’t working correctly, use :checkhealth sg to help understand what went wrong.

With the above configuration, blink.cmp completions should work similarly to the other tools. To interact with the Cody chat assistant, use the command :CodyAsk with a prompt. For example, select some code and type :CodyAsk what does this do?

A window pops up in front of your code that looks like a chat interface, with some keybinding tips on the right side:

cody ask dark
Figure 93. Cody Ask Chat

Other chat commands you may want to read about in the help files include :CodyChat!, :CodyTask, :CodyToggle, :CodyExplain, and :CodyRestart.

16.6. Supermaven AI

Supermave claims to have the fastest response times of the various AI coding assistants. It has the advantage of being able to use the free plan without setting up an account, but if you sign up for a paid account, you’ll supposedly get better results.

Enabling the plugin is as easy as the other tools. Simply hit :LazyExtras, search for ai.supermaven and hit x. Restart Neovim and give it a few moments to download the Supermaven binary behind your back.

You should automatically see an activation link that looks something like this:

supermaven activation url dark
Figure 94. Supermaven Activation Link

If you want to use the pro plan, click (if your terminal supports clicking links) or copy-paste the link to your browser and follow instructions. If you want the free version, close the popup window with :q and then enter the :SupermavenUseFree command.

Now you should see Supermaven completions in your blink.cmp popup as you type.

Supermaven doesn’t have a chat interface. There are a handful of commands for things like restarting Supermaven if you type :Supermaven<Tab>.

If you have tried the free plan and decided to upgrade to pro, you may be confused! You’ll need to use :SupermavenLogout followed by :SupermavenUsePro to get the activation link again.

16.7. What About ChatGPT?

ChatGPT is a good coding assistant, and there are several plugins that integrate nicely with the editor. However, none of them are available as LazyExtras. They also all require an OpenAI API key, which tends to be a lot more expensive than opening ChatGPT in the browser and asking it questions on the paid plan, then copying text over in the most awful way possible.

For the most part, I think GitHub Copilot is as smart as ChatGPT (about coding, anyway), and the CopilotChat plugin is a nice interface, so I recommend that instead.

If you want to explore integrating ChatGPT, Claude, Gemini, or several other AI engines into Neovim, check out the parrot.nvim plugin. Configuration is similar to the other tools above.

16.8. Summary

This chapter was all about AI. AI is a weirdly simple topic, considering the complexity it abstracts away. Under the hood, LLMs are really cool, but the interface to interacting with them is typically just a simple HTTP request to some API somewhere (an API that is so useful we collectively don’t think hard enough about how much we trust it).

Various Neovim plugins have created interfaces to these APIs. LazyVim makes it easy to integrate many of them with blink.cmp, and other services are covered with third-party plugins.

Choosing between the various options is the hard part. None of them work quite as well as I would like, and I find that the amount of time they save is about equal to the amount of time they cost when they get things wrong. I currently use Copilot because my employer pays for it, but I have disabled it before and not missed it.

Next up, we’ll discuss running debuggers from inside LazyVim.

Reminder

This book is freely available. It contains a textbook's worth of material. If you are deriving value from this content, I really appreciate any support you can give (both to me, and to those who can't afford to give it) by purchasing a copy of the book or through donation. Click here to see various purchase and donation options.

Copyright © 2024 Dusty Phillips