Skip to content

Instantly share code, notes, and snippets.

@iandunn
Created March 30, 2026 21:04
Show Gist options
  • Select an option

  • Save iandunn/dc91bba0541f5545cc973fa901808fdc to your computer and use it in GitHub Desktop.

Select an option

Save iandunn/dc91bba0541f5545cc973fa901808fdc to your computer and use it in GitHub Desktop.
Open Claude Code at project root
# 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
}
@iandunn

iandunn commented Mar 30, 2026

Copy link
Copy Markdown
Author

This is a Bash function to wrap the claude command.

To use it:

  1. Add it to your ~/.bashrc file
  2. source ~/.bashrc to re-load the file
  3. Add a CLAUDE.md file to the root of your project. For me, I like to use /path/to/site instead 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.
  4. Type claude to launch it from anywhere. If you're in a subdirectory of the project, it'll launch claude from the root instead

If you want to merge conversations from subfolders up to the project root, you can go to ~/.claude/projects and just move the files one one folder to the other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment