Skip to content

[BUG] Split window shows duplicate terminal buffer when opening Claude Code #121

@waipbmtd

Description

@waipbmtd

Bug Description

When opening Claude Code with the default split window mode (non-float), the terminal buffer
is displayed in both the new split window and the original editor window. Both windows show
identical content and input is synchronized between them.

Steps To Reproduce

  1. Open Neovim with a single window (e.g. nvim with no arguments, or with a file)
  2. Press <C-,> (or run :ClaudeCode) to open Claude Code
  3. Observe that two windows display the same Claude Code terminal — one in the right split
    and one in the original editor area

Expected Behavior

Only the new split window (right side) should display the Claude Code terminal. The original
editor window should remain on its previous buffer.

Root Cause

In lua/claude-code/terminal.lua, function create_new_instance, the split code path (line
~354-383):

create_split(config.window.position, config)
-- At this point, vsplit makes both windows share the same buffer

local cmd = 'terminal ' .. base_cmd
vim.cmd(cmd) -- converts the shared buffer into a terminal → both windows affected

create_split runs vsplit, so the new window inherits the original buffer. When :terminal then
replaces that buffer with a terminal, both windows end up showing it.

The float code path does NOT have this issue because it creates a dedicated buffer via
nvim_create_buf() before opening the terminal.

Suggested Fix

Align the split path with the float path — create a dedicated buffer with nvim_create_buf()
and use termopen() instead of :terminal:

else          
  -- Regular split window: create a dedicated buffer first to avoid sharing
  -- the original window's buffer (vsplit would make both windows display
  -- the same terminal otherwise).                                                         
  local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch
  vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})                    
                                                                                           
  -- Open split and switch it to the dedicated buffer
  create_split(config.window.position, config, new_bufnr)                                  
              
  -- Determine if we should use the git root directory
  local base_cmd = build_command_with_git_root(config, git, config.command)
                                                                                           
  -- Run terminal in the dedicated buffer
  vim.fn.termopen(base_cmd)                                                                
              
  -- Create a unique buffer name
  local buffer_name = generate_buffer_name(instance_id, config)
  vim.api.nvim_buf_set_name(new_bufnr, buffer_name)                                        

  -- Configure window options using helper function                                        
  local current_win = vim.api.nvim_get_current_win()
  configure_window_options(current_win, config)
                                                                                           
  -- Store buffer number for this instance
  claude_code.claude_code.instances[instance_id] = new_bufnr                               
              
  -- Automatically enter insert mode in terminal unless configured to start in normal mode
  if config.window.enter_insert and not config.window.start_in_normal_mode then
    vim.cmd 'startinsert'                                                                  
  end
end                                                                                        

This has been tested and confirmed to fix the issue.

Environment

  • OS: macOS (Darwin 25.2.0)
  • Neovim version: v0.12.1
  • Claude Code CLI version: 2.1.110
  • Plugin version: v0.4.2 (commit 55c0cb5)

Plugin Configuration

{
"greggh/claude-code.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {
window = {
split_ratio = 0.35,
position = "botright vertical",
enter_insert = true,
hide_numbers = true,
hide_signcolumn = true,
},
git = {
multi_instance = false,
},
},
}

Additional Context

  • The default position "botright" also triggers this bug since it also goes through the
    vsplit/split path.
  • The floating window mode (position = "float") is not affected because it already uses
    nvim_create_buf() + termopen().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions