# Install Cody for Neovim

<p className="subtitle">
	Learn how to use Cody and its features with the Neovim editor.
</p>

<Callout type="note" title="Experimental">
	Cody for Neovim is currently in the experimental stage and is no longer
	actively maintained, so support will be limited.
</Callout>

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.

<LinkCards>
	<LinkCard
		href="https://github.com/sourcegraph/sg.nvim"
		imgSrc="https://storage.googleapis.com/sourcegraph-assets/Docs/neovim-logo.png"
		imgAlt="Cody for Neovim"
		title="Neovim Extension (Experimental)"
		description="Install Cody's free extension for Neovim."
	/>
</LinkCards>

## Prerequisites

-   `nvim 0.9 or nvim nightly` version of <a href="https://github.com/neovim/neovim/wiki/Installing-Neovim" target="_blank" rel="noopener noreferrer">Neovim</a> installed
-   `Node.js >= 18.17.0 (LTS)` at runtime for cody-agent.js
-   You have enabled an instance for [Cody from your Sourcegraph.com](/cody/clients/cody-with-sourcegraph) account

## 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`

```lua
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`

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

### `vim-plug`

```lua
-- 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`](https://github.com/sourcegraph/src-cli#log-into-your-sourcegraph-instance).

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 |

### Search

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` (less than 20 references) |
| **Basic search**             | Keyword and regexp 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
