Skip to content

How-ToGPU

How to Use Neovim with Ollama: Set Up CodeCompanion for Private AI Coding

Set up CodeCompanion.nvim with Ollama for private AI coding in Neovim. We cover picking a tool-calling model, installing the plugin, pointing it at Ollama, chatting about your code, inline edits with diff review and tuning context and memory.

By , Systems & Technical Editor

6 min read

How to Use Neovim with Ollama: Set Up CodeCompanion for Private AI Coding

If you live in Neovim and want an AI assistant that never sends your code to the cloud, the simplest robust route is CodeCompanion.nvim talking to Ollama on your own PC. You get a chat buffer that can see your code and LSP errors, plus inline edits you review as a diff before they land. This guide covers the setup from a working Neovim install to your first local edit.

What you need

  • Neovim 0.12 or later. CodeCompanion's installation page lists 0.12 as the minimum. Run nvim --version to check; Neovim 0.12.7 was the latest release on GitHub when we checked.

  • curl, which CodeCompanion uses to talk to Ollama.

  • A plugin manager. The examples here use lazy.nvim; CodeCompanion's docs also give examples for Neovim's built-in vim.pack and Packer.

  • Ollama, installed and running. Our guides cover installing Ollama on Linux and running models locally on Windows.

  • Enough VRAM for a capable model. Local models are limited by your hardware, and CodeCompanion's tools need a model that supports tool calling. See how much VRAM you need, or our guide to running an LLM on an 8GB card.

Step 1: Pull a model that supports tools

CodeCompanion's own local-models guide uses Qwen3 8B as its example:

ollama pull qwen3:8b

Then check it supports tool calling:

ollama show qwen3:8b

Look for tools under the model's capabilities. This matters more than it might seem: CodeCompanion's inline mode edits your buffer by calling a tool, and file tools such as @{files} only work with tool-calling models. Its docs list qwen3, qwen3-coder, gpt-oss, devstral and llama3.1 among models that report tools, but warn that smaller models often call the wrong tool or stop halfway, and suggest 20B parameters or more as a sensible starting point for its @{agent} mode. If you're torn between two coding models, our Devstral Small 2 vs Qwen3 Coder comparison may help.

Step 2: Install CodeCompanion

Add the plugin to your lazy.nvim spec, pinned to a release as the developer recommends to avoid breaking changes:

