Codex Command Line Tool

Codex CLI is an official command-line tool written in Rust. It is lightweight and efficient, suitable for terminal enthusiasts.


Install CLI

Codex CLI supports macOS, Linux, and Windows (experimental).

npm Install

npm Global Install

# Install using npm
npm install -g @openai/codex

# Verify installation
codex --version

Homebrew Install

Homebrew Install

# Use Homebrew on macOS
brew install codex

Update CLI

Update Version

# Update via npm
npm install -g @openai/codex@latest

# Update via Homebrew
brew upgrade codex

First Time Use

The first time you run Codex, it will guide you through authentication.

Launch Codex

# Launch the interactive interface
codex

# Or specify an initial task
codex "Explain this project structure"

Codex will open a browser for authentication.

  1. Log in with ChatGPT account
  2. Or use OpenAI API Key
  3. After authorization, return to the terminal to continue.

Interactive TUI

By default, the CLI enters Terminal User Interface (TUI) mode, providing an immersive experience.

TUI Interface Areas

AreaFunction
Message AreaDisplays conversation history, tool calls, and execution results
Input AreaBottom input box, send Prompt
Status BarDisplays model, context usage, and thread information

TUI Mode Settings

TUI Configuration

# ~/.codex/config.toml

[tui]
# Alternate screen mode
alternate_screen = "auto"  # auto | always | never

# Keep scroll history when alternate screen is disabled
# alternate_screen = "never"

Alternate screen mode makes Codex use a separate screen and restores the original terminal state when exiting.


Approval Mode

Approval Mode controls the confirmation behavior before Codex performs actions.

Four Modes

ModeDescriptionBehavior
suggestSuggest ModeCodex provides suggestions but does not execute
auto-editAuto-editAutomatically performs file edits, commands require confirmation
full-autoFully AutomaticAutomatically executes all operations (use with caution)
interactiveInteractive ModeAsk before sensitive operations (default)

Set Approval Mode

# Specify mode via command line
codex --approval-mode full-auto

# Or switch in session
/approval full-auto

In full-auto mode, Codex automatically executes all operations, including deleting files. Use with caution.


Sandbox Mode

Sandbox mode restricts Codex's file and command access scope.

ModeFile modificationCommand executionNetwork access
read-onlyForbiddenForbiddenForbidden
workspace-writeWorkspace onlyAllowedForbidden
danger-full-accessAllowedAllowedAllowed

Set Sandbox Mode

# Read-only mode
codex --sandbox read-only

# Workspace write mode (recommended)
codex --sandbox workspace-write

# Full access mode (dangerous)
codex --sandbox danger-full-access

Slash Commands

The CLI provides a rich set of slash commands:

CommandFunction
/modelSwitch model (e.g., /model gpt-5.4-mini)
/fastToggle Fast mode
/planEnter plan mode
/reviewReview code changes
/newStart a new session
/resumeResume a previous session
/forkClone current session to a new thread
/compactCompress context history
/statusShow session status
/clearClear screen
/quitExit Codex

Command Usage Examples

Slash Commands

# Switch model
/model gpt-5.4-mini

# Plan mode
/plan implement user authentication system

# View status
/status

Shell Command Execution

Execute shell commands directly in the CLI:

Execute Command

# Prepend with ! to execute a command
! ls -la

# Execute git command
! git status

# View processes
! ps aux

Command Confirmation

Dangerous commands require confirmation:

  • Delete files (rm)
  • Terminate processes (kill)
  • Modify system configuration

Image Input

The CLI supports attaching images as context:

Attach Image

# Attach screenshot
codex -i screenshot.png

# Attach design mockup
codex --image design.png "Implement the page based on this design"

# Multiple images
codex -i img1.png -i img2.png

Image Usage

  • Analyze error messages from screenshots
  • Convert UI design mockups to code
  • Interpret chart data

Non-interactive Mode (exec)

Use the exec command to run a one-off task, suitable for scripts and CI/CD:

exec Command

# Run a one-off task
codex exec "Review the code and output a report"

# Output to file
codex exec "Generate README" -o README.md

# Use a specific model
codex exec -m gpt-5.4-mini "Analyze project structure"

# Full auto mode
codex exec --full-auto "Run tests and fix failures"

Common exec Parameters

ParameterDescription
-mSpecify model
-oOutput to file
--full-autoFull auto execution
--ephemeralDo not save session files
--jsonJSON output format

Keyboard Shortcuts

ShortcutFunction
EnterSend message
Shift+EnterNew line
Ctrl+CInterrupt operation
Ctrl+C (两次)Exit Codex
Ctrl+DExit (when input is empty)
Ctrl+RSearch history
Up/DownBrowse history
TabAuto-complete
Esc EscEdit last message
Ctrl+OCopy last reply

FAQ

Q: Windows support status?

Windows native support is experimental; WSL2 is recommended for the best experience.

Q: How to disable alternate screen?

Usecodex --no-alt-screenor configurealternate_screen = "never"。

Q: Command not found?

Ensure PATH includes the npm global installation directory, or install using Homebrew.

Q: How to view detailed logs?

Logs are located in~/.codex/log/directory.

Other extensions