Install Cody for Neovim

Learn how to use Cody and its features with the Neovim editor.

The Cody extension for Neovim by Sourcegraph enhances your coding experience in your IDE by providing intelligent code suggestions, context-aware completions, and advanced code analysis.

This guide will walk you through installing and setting up the Cody within your Neovim environment.

Prerequisites

Installation

sg.nvim is a plugin that uses Sourcegraph's code intelligence features directly within the Neovim text editor. You can install the plugin using different Neovim plugin managers like:

lazy.nvim

return {
  {
    "sourcegraph/sg.nvim",
    dependencies = { "nvim-lua/plenary.nvim" },

    -- If you have a recent version of lazy.nvim, you don't need to add this!
    build = "nvim -l build/init.lua",
  },
}

packer.nvim

-- Packer.nvim, also make sure to install nvim-lua/plenary.nvim
use { 'sourcegraph/sg.nvim', run = 'nvim -l build/init.lua' }

vim-plug

-- Using vim-plug
Plug 'sourcegraph/sg.nvim', { 'do': 'nvim -l build/init.lua' }

Once you have installed the plugin, run :checkhealth sg to verify a successful installation. Next, you are prompted to log in as a free user by connecting to your Sourcegraph.com account or using the enterprise instance.

Setting up with Sourcegraph instance

To connect sg.nvim with Sourcegraph, you need to follow these steps:

  • Log in on your Sourcegraph instance
  • Go to Settings > Access tokens from the top right corner
  • Create your access token, and then run :SourcegraphLogin in your neovim editor after installation
  • Type in the link to your Sourcegraph instance (for example, https://sourcegraph.com)
  • Next, paste your generated access token

An alternative way to this is to use the environment variables specified for src-cli.

At any point, you can run :checkhealth sg to ensure you're logged in and connected to your Sourcegraph instance.

Features

The sg.nvim plugin supports a wide range of features that helps you integrate and use Cody and Sourcegraph Search directly within your Neovim environment.

Cody

The sg.nvim extension supports the following features for Cody:

Feature Description
Chat Chat interface and associated commands
Autocompletions Support both prompted and suggested autocompletions

The sg.nvim extension supports the following features for Sourcegraph Search:

Feature Description
Read files Directly from sourcegraph links: :edit <sourcegraph url>
Automatically adds protocols for https://sourcegraph.com/* links
Directly from buffer names :edit sg://github.com/tjdevries/sam.py/-/src/sam.py
Read non-files Repository roots, folders (both expanded and non-expanded), open file from folder
Built-in LSP client Connects to Sourcegraph via Goto Definition and Goto References (<20 references)
Basic search Literal, regexp, and structural search support, type:symbol support, and repo support
Advanced search features Autocompletions and memory of last searches

Commands

The sg.nvim extension also supports pre-built reusable prompts for Cody called "Commands" that help you quickly get started with common programming tasks like:

  • :CodyAsk: Ask a question about the current selection
  • :CodyChat {title}: Starts a new Cody chat, with an optional {title}
  • :CodyRestart: Restarts Cody and Sourcegraph
  • :CodyTask {task_description}: Instructs Cody to perform a task on a selected text
  • :CodyTaskAccept: Accepts the current CodyTask
  • :CodyTaskNext: Cycles to the next CodyTask
  • :CodyTaskPrev: Cycles to the previous CodyTask
  • :CodyTaskView: Opens the last active CodyTask
  • :CodyToggle: Toggle to the current Cody Chat window

More benefits

Read more about Cody capabilities to learn about all the features it provides to boost your development productivity.

More resources

For more information on what to do next, we recommend the following resources: