平时用 Codex 写代码、看项目时顺手攒的一些提示词和命令行用法,按场景整理一下方便查阅。

常用提示词

全局分析

  • scan the entire repo and suggest improvements
    扫描整个仓库,给出架构建议,排查性能和安全问题。
  • analyze the entire repository, find bugs, security issues, and performance problems, then suggest and apply fixes
    分析代码结构,找出 bug、潜在安全漏洞和性能问题,并直接给出或应用修复。
  • explain the architecture of this project in detail
    说明项目目录结构、各模块职责以及主要的代码调用关系。

读代码

  • trace how a request flows from the entry point to the database, list every file it touches
    从入口一路追到数据库,列出经过的每个文件,接手陌生项目时很实用。
  • where is X defined and where is it used?
    查某个函数、变量或配置的定义位置和所有调用点。
  • explain this file line by line for a beginner
    逐行解释,看不太熟的算法或底层代码时用。

改代码

  • implement X, keep the existing code style, do not change unrelated files
    明确限制不要动无关文件,能减少很多多余改动。
  • refactor this function to be more readable without changing behavior, then run the tests
    重构但不改外部行为,改完顺手跑测试。
  • this error happens when I run xxx: (贴报错). find the root cause before fixing
    让它先定位根因再动手,防止直接套一层 try-except 把报错吞掉。

测试与收尾

  • write unit tests for this module, cover edge cases
    补单元测试,顺便覆盖边界情况。
  • run the project and tell me whether it actually works
    让它实际跑一遍验证,别只看静态代码。
  • summarize what you changed and why, list anything you are unsure about
    改完列出改动原因和不确定的点,方便自己 review。
  • write a commit message for these changes
    根据当前改动生成 commit message。

Codex CLI 基本用法

npm install -g @openai/codex   # 安装
codex                          # 在当前目录进入交互模式
codex "解释这个项目的结构"      # 带着第一条指令启动
codex exec "跑一遍测试并修复失败的用例"   # 非交互模式,跑完就退出,适合写进脚本
codex resume                   # 接着上一次的会话

注意要在项目根目录启动,它默认的操作范围就是当前目录。

交互模式斜杠命令

命令作用
/init扫描项目,生成一份 AGENTS.md
/model切换模型和推理强度
/approvals设置权限:改文件、跑命令前要不要先确认
/diff查看这一轮改了哪些代码
/status查看当前会话配置和用量
/compact压缩上下文,对话太长时用
/new开新会话

AGENTS.md

放在项目根目录的说明文件,Codex 启动时会自动读取。平时把固定规矩写进去,省得每次对话重复提:

# AGENTS.md
- 用中文回复
- 包管理用 npm,不要用 yarn
- 改完代码后运行 `npm run lint` 和 `npm test`
- 不要修改 `data/` 目录下的文件
- 提交信息用中文,格式:类型: 说明

使用注意点

  • 一次尽量只交代一件事。把“修 bug + 加功能 + 重构”混在一起提,出问题时很难排查是哪步改坏的。
  • 动手前先用 Git 提交一次,改乱了直接 git checkout . 回退。
  • 验收标准说具体一点,比如“测试全部通过”或“页面能正常打开”,不然它可能改到一半就停了。
  • 中英文提示词效果差别不大,但报错堆栈、文件名和标识符最好原样贴过去,别自己翻译。
  • 它说“已修复”之后,自己跑一遍确认最稳妥。

觉得有用的话,点个赞