Add a codex shell function to ~/.zshrc (or ~/.bashrc) that:
- Always runs codex in full-auto / yolo mode (
--dangerously-bypass-approvals-and-sandbox) - Supports a
-w [name]flag that creates a git worktree at~/.worktrees/<repo-name>/<name>from the current HEAD, then opens codex inside it. If no name is given, auto-generates one from the current timestamp (e.g.codex-20260307-143022).
Add this to your shell config:
# Codex with yolo mode and optional worktree support
codex() {
local worktree_dir=""
local pass_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-w|--worktree)
shift
if [[ -z "$1" || "$1" == -* ]]; then
worktree_dir="codex-$(date +%Y%m%d-%H%M%S)"
else
worktree_dir="$1"
shift
fi
;;
*)
pass_args+=("$1")
shift
;;
esac
done
if [[ -n "$worktree_dir" ]]; then
local repo_name
repo_name=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)") || repo_name="default"
local target="$HOME/.worktrees/$repo_name/$worktree_dir"
if [[ ! -d "$target" ]]; then
mkdir -p "$(dirname "$target")"
git worktree add -b "$worktree_dir" "$target"
fi
command codex --dangerously-bypass-approvals-and-sandbox -C "$target" "${pass_args[@]}"
else
command codex --dangerously-bypass-approvals-and-sandbox "${pass_args[@]}"
fi
}Usage examples:
codex— open codex in yolo mode in current dircodex "fix the auth bug"— open with initial promptcodex -w— create worktree with auto name, open codex therecodex -w my-feature— create worktree at ~/.worktrees//my-featurecodex -w my-feature "prompt"— worktree + initial prompt
Notes:
- The worktree is scoped under the current repo name, e.g. running from
morphite-aiwith-w my-featurecreates~/.worktrees/morphite-ai/my-feature. - The worktree branches from current HEAD. To always branch from main, change
git worktree add -b "$worktree_dir" "$target"togit worktree add -b "$worktree_dir" "$target" main - If the target dir already exists, worktree creation is skipped and codex opens as-is.
- Codex must be installed: https://github.com/openai/codex