🦾 从新手到 Codex CLI 老司机:6 个让你效率翻倍的硬核技巧

166 阅读4分钟

在这里插入图片描述


🎯 先说结论:Codex CLI 不是“高级版 ChatGPT”,而是你的 AI 结对程序员

  • 它能读你整个项目上下文 ✅
  • 它能自动 commit / push ✅
  • 它能并行跑多个任务 ✅
  • 它甚至会在你写 bug 前就喊:“停!这个 null 会炸!” ✅

但前提是——你会用它
下面这 6 个技巧,将帮你从 codex 新手 → codex pro,省下至少 3 小时/天。


🖼️ 1. 复制粘贴图片?终端也行!

🤯 场景:

  • 你截图了一个报错弹窗
  • 或设计稿里一个组件
  • 或手机拍的白板草图
    → 想让 Codex 直接“看图写代码”?

✅ 操作三步走:

步骤操作快捷键(macOS)
1️⃣ 截图到剪贴板框选区域截图,不保存文件⌃ Ctrl + ⌘ Cmd + ⇧ Shift + 4
2️⃣ 进入 Codex CLIcodex
3️⃣ 粘贴图片直接 ⌃ Ctrl + V✅ 支持 PNG/JPEG
codex> Please implement this UI component from the screenshot.
       [🖼️ Image pasted]
codex> 🖼️ Received image (1280×720). Analyzing…
       → Detected: React + Tailwind, dark mode, sidebar layout…
       → Generating code…

💡 原理:Codex CLI 支持 multipart/form-data 输入,自动 Base64 编码上传。 在这里插入图片描述


🔍 2. /review:你的专属“代码安检仪”

🧨 真实痛点:

你写了 200 行重构代码,git push 后 → CI 红了 → SonarQube 报 7 个 critical bug → 同事 PR 评论:“这个 NPE 会炸线上”。

✅ 解法:/review + please fix 循环

codex> /review
codex> 📋 Code Review Summary (gpt-5-codex, high reasoning):
       - ⚠️ CRITICAL: `user.profile?.name` may be null in line 42 (profile not guaranteed)
       - ⚠️ HIGH: Missing `await` on `cache.flush()` in async handler (race condition)
       - ✅ No style violations detected.

codex> please fix the null safety issue
codex> ✅ Applied fix:
       - Added null check + fallback
       - Added unit test case: `test_user_profile_null`
       Ready for re-review? [Y/n]

🎯 关键优势

  • 不像 Linter 那样报 100 条“缩进不对”
  • 只聚焦 1~2 个真正会炸的 bug
  • 支持“修复 → 再 review”闭环,直到绿灯 ✅ 在这里插入图片描述

🧩 3. 分阶段 checklist:让 Codex 工作 1 小时不宕机!

🤖 问题:

你让 Codex “实现用户登录功能”,它 3 分钟后回:“done”,结果只写了前端表单,没碰后端、没加测试、没改 DB。

✅ 正确姿势:先让它写计划 → 再分阶段执行

🔤 Prompt 模板(直接复制用):

go ahead and do the following:
1. investigate how auth is currently implemented in this codebase
2. think about how to add OAuth2 (Google/GitHub) support
3. create a sectioned checklist to implement it, where each section:
   - is a self-contained phase
   - can be committed separately
   - is prioritized by risk/importance
then go ahead and implement each phase. commit and push after a phase is done before you move to the next.

📋 Codex 生成的 checklist 示例:

## ✅ Phase 1: Audit Existing Auth — `git commit -m "chore: audit auth flow"`
- [x] Review `auth.service.ts`, `jwt.middleware.js`
- [x] Identify session store (Redis? Cookie?)

## 🛠️ Phase 2: Add OAuth Provider Interface — `git commit -m "feat: add OAuthProvider interface"`
- [ ] Define `IOAuthProvider` with `login()`, `callback()`
- [ ] Implement `GoogleOAuthProvider`
- [ ] Unit tests

## 🔐 Phase 3: Secure Token Exchange — `git commit -m "feat: secure token exchange"`
- [ ] Add PKCE flow
- [ ] Validate `state` param
- [ ] E2E test with mock OAuth server
...

💡 结果:Codex 分阶段 commit + push,你每 10 分钟就能 git log 看进度,不怕它“跑飞”。


⚙️ 4. 永不掉线的 Codex:alias + resume 组合拳

🛠️ 推荐 .bashrc / .zshrc 配置:

# 默认高配模式:gpt-5-codex + 高推理 + 全权限 + 联网
alias c="codex -m gpt-5-codex -c model_reasoning_effort='high' --search --yolo"

# 快速恢复上一个会话(手滑 Ctrl+C 也不怕)
alias cr="codex resume"

💬 效果:

$ c
codex> 🚀 Session started (gpt-5-codex, high reasoning, web enabled)
       Working on: user-auth-oauth.feature

$ cr  # 假设刚才意外退出
codex> 🔙 Resuming session ‘user-auth-oauth’...
       Last action: wrote GoogleOAuthProvider.ts
       Next: implement callback handler

🔐 --yolo 是什么?
官方彩蛋 flag 😄:Y ou’re O n L ife O rder — 即“允许执行危险操作”(如 rm -rf, git push --force),慎用!


☁️ 5. codex cloud:把小 bug 扔给“云队友”

🧠 策略:主战场(CLI)+ 分战场(Cloud)并行开发

任务类型推荐方式理由
核心 feature(>2 小时)codex CLI上下文全、可持久、支持 worktree
小 bug / 文档 / 测试codex cloud快速 offload,不打断 flow

✅ 用法:

# 在另一个 terminal:
$ codex cloud
codex cloud> Fix the typo in README.md and add a missing example for /review
codex cloud> ✅ Pushed fix to branch `fix/readme-typo`
             → PR created: https://github.com/you/project/pull/138

🎯 最佳拍档:搭配 Preview Environment(如 Vercel / Netlify),自动部署 → 直接点链接验收 ✅


🌳 6. 终极秘技:git worktree + 多 Codex 实例并行

🤯 痛点:

你想同时让 Codex:

  • 写新功能(feat/payment
  • 修紧急 bug(hotfix/npe-user
    → 但它们共享同一工作区,互相覆盖文件

✅ 解法:用 git worktree 开“分身”

# 主功能:在 ../payment 分支开发
git worktree add ../payment -b feat/payment origin/main

# 紧急修复:在 ../hotfix 分支开发
git worktree add ../hotfix -b hotfix/npe-user origin/main
# Terminal 1: 主功能开发
cd ../payment
c  # codex CLI 专注 feature

# Terminal 2: 紧急修复
cd ../hotfix
c  # 另一个 codex CLI 专注 hotfix

✅ 关键优势:

  • 每个 worktree 有独立 node_modules / .env
  • Codex 不会“看到”其他分支的临时文件
  • git add . 不会误提交隔壁分支的改动!