Skip to content

Instantly share code, notes, and snippets.

@tzachbon
Last active March 7, 2026 21:13
Show Gist options
  • Select an option

  • Save tzachbon/c2db7e258ed9eba8138cb71d6f9443a6 to your computer and use it in GitHub Desktop.

Select an option

Save tzachbon/c2db7e258ed9eba8138cb71d6f9443a6 to your computer and use it in GitHub Desktop.
Prompt: add codex yolo + worktree shell function

Add a codex shell function to ~/.zshrc (or ~/.bashrc) that:

  1. Always runs codex in full-auto / yolo mode (--dangerously-bypass-approvals-and-sandbox)
  2. 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 dir
  • codex "fix the auth bug" — open with initial prompt
  • codex -w — create worktree with auto name, open codex there
  • codex -w my-feature — create worktree at ~/.worktrees//my-feature
  • codex -w my-feature "prompt" — worktree + initial prompt

Notes:

  • The worktree is scoped under the current repo name, e.g. running from morphite-ai with -w my-feature creates ~/.worktrees/morphite-ai/my-feature.
  • The worktree branches from current HEAD. To always branch from main, change git worktree add -b "$worktree_dir" "$target" to git 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment