This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
codex.nvim is a minimal Neovim plugin that provides integration with the OpenAI Codex CLI. It opens Codex TUI in terminal splits and runs non-interactive codex exec commands with prompts from Neovim, including visual selections.
# Run all tests
make test
# Individual test files can be run manually:
nvim --headless -u tests/minimal_init.lua +"lua dofile('tests/run_basic.lua')"
nvim --headless -u tests/minimal_init.lua +"lua dofile('tests/run_commands.lua')"
# Tests run headless with nvim and don't require the actual Codex CLI
# Tests are in tests/ directory with minimal_init.lua, run_basic.lua, and run_commands.luaThe plugin follows a modular architecture with clear separation of concerns:
-
lua/codex/init.lua - Main entry point and API (lines 1-80+)
setup()- Initialize plugin with user configurationopen()- Open Codex TUI with optional prompttoggle()- Smart toggle: opens terminal if none exists, otherwise toggles visibility- Creates user commands:
:CodexOpen,:CodexToggle
-
lua/codex/config.lua - Configuration management (lines 1-50)
- Default settings for binary path, model, terminal behavior
- Supports
cwd_provider: 'git' (repo root), 'cwd' (current dir), 'file' (file's directory) - Terminal configuration: direction, size, position, provider (native/snacks/auto)
- Handles backward compatibility aliases
-
lua/codex/terminal.lua - Terminal split management (lines 1-300+)
- Manages terminal buffer lifecycle and reuse
- Supports multiple terminal providers: native Neovim, Snacks.nvim (if available)
- Handles headless mode for testing (runs as background jobs)
- Smart split sizing with fractional support (0-1 as percentage)
run()- Main function to execute commands in terminal (line 175)simple_toggle()- Toggle terminal visibility with improved state checking (line 137)
-
lua/codex/utils.lua - Utility functions (lines 1-75)
get_cwd()- Determine working directory based on provider settingbuild_cmd()- Build command array with model, approval flags, extra args- Git repository detection and root finding
-
lua/codex/logger.lua - Logging system
- Log levels: trace, debug, info, warn, error
- Configurable via
log_leveloption orCODEX_LOG_LEVELenv var
- State Management: Single state object in init.lua tracks configuration and terminal state
- Terminal Reuse: Reuses terminal buffers by default to avoid multiple windows
- Provider Abstraction: Supports multiple terminal providers with fallback logic
- Command Building: Central command construction with consistent flag handling
- Working Directory Logic: Smart CWD detection prioritizing git root, with fallbacks
- Environment Sanitization: All environment variables are converted to strings and nil values filtered out
- Codex CLI: Expects
codexbinary on PATH (or custom path viabinconfig) - Git: Optional git integration for project root detection via
git rev-parse - Snacks.nvim: Optional enhanced terminal provider if available (auto-detected)
- Neovim APIs: Uses native terminal, buffer, and window management APIs
- Headless Support: Detects headless mode and runs commands as background jobs
The plugin supports multiple terminal providers with fallback:
native: Pure Neovim terminal splitssnacks: Uses folke/snacks.nvim if availableauto: Detects Snacks availability and falls back gracefully
Size calculation supports both absolute values (>= 1) and fractional values (0 < size < 1) as percentages of the current window.
When modifying the plugin:
- Adding new commands: Update
M._create_commands()in init.lua (around line 90+) - Terminal behavior: Modify terminal.lua, especially
M.run()at line 175+ andM.simple_toggle()at line 137+ - Configuration options: Add to
M.defaultsin config.lua:3 and handle inM.apply() - Command building: Update
build_cmd()in utils.lua:42 for new CLI flags - Logging: Use the logger module for debugging - levels: trace, debug, info, warn, error
The test suite uses headless Neovim and mocks the Codex CLI (using echo instead):
tests/minimal_init.lua- Sets up minimal environment and silences UI promptstests/run_basic.lua- Tests core functionalitytests/run_commands.lua- Tests user commands- Tests validate plugin behavior without requiring actual Codex CLI installation