Open Claude Code in the project root directory

I’ve found it really annoying how Claude Code creates .claude/settings.local.json in every folder it’s opened from, rather than just at the root of a project.

So here’s a fix fix that. It’s a Bash function that wraps the claude command and calls it from the correct folder.

Code

# 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
}

Usage

  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

Merging scattered conversations

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

Leave a Reply

Your email address will not be published. Required fields are marked *