{
  "olimorris/codecompanion.nvim",
  version = "^19.0.0",
  opts = {},
  dependencies = {
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
},

The docs warn that if you pin your plugins to releases, you should keep plenary.nvim on its master branch. Restart Neovim so lazy.nvim installs everything.

Step 3: Point it at Ollama

CodeCompanion's default adapter is GitHub Copilot, so you need to tell it to use Ollama. Set the adapter and model for both the chat buffer and inline edits. With lazy.nvim, this goes in opts (the contents of setup()):

opts = {
  interactions = {
    chat = {
      adapter = { name = "ollama", model = "qwen3:8b" },
    },
    inline = {
      adapter = { name = "ollama", model = "qwen3:8b" },
    },
  },
},

Always include the model. The docs explain that if you set only adapter = "ollama", CodeCompanion picks one of your installed models and which one isn't predictable, and that the table form without a model throws an error when you open a chat. The model name is the tag shown by ollama list.

By default the adapter connects to http://localhost:11434. If Ollama runs on another PC on your network, set the same OLLAMA_HOST environment variable the Ollama CLI reads, for example export OLLAMA_HOST="http://192.168.1.100:11434". Our guide to sharing Ollama on your local network covers the server side.

Run :checkhealth codecompanion to confirm the dependencies are in place.

Step 4: Chat about your code

Open a file and run :CodeCompanionChat. Type a message and send it with Ctrl + S in insert mode or Enter in normal mode. To share the file you opened the chat from, include #{buffer}:

What does the code in #{buffer} do?

Other editor context includes #{diagnostics} for LSP errors, #{selection} for a visual selection and #{terminal} for your latest terminal output; typing # lists everything available. Press ga in the chat buffer to switch to another model you've pulled. :CodeCompanionChat Toggle hides and restores the chat without losing its history.

To let the model edit files, add @{files} to a message. Some tools, such as reading a file, ask for your approval before they run, and edits are shown as a diff before they're written; accept a change with g2 or reject it with g3.

Step 5: Edit code in place

For smaller changes you don't need the chat. Select some lines in visual mode and run:

:'<,'>CodeCompanion Use early returns

The model edits only the selected lines (it can still see the rest of the buffer for context) and shows the change as a diff. Move between hunks with } and {, accept or reject the hunk under the cursor with ga or gr, or accept or reject all the rest with g2 or g3. Press q to stop a request that's taking too long.

The built-in prompt library saves typing: :'<,'>CodeCompanion /explain explains a selection, /fix fixes it, /lsp explains its LSP diagnostics, and :CodeCompanion /commit writes a commit message for your staged changes. :CodeCompanionActions lists them all. CodeCompanion sets no global keymaps, so map the commands you use most yourself.

Step 6: Tune it for your hardware

CodeCompanion lets you set three Ollama options on the adapter that make a real difference on a home PC:

opts = {
  adapters = {
    http = {
      ollama = function()
        return require("codecompanion.adapters").extend("ollama", {
          schema = {
            num_ctx = { default = 32768 },
            think = { default = false },
            keep_alive = { default = "30m" },
          },
        })
      end,
    },
  },
},
  • num_ctx sets the context window Ollama loads the model with. Ollama's own default is 4,096 tokens, which fills up quickly once you share a whole file, but a larger window uses more memory. See Ollama context length explained.

  • think turns reasoning on or off for models that support it. Turning it off skips the model's reasoning step; see how to turn off thinking in Ollama.

  • keep_alive controls how long the model stays in memory after a request. Ollama's default is 5 minutes, after which the next request has to load the model again. Our guide to keeping Ollama models loaded explains the trade-off.

If you lower num_ctx below the model's maximum, CodeCompanion's docs recommend setting its context-management triggers as token counts rather than percentages, because it calculates the percentages from the model's maximum context window, not your setting.

Troubleshooting

  • The chat errors or hangs when it opens. CodeCompanion asks Ollama for its models when a chat opens and waits up to 3 seconds before giving up. Make sure Ollama is running (ollama list should respond).

  • Inline edits or @{files} do nothing. The model probably doesn't report tool support, so CodeCompanion doesn't send the tools. Check with ollama show and pick a tool-calling model.

  • Errors about system messages once tools are added. Some model templates need a single system message; the local-models guide includes a handler that merges them.

  • It's slow. Confirm Ollama is using your GPU and reduce num_ctx if the model is spilling out of VRAM.

Want to compare other options? See our best Neovim AI plugins for local LLMs and CodeCompanion vs avante.nvim. If you also use VS Code, our Ollama in VS Code guide covers the equivalent setup there.

The bottom line

To use Neovim with Ollama, pull a model that reports tool support (check with ollama show), install CodeCompanion.nvim pinned to a release on Neovim 0.12 or later, set both the chat and inline adapters to ollama with an explicit model, then use :CodeCompanionChat with #{buffer} to ask about your code and :'<,'>CodeCompanion to make edits you review as a diff. Raise num_ctx for bigger files, keep the model loaded, and expect better tool use from larger models if your GPU has the VRAM.

Sources

  1. 01CodeCompanion.nvim documentation, Running Local Models
  2. 02CodeCompanion.nvim documentation, Installation
  3. 03CodeCompanion.nvim documentation, Getting Started
  4. 04CodeCompanion.nvim documentation, Using the Inline Interaction
  5. 05GitHub, neovim/neovim releases
  6. 06Ollama documentation, FAQ

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

How to Choose an AI Upscaling Model: Using OpenModelDB to Find the Right One

How-To

How to Choose an AI Upscaling Model: Using OpenModelDB to Find the Right One

AI upscalers are only as good as the model you use. We explain how to use OpenModelDB to find the right one: subject and purpose tags, scale, architecture and app compatibility, file formats for PyTorch, ONNX and NCNN, licences, and how to shortlist and test models on your own images.

Marcus Thorne5 min

chaiNNer vs Upscayl: Which Free AI Image Upscaler Should You Use?

Versus

chaiNNer vs Upscayl: Which Free AI Image Upscaler Should You Use?

chaiNNer and Upscayl are two free, open source AI image upscalers you can run on your own PC. We compare ease of use, GPU support, model support, batch and video processing, how actively each is maintained, and licensing to help you choose.

Callum Reid6 min

How to Batch Upscale Images With chaiNNer: Free, Local AI Upscaling for Any GPU

How-To

How to Batch Upscale Images With chaiNNer: Free, Local AI Upscaling for Any GPU

chaiNNer is a free, open source, node-based app for running AI upscaling models locally. We cover which framework to install for NVIDIA, AMD and Intel graphics cards, downloading a model, building a simple upscale chain, turning it into a folder batch job and fixing black output and other common errors.

Marcus Thorne7 min

Best Monitor Control Apps for Windows in 2026: Ditch the OSD Buttons

Best Picks

Best Monitor Control Apps for Windows in 2026: Ditch the OSD Buttons

Most big monitor brands offer a free Windows app that replaces fiddly OSD buttons with mouse controls, plus extras such as layouts, per-app modes and firmware updates. Using each maker's own pages, we pick five: Dell Display and Peripheral Manager, ASUS DisplayWidget Center, LG OnScreen Control, BenQ Display Pilot 2 and Gigabyte OSD Sidekick.

Hannah Vance5 min