claude-code.nvim

repository·main·Indexed 24 days ago

https://github.com/greggh/claude-code.nvim

A Neovim plugin providing terminal integration for the Claude Code AI assistant. It allows users to toggle the assistant, manage conversations, and automatically sync file changes. Features include support for floating windows, custom command variants, and git root detection. Requires Neovim 0.7.0+, the Claude Code CLI, and plenary.nvim.

Tokens
3.1K
Snippets
7
Records
12
Agent score
83%

What's inside claude-code.nvim

  1. Install development dependencies for Linux, macOS, and Windows

    main

    To develop for claude-code.nvim, you must install Neovim (0.10.0+), Git, Make, LuaRocks, stylua (formatter), and luacheck (linter). Optional but recommended tools include ripgrep and fd.

    Ubuntu/Debian

    # Install Neovim (from PPA for latest version)
    sudo add-apt-repository ppa:neovim-ppa/unstable
    sudo apt-get update
    sudo apt-get install neovim
    
    # Install luarocks and other dependencies
    sudo apt-get install luarocks ripgrep fd-find git make
    
    # Install luacheck
    sudo luarocks install luacheck
    
    # Install stylua
    curl -L -o stylua.zip $(curl -s https://api.github.com/repos/JohnnyMorganz/StyLua/releases/latest | grep -o "https://.*stylua-linux-x86_64.zip")
    unzip stylua.zip
    chmod +x stylua
    sudo mv stylua /usr/local/bin/

    Arch Linux

    # Install dependencies
    sudo pacman -S neovim luarocks ripgrep fd git make
    
    # Install luacheck
    sudo luarocks install luacheck
    
    # Install stylua (from AUR)
    yay -S stylua

    Fedora

    # Install dependencies
    sudo dnf install neovim luarocks ripgrep fd-find git make
    
    # Install luacheck
    sudo luarocks install luacheck
    
    # Install stylua
    curl -L -o stylua.zip $(curl -s https://api.github.com/repos/JohnnyMorganz/StyLua/releases/latest | grep -o "https://.*stylua-linux-x86_64.zip")
    unzip stylua.zip
    chmod +x stylua
    sudo mv stylua /usr/local/bin/

    macOS

    # Install Homebrew if not already installed
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
    # Install dependencies
    brew install neovim luarocks ripgrep fd git make
    
    # Install luacheck
    luarocks install luacheck
    
    # Install stylua
    brew install stylua

    Windows (Scoop)

    # Install scoop if not already installed
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
    
    # Install dependencies
    scoop install neovim git make ripgrep fd
    
    # Install luarocks
    scoop install luarocks
    
    # Install luacheck
    luarocks install luacheck
    
    # Install stylua
    scoop install stylua

    Windows (Chocolatey)

    # Install chocolatey if not already installed
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
    
    # Install dependencies
    choco install neovim git make ripgrep fd
    
    # Install luarocks
    choco install luarocks
    
    # Install luacheck
    luarocks install luacheck
    
    # Install stylua (download from GitHub)
    # Visit https://github.com/JohnnyMorganz/StyLua/releases
  2. Use Claude Code commands and keymaps

    main

    Commands

    • :ClaudeCode: Toggles the Claude Code terminal window.
    • :ClaudeCodeContinue: Resumes the most recent conversation (requires --continue variant).
    • :ClaudeCodeResume: Displays an interactive conversation picker (requires --resume variant).
    • :ClaudeCodeVerbose: Enables verbose logging (requires --verbose variant).

    Note: Commands are automatically generated based on your command_variants configuration.

    Default Key Mappings

    • <leader>ac: Toggle Claude Code terminal window (Normal mode).
    • <C-,>: Toggle Claude Code terminal window (Normal and Terminal modes).

    Variant Mappings (if configured)

    • <leader>cC: Toggle Claude Code with --continue flag.
    • <leader>cV: Toggle Claude Code with --verbose flag.

    Terminal Navigation & Scrolling

    While inside the Claude Code terminal:

    • <C-h/j/k/l>: Navigate to the window on the left, below, above, or right.
    • <C-f>: Scroll full-page down.
    • <C-b>: Scroll full-page up.

    Note: After scrolling with <C-f> or <C-b>, press i to re-enter insert mode to continue typing.

  3. Access Claude Code documentation

    main

    You can find information about Claude Code in several locations:

    • Basic usage and installation: Refer to the README.md.
    • Neovim help documentation: Use the built-in Neovim help system to view doc/claude-code.txt.
    • Development information: Refer to DEVELOPMENT.md.
    • Configuration tips: See the CLAUDE.md file for additional configuration options and effective usage tips.
  4. Set up the development environment and Git hooks

    main

    To begin developing, clone the repository and initialize the Git hooks which automate formatting, linting, and testing during commits.

    # 1. Clone the repository
    git clone https://github.com/greggh/claude-code.nvim.git
    
    # 2. Install Git hooks
    cd claude-code.nvim
    ./scripts/setup-hooks.sh
    git clone https://github.com/greggh/claude-code.nvim.git
    cd claude-code.nvim
    ./scripts/setup-hooks.sh
  5. Requirements for claude-code.nvim

    main

    Before using this plugin, ensure you meet the following requirements:

    • Neovim: version 0.7.0 or later.
    • Claude Code CLI: Must be installed and available in your system PATH.
    • plenary.nvim: Required dependency for git operations.
  6. Install claude-code.nvim

    main

    Install the plugin using your preferred Neovim package manager. Note that plenary.nvim is a required dependency for git operations.

    Using lazy.nvim

    return {
      "greggh/claude-code.nvim",
      dependencies = {
        "nvim-lua/plenary.nvim", -- Required for git operations
      },
      config = function()
        require("claude-code").setup()
      end
    }

    Using packer.nvim

    use {
      'greggh/claude-code.nvim',
      requires = {
        'nvim-lua/plenary.nvim', -- Required for git operations
      },
      config = function()
        require('claude-code').setup()
      end
    }

    Using vim-plug

    Plug 'nvim-lua/plenary.nvim'
    Plug 'greggh/claude-code.nvim'
    " After installing, add this to your init.vim:
    " lua require('claude-code').setup()
    return {
      "greggh/claude-code.nvim",
      dependencies = {
        "nvim-lua/plenary.nvim", -- Required for git operations
      },
      config = function()
        require("claude-code").setup()
      end
    }
  7. Get help with Claude Code

    main

    Depending on your needs, you can seek support through the following channels:

    • General questions, ideas, or community discussions: Use GitHub Discussions. Categories include Q&A, Ideas, Show and Tell, and General.
    • Reporting bugs or requesting features: Use the GitHub issue tracker.

    When reporting an issue, please include:

    • Neovim version
    • Plugin version
    • Operating system
    • Detailed description of the problem or request
  8. Configure claude-code.nvim

    main

    Configure the plugin by passing a table to the setup() function.

    Key Configuration Sections:

    • window: Controls terminal window behavior, including split_ratio, position (e.g., "botright", "float"), and enter_insert.
    • window.float: Settings for floating windows, such as width, height, row, col, relative, and border (e.g., "rounded", "double").
    • refresh: Manages file change detection via enable, updatetime, and timer_interval.
    • git: Use use_git_root to automatically set the working directory to the git root.
    • command: The CLI command used to launch Claude Code (defaults to "claude").
    • command_variants: Define custom command-line arguments (like --continue or --verbose) which will automatically generate corresponding Neovim commands and keymaps.
    • keymaps: Define custom mappings for toggle (normal and terminal modes) and variants.
    require("claude-code").setup({
      -- Terminal window settings
      window = {
        split_ratio = 0.3,
        position = "botright",
        enter_insert = true,
        hide_numbers = true,
        hide_signcolumn = true,
        
        -- Floating window configuration (only applies when position = "float")
        float = {
          width = "80%",
          height = "80%",
          row = "center",
          col = "center",
          relative = "editor",
          border = "rounded",
        },
      },
      -- File refresh settings
      refresh = {
        enable = true,
        updatetime = 100,
        timer_interval = 1000,
        show_notifications = true,
      },
      -- Git project settings
      git = {
        use_git_root = true,
      },
      -- Shell-specific settings
      shell = {
        separator = '&&',
        pushd_cmd = 'pushd',
        popd_cmd = 'popd',
      },
      -- Command settings
      command = "claude",
      -- Command variants
      command_variants = {
        continue = "--continue",
        resume = "--resume",
        verbose = "--verbose",
      },
      -- Keymaps
      keymaps = {
        toggle = {
          normal = "<C-,>",
          terminal = "<C-,>",
          variants = {
            continue = "<leader>cC",
            verbose = "<leader>cV",
          },
        },
        window_navigation = true,
        scrolling = true,
      }
    })
  9. Verify Neovim detection logic

    main

    When running tests from within Neovim, the test script handles the $NVIM environment variable (which often points to a socket). It attempts to use the path in $NVIM if it's an executable, otherwise falling back to nvim in $PATH. You can verify this logic by running the detection script.

    ./scripts/test_nvim_detection.sh
  10. Configure Claude Code in a floating window

    main

    To use Claude Code in a floating window instead of a split, set the window.position to "float" in your setup configuration. You can then define the size and position of the float using the window.float table.

    require("claude-code").setup({
      window = {
        position = "float",
        float = {
          width = "90%",
          height = "90%",
          row = "center",
          col = "center",
          relative = "editor",
          border = "double",
        },
      },
    })
    require("claude-code").setup({
      window = {
        position = "float",
        float = {
          width = "90%",      -- Take up 90% of the editor width
          height = "90%",     -- Take up 90% of the editor height
          row = "center",     -- Center vertically
          col = "center",     -- Center horizontally
          relative = "editor",
          border = "double",  -- Use double border style
        },
      },
    })
  11. Write tests using the BDD-style Lua API

    main

    Tests are written in Lua using a BDD-style (Behavior Driven Development) API provided by the internal test runner. Use test.describe to group tests and test.it for individual test cases.

    local test = require("tests.run_tests")
    
    test.describe("Feature name", function()
      test.it("should do something", function()
        -- Test code
        test.expect(result).to_be(expected)
      end)
    end)
  12. Run development tasks with Make

    main

    The project uses a Makefile to provide a consistent interface for common development tasks across different platforms.

    CommandDescription
    make testRun all tests
    make test-verboseRun tests with verbose output
    make test-basicRun basic test suites
    make test-configRun configuration test suites
    make lintRun Lua linting with luacheck
    make formatFormat code with stylua
    make helpView available commands

    Note: Pre-commit hooks automatically run stylua, luacheck, and basic tests. To bypass these, use git commit --no-verify.

    make test
    make lint
    make format
    make help