分享一个自用powershell环境codex系统提示词片段,很好的解决了经常出错的问题

liu juntao 2026-07-09 15:11 1

如题,之前一直是WSL2环境使用Codex,流畅是流畅,但很占内存,而且毕竟不是win原生的文件系统,想通过目录直接打开文件夹,或者是编辑器的时候有点不太方便。


所以最近一直在尝试切换到原生的Windows,使用PowerShell命令。用了一阵子,现在也挺丝滑流畅的。


给大家分享一个我自用的系统提示词,能够减少PowerShell的报错,并且提升执行速度。


整体思路:




  1. 默认用 non-login shell,避免加载用户 profile 带来的别名、提示符集成、环境污染。只有明确依赖用户 shell 配置时才用 login/profile shell。




  2. Windows 上优先使用 PowerShell 原生命令,文件和环境操作优先用 PowerShell cmdlet。显式启动 PowerShell 时默认加 -NoProfile。




  3. 不混用 Bash 和 PowerShell 语法。




  4. 多行脚本加 $ErrorActionPreference = ‘Stop’,防止失败后继续执行。




  5. 写文本文件时指定 -Encoding UTF8。




  6. 结构化数据处理优先用对象管道,而不是脆弱的字符串切割。




  7. 搜索要高效且低噪声,大范围搜索优先用 rg / rg --files,搜索范围从尽可能窄的目录开始。

    大目录搜索时排除 .gitnode_modules缓存 等高噪声目录。复杂正则用 rg --pcre2,固定字符串用 rg -F。




放在 ~/.codex/AGENTS.md 下即可。


## PowerShell Guidelines

- On Windows, prefer PowerShell commands and PowerShell-native cmdlets when interacting with the filesystem or environment.
- When explicitly launching PowerShell from another command, use `powershell -NoProfile` or `pwsh -NoProfile` by default to avoid user profile scripts, prompt integrations, aliases, and shell customizations. Omit `-NoProfile` only when the command intentionally depends on the user's PowerShell profile.
- Do not mix Bash syntax with PowerShell syntax in the same command or script.
- For multi-line PowerShell scripts, set `$ErrorActionPreference = 'Stop'` near the top so failures do not continue silently.
- When creating or writing text files from PowerShell, specify `-Encoding UTF8` when the cmdlet supports it.
- Prefer PowerShell object pipelines such as `Select-Object`, `Where-Object`, and `ForEach-Object` for structured data handling instead of fragile text slicing.

## Search Guidelines

- For broad file or text searches, prefer `rg` / `rg --files` over recursive `Get-ChildItem`. Start from the narrowest known directory. When searching large trees such as the user home directory, exclude high-noise directories like `.git`, `node_modules`, virtualenv folders, and package caches, and use `--hidden` only when hidden directories are relevant.
- Use `rg --pcre2` for regex look-around: `(?!...)`, `(?=...)`, `(?<!...)`, `(?<=...)`.
- In PowerShell, quote complex `rg` patterns with single quotes and escape `'` as `''`.
- Use `rg -F` for fixed strings; test complex commands before sharing.

最新回复 (10)
  • eman 07-09 15:36
    1

    不错不错,谢谢佬分享这么实用的东西

  • HipHopMan 07-09 15:53
    2

    先^-^后看,非常不错,感谢佬的分享

  • Fengshi 07-09 16:19
    3

    powershell的问题升级powershell7更好,但是要确认codex调起的确实是powershell7。

    文件检索我推荐用codegraph,速度和token用量都会更好。

    直接塞提示词操作简单,但是token消耗会高一些。

  • 魔幻之地 07-09 16:26
    4

    感谢分享,我升级powershell7没怎么碰上错误,还是加上了这个看看

  • yangjiamuzi 07-09 20:36
    5

    感谢佬的分享,不管其他先配上试试

  • paddington 07-10 11:20
    6

    太棒了 每次分享的东西都很实用 嘻嘻嘻嘻嘻

  • Katoyu 07-11 09:57
    7

    感谢大佬分享,已严肃复制准备实操看看效果

  • wang8890 07-11 10:32
    8

    感谢分享,确实看codex cli执行的时候,各种power shell报错。按佬们说的,先安装了power shell 7。然后添加了提示词

  • 孙文昌 07-11 11:01
    9



    大佬,我打开codex就会出现白屏,这个怎么解决?

  • liu juntao 楼主 07-11 11:08
    10

    这个应该是启动慢吧,特别是初次启动,就一直等着应该能好。

* 帖子来源Linux.do
返回