Codex CLI — AI Coding in Your Terminal
Codex CLI is OpenAI's terminal-native AI coding assistant. This cheatsheet covers the most frequently used commands and configurations for daily workflows.
Installation & Startup {#installation}
### One-liner install script
``bash
curl -fsSL https://codex.sh/install | sh
`
### Install via npm
`bash
npm install -g @openai/codex
`
### Install via Homebrew (macOS)
`bash
brew install --cask codex
`
### Verify installation
`bash
codex --version
`
### Start interactive mode
`bash
codex
`
### Launch web UI
`bash
codex app
`
### Single prompt
`bash
codex "Write a Python Fibonacci function"
`
### Download from GitHub Release
`bash
# Visit https://github.com/openai/codex/releases to download platform-specific binaries
`
Chat Sessions {#chat-sessions}
### View help
`bash
/help
`
### Clear session
`bash
/clear
`
### View diff
`bash
/diff
`
### Undo changes
`bash
/undo
`
### Exit
`bash
/exit
`
### Specify model
`bash
codex --model o4-mini
`
Code Development {#code-development}
### Code review
`bash
/review
`
### Auto-fix
`bash
/fix
`
### Run tests
`bash
/test
`
### Commit code
`bash
/commit
`
### Generate tests
`bash
codex "Generate pytest tests for functions in utils.py"
`
### Refactor code
`bash
codex "Refactor main.py to extract database logic into a separate module"
`
### Generate documentation
`bash
codex "Generate Google-style docstrings for all public functions in project/"
`
Configuration & Environment {#configuration}
### CODEX.md project config
`markdown
# .codex configuration — create CODEX.md in project root
constInstructions
- Use TypeScript strict mode
- All functions require JSDoc comments
- Prefer over let
Context
Project is a Next.js 14 App Router app using Prisma ORM. `### API Key authentication
`bash
export OPENAI_API_KEY="sk-xxxx"
codex
`### ChatGPT sign-in
`bash
codex app
# Sign in with your ChatGPT account in the browser that opens
`### Check version
`bash
codex --version
`### Config file location
`bash
# macOS: ~/Library/Application Support/codex/
# Linux: ~/.config/codex/
# Env variable: CODEX_DIR
``
Installation & Startup(9)
| Command | Level | ||
|---|---|---|---|
curl -fsSL https://codex.sh/install | shOne-liner install script | Basic | curl -fsSL https://codex.sh/install | sh | |
npm install -g @openai/codexInstall globally via npm | Basic | npm install -g @openai/codex | |
brew install --cask codexInstall via Homebrew (macOS) | Basic | brew install --cask codex | |
codexStart interactive session | Basic | codex | |
codex appLaunch web user interface | Basic | codex app | |
codex <prompt>Single-shot prompt for a direct answer | Basic | codex "Write a Python Fibonacci function" | |
codex --versionCheck installed version | Basic | codex --version | |
curl -L -o codex https://github.com/openai/codex/releases/latest/download/codex-<platform>Download binary from GitHub Release | Intermediate | curl -L -o codex https://github.com/openai/codex/releases/latest/download/codex-linux-x64 && chmod +x codex | |
codex doctorRun diagnostics to verify environment and configuration | Basic | codex doctor |
Chat Sessions(5)
| Command | Level | ||
|---|---|---|---|
/helpView help information | Basic | /help | |
/clearClear current session context | Basic | /clear | |
/diffView diff of current changes | Intermediate | /diff | |
/undoUndo the most recent change | Intermediate | /undo | |
/exitExit interactive session | Basic | /exit |
Code Development(13)
| Command | Level | ||
|---|---|---|---|
/reviewReview current code changes | Intermediate | /review | |
/fixAuto-fix issues in the code | Intermediate | /fix | |
/testRun tests | Intermediate | /test | |
/commitGenerate commit message and run git commit | Expert | /commit | |
codex "为 utils.py 生成 pytest 测试"Generate test cases | Intermediate | codex "Generate pytest tests for utils.py" | |
codex "重构 main.py,分离数据库逻辑"Refactor code structure | Expert | codex "Refactor main.py to extract database logic" | |
codex "为 project/ 生成文档字符串"Auto-generate code documentation | Intermediate | codex "Generate docstrings for all public functions in project/" | |
codex forkCreate a forked copy of the current project for isolated development | Intermediate | codex fork | |
codex fork-resetReset fork branch state, discarding all local changes | Intermediate | codex fork-reset | |
codex pr-createCreate a Pull Request from current changes | Expert | codex pr-create | |
codex reviewAI-powered review of current code changes with suggestions | Intermediate | codex review | |
codex testRun the project test suite | Intermediate | codex test | |
codex explainExplain the functionality and logic of specified files or code snippets | Basic | codex explain "Explain the authentication flow in src/auth.ts" |
Configuration & Environment(5)
| Command | Level | ||
|---|---|---|---|
codex --model o4-miniSpecify a particular model | Intermediate | codex --model o4-mini | |
export OPENAI_API_KEY=sk-xxx && codexAuthenticate with API Key | Basic | export OPENAI_API_KEY="sk-xxx" && codex | |
CODEX.mdProject-level config file (project root) | Expert | # Create CODEX.md in your project root with instructions and context | |
codex --model o3Run with o3 model | Intermediate | codex --model o3 | |
codex config showView current Codex CLI configuration | Basic | codex config show |