![codeium auth dark](/images/book/chapter-16/codeium-auth-dark.png)
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, if you are willing to pay for it, GitHub Copilot is probably the best, and if you just want to get started with a free tool, Codeium easily has the most capable AI in their free tier.
16.1. Basic Anatomy
Most AI-coding tools integrate with nvim-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](/images/book/chapter-16/codeium-auth-dark.png)
Figure 86. 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](/images/book/chapter-16/codeium-cmp-dark.png)
Figure 87. 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
GitHub Copilot is considered the current (2024) 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.
I think Copilot justifies its cost, although it sure doesn’t make me 55% faster like the GitHub marketing page says it does. As an unemployed author, I recently cancelled my subscription and don’t miss it, so… your mileage may vary.
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 nvim-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 nvim-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](/images/book/chapter-16/copilot-chat-help-dark.png)
Figure 88. 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.
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 nvim-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 53. Source Graph Cmp Configuration
return {
{
"nvim-cmp",
dependencies = { "sourcegraph/sg.nvim" },
opts = function(_, opts)
table.insert(opts.sources, 1, { name = "cody" })
end,
},
{
"sourcegraph/sg.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim"
},
opts = {},
},
}
You can leave the Telescope dependency off if you prefer Fzf.lua, but SourceGraph is not aware of other pickers so some features will not work.
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. For example, the
SourcegraphSearch
command will pop up the Telescope picker if it is
installed. You may prefer it over the ripgrep-based search feature we discussed
in Chapter 12. The product is actually capable of much more than that, and can
even dive into code in dependencies hosted on GitHub. 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, nvim-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](/images/book/chapter-16/cody-ask-dark.png)
Figure 89. 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. What About ChatGPT?
ChatGPT is a good coding assistant, and there are several plugins that integrate nicely with the editor. They 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.7. 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 POST 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 nvim-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, yet all of them work too well for me to dismiss the idea entirely.
Next up, we’ll discuss running debuggers from inside LazyVim.