Skip to content

Commit dfd1d3f

Browse files
feat: add env configuration option
- Add env field to config for passing environment variables to Claude CLI - Update init.lua to pass env variables when spawning Claude terminal - Allows users to set custom environment like ANTHROPIC_API_KEY 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c1cdcd5 commit dfd1d3f

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lua/claudecode/config.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ M.defaults = {
66
port_range = { min = 10000, max = 65535 },
77
auto_start = true,
88
terminal_cmd = nil,
9+
env = {}, -- Custom environment variables for Claude terminal
910
log_level = "info",
1011
track_selection = true,
1112
visual_demotion_delay_ms = 50, -- Milliseconds to wait before demoting a visual selection
@@ -61,6 +62,13 @@ function M.validate(config)
6162
assert(type(config.diff_opts.vertical_split) == "boolean", "diff_opts.vertical_split must be a boolean")
6263
assert(type(config.diff_opts.open_in_current_tab) == "boolean", "diff_opts.open_in_current_tab must be a boolean")
6364

65+
-- Validate env
66+
assert(type(config.env) == "table", "env must be a table")
67+
for key, value in pairs(config.env) do
68+
assert(type(key) == "string", "env keys must be strings")
69+
assert(type(value) == "string", "env values must be strings")
70+
end
71+
6472
return true
6573
end
6674

@@ -76,9 +84,19 @@ function M.apply(user_config)
7684
config = vim.tbl_deep_extend("force", config, user_config)
7785
end
7886

87+
-- Check environment variable for terminal_cmd if not set in user config
88+
if config.terminal_cmd == nil then
89+
local env_cmd = vim.fn.getenv("CLAUDE_TERMINAL_CMD")
90+
if env_cmd ~= vim.NIL then
91+
config.terminal_cmd = env_cmd
92+
end
93+
end
94+
7995
M.validate(config)
8096

8197
return config
8298
end
8399

84100
return M
101+
102+

lua/claudecode/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ M.version = {
3434
--- @field port_range {min: integer, max: integer} Port range for WebSocket server.
3535
--- @field auto_start boolean Auto-start WebSocket server on Neovim startup.
3636
--- @field terminal_cmd string|nil Custom terminal command to use when launching Claude.
37+
--- @field env table<string,string> Custom environment variables for Claude terminal.
3738
--- @field log_level "trace"|"debug"|"info"|"warn"|"error" Log level.
3839
--- @field track_selection boolean Enable sending selection updates to Claude.
3940
--- @field visual_demotion_delay_ms number Milliseconds to wait before demoting a visual selection.
@@ -44,6 +45,7 @@ local default_config = {
4445
port_range = { min = 10000, max = 65535 },
4546
auto_start = true,
4647
terminal_cmd = nil,
48+
env = {},
4749
log_level = "info",
4850
track_selection = true,
4951
visual_demotion_delay_ms = 200,
@@ -97,14 +99,14 @@ function M.setup(opts)
9799
local logger = require("claudecode.logger")
98100
logger.setup(M.state.config)
99101

100-
-- Setup terminal module: always try to call setup to pass terminal_cmd,
102+
-- Setup terminal module: always try to call setup to pass terminal_cmd and env,
101103
-- even if terminal_opts (for split_side etc.) are not provided.
102104
local terminal_setup_ok, terminal_module = pcall(require, "claudecode.terminal")
103105
if terminal_setup_ok then
104106
-- terminal_opts might be nil if user only configured top-level terminal_cmd
105107
-- and not specific terminal appearance options.
106108
-- The terminal.setup function handles nil for its first argument.
107-
terminal_module.setup(terminal_opts, M.state.config.terminal_cmd)
109+
terminal_module.setup(terminal_opts, M.state.config.terminal_cmd, M.state.config.env)
108110
else
109111
logger.error("init", "Failed to load claudecode.terminal module for setup.")
110112
end
@@ -338,3 +340,5 @@ function M.get_version()
338340
end
339341

340342
return M
343+
344+

lua/claudecode/terminal.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local term_module_config = {
2222
provider = "snacks",
2323
show_native_term_exit_tip = true,
2424
terminal_cmd = nil, -- Will be set by setup() from main config
25+
env = {}, -- Will be set by setup() from main config
2526
}
2627

2728
--- State to keep track of the managed Claude terminal instance (from Snacks).
@@ -53,7 +54,8 @@ end
5354
-- @field user_term_config.provider string 'snacks' or 'native' (default: 'snacks').
5455
-- @field user_term_config.show_native_term_exit_tip boolean Show tip for exiting native terminal (default: true).
5556
-- @param p_terminal_cmd string|nil The command to run in the terminal (from main config).
56-
function M.setup(user_term_config, p_terminal_cmd)
57+
-- @param p_env table|nil Custom environment variables to pass to the terminal (from main config).
58+
function M.setup(user_term_config, p_terminal_cmd, p_env)
5759
if user_term_config == nil then -- Allow nil, default to empty table silently
5860
user_term_config = {}
5961
elseif type(user_term_config) ~= "table" then -- Warn if it's not nil AND not a table
@@ -71,6 +73,16 @@ function M.setup(user_term_config, p_terminal_cmd)
7173
term_module_config.terminal_cmd = nil -- Fallback to default behavior in get_claude_command
7274
end
7375

76+
if p_env == nil or type(p_env) == "table" then
77+
term_module_config.env = p_env or {}
78+
else
79+
vim.notify(
80+
"claudecode.terminal.setup: Invalid env provided: " .. tostring(p_env) .. ". Using empty table.",
81+
vim.log.levels.WARN
82+
)
83+
term_module_config.env = {}
84+
end
85+
7486
for k, v in pairs(user_term_config) do
7587
if term_module_config[k] ~= nil and k ~= "terminal_cmd" then -- terminal_cmd is handled above
7688
if k == "split_side" and (v == "left" or v == "right") then
@@ -351,6 +363,11 @@ local function get_claude_command_and_env()
351363
env_table["CLAUDE_CODE_SSE_PORT"] = tostring(sse_port_value)
352364
end
353365

366+
-- Merge custom environment variables from config
367+
for key, value in pairs(term_module_config.env) do
368+
env_table[key] = value
369+
end
370+
354371
return cmd_string, env_table
355372
end
356373

0 commit comments

Comments
 (0)