Created
March 30, 2026 21:04
-
-
Save iandunn/dc91bba0541f5545cc973fa901808fdc to your computer and use it in GitHub Desktop.
Open Claude Code at project root
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Force Claude to open in the root folder of a project | |
| # | |
| # Claude annoying treats the folder it was launched from as the project root. If the root is ~/local-sites/core/ | |
| # and that's where the CLAUDE.md file is, but you launch claude from ~/local-sites/core/app/public/wp-content or | |
| # ~/local-sites/core/app/public/wp-content/mu-plugins, then it'll created .claude/settings.local.json files in | |
| # those subdirectories, and have separate conversation history from each other and from the project root. | |
| # | |
| # This scans for a CLAUDE.md at the project root and launches claude from there. $HOME also has a global CLAUDE.md, | |
| # though, so this stops before that, to avoid $HOME being treated as a project root. | |
| # | |
| # This assumes that all projects should have a CLAUDE.md file at the root folder that identifies it as a project | |
| # and provides project-specific instructions. | |
| function claude() { | |
| if [[ -f "$PWD/CLAUDE.md" ]]; then | |
| command claude | |
| return | |
| fi | |
| local dir="$PWD" | |
| local root="" | |
| # Walk up to find CLAUDE.md, stopping before $HOME to avoid the global one | |
| while [[ "$dir" != "$HOME" && "$dir" != "/" ]]; do | |
| if [[ -f "$dir/CLAUDE.md" ]]; then | |
| root="$dir" | |
| break | |
| fi | |
| dir="$(dirname "$dir")" | |
| done | |
| if [[ -n "$root" ]]; then | |
| printf "\n⚠️ Project root found at $root, launching from that folder\n\n" | |
| cd "$root" && command claude "$@" | |
| else | |
| printf "\n⚠️ No project root found, launching from current folder\n\n" | |
| command claude "$@" | |
| fi | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a Bash function to wrap the
claudecommand.To use it:
~/.bashrcfilesource ~/.bashrcto re-load the fileCLAUDE.mdfile to the root of your project. For me, I like to use/path/to/siteinstead of/path/to/site/app/public/wp-content, so that Claude has access to logs, SQL backups, etc. This also lets me customize the instructions, rather than having it in version control and shared by everyone. There can be advantages to that too, though.claudeto launch it from anywhere. If you're in a subdirectory of the project, it'll launch claude from the root insteadIf you want to merge conversations from subfolders up to the project root, you can go to
~/.claude/projectsand just move the files one one folder to the other