Claude Code CLI Cheat Sheet & Guide
In this article
This cheat sheet covers the Claude Code command-line interface (CLI) for working with Anthropic's Claude models directly in your terminal. You'll learn the essential commands for installation, configuration, common workflows, and troubleshooting, so you can start using Claude Code effectively right away.
What is Claude Code?
Claude Code is Anthropic's official command-line tool that lets you interact with Claude models (like Opus, Sonnet, and Haiku) from your terminal. It's designed for developers who want to integrate AI assistance into their coding workflow without leaving the command line. Claude Code can read and edit files, run terminal commands, and answer questions about your codebase, making it a powerful pair programmer.
Unlike a simple API wrapper, Claude Code is an agentic tool: it can take multi-step actions, such as searching through your project, making changes across multiple files, and running tests to verify its work. It's particularly useful for tasks like refactoring, debugging, and writing boilerplate code.
Installation and First Setup
To install Claude Code, you need Node.js (version 18 or higher) and npm. The installation is done via npm, and the tool is available for macOS, Linux, and Windows (through WSL).
- Install globally: Run
npm install -g @anthropic-ai/claude-codein your terminal. - Verify installation: Check the version with
claude --version. - Authenticate: Run
claudefor the first time. You'll be prompted to log in with your Anthropic account or API key. For API key authentication, set the environment variableANTHROPIC_API_KEY. - Start a session: Simply type
claudein any directory to start an interactive session. The tool will detect your project context and be ready for your prompts.
If you prefer to use Claude Code without installing it globally, you can run it via npx @anthropic-ai/claude-code, which will fetch the latest version on the fly.
Core Commands and Flags
Claude Code has a set of primary commands that you'll use most often. Here's a breakdown of the most useful ones:
claude– Starts an interactive session in the current directory.claude "prompt"– Runs a single prompt in non-interactive mode and prints the response. For example:claude "Explain the code in main.py"claude -p "prompt"– Same as above, but explicitly for print mode (useful in scripts).claude --continue– Continues the most recent conversation from the last session.claude --resume– Shows a list of previous sessions to resume from.claude --model sonnet– Specifies which Claude model to use (e.g.,opus,sonnet,haiku). Default is often the latest Opus model.claude --output-format json– Outputs the response in JSON format, useful for scripting.claude --verbose– Shows detailed logs of what Claude is doing (tool calls, file reads, etc.).claude --help– Displays all available commands and flags.
For a full list of options, run claude --help in your terminal. The CLI also supports a --dangerously-skip-permissions flag to bypass permission prompts, but use it with caution as it gives Claude full access to your system.
Working with Files and Tools
Claude Code can interact with your filesystem and execute commands. Here's how to make the most of it:
- File editing: Claude can read, create, and modify files. For example, you can ask: "Add error handling to the fetch function in api.js" and it will make the changes directly.
- Running commands: Claude can run terminal commands to test your code, install dependencies, or check git status. It will ask for permission before running each command unless you've granted auto-approval.
- Searching: Use the built-in search tools to find text or files. You can ask "Where is the function that validates email addresses?" and Claude will search your project.
- Multi-file changes: Claude can handle changes across multiple files. For instance: "Rename the variable
usertoaccountin all files in the src folder."
To grant permissions more efficiently, you can use the --allowedTools flag to specify which tools (like Bash, Read, Write) Claude can use without asking. For example: claude --allowedTools "Read,Write".
Configuration and Customization
Claude Code can be customized to fit your workflow. Configuration is done via a settings.json file in your project's .claude directory, or globally in your home directory.
- Model selection: Set a default model in settings with
"model": "sonnet". - Permissions: Pre-approve tools by adding them to
"permissions": {"allow": ["Bash", "Read"]}. - System prompt: Add a custom system prompt to guide Claude's behavior. For example, "Always write unit tests for new functions."
- Environment variables: Set
ANTHROPIC_MODELto change the default model, orANTHROPIC_API_KEYfor authentication. - CLAUDE.md file: Create a
CLAUDE.mdfile in your project root. Claude Code will automatically read this file at the start of each session, making it ideal for project-specific instructions, coding standards, or common commands.
To see your current configuration, run claude config in any directory.
Common Workflows and Tips
Here are some practical ways to use Claude Code in your daily work:
- Code review: Ask Claude to review your recent changes: "Review the diff in the last commit and suggest improvements."
- Debugging: Paste an error message and ask: "What's causing this error and how do I fix it?" Claude can also run your tests to reproduce the issue.
- Refactoring: "Split this large function into smaller, more readable functions" – Claude will handle the edits and run tests to ensure nothing breaks.
- Writing tests: "Write unit tests for the
validatefunction using Jest" – Claude will create the test file and explain its approach. - Documentation: "Generate a README for this project based on the code" – Claude can summarize your project structure and usage.
- Learning: Use
claude "explain this code"to get a line-by-line explanation of a file or function.
For long-running tasks, consider using --output-format json and redirecting to a file to avoid terminal clutter. Also, use --continue to pick up where you left off after a break.
Troubleshooting and Limitations
Even with a smooth tool, you might run into issues. Here are common problems and their solutions:
- Authentication errors: If you get a 401 error, your API key may be invalid or expired. Re-run
claudeand log in again, or update yourANTHROPIC_API_KEY. - Node version too old: Claude Code requires Node 18+. Check with
node --version. If it's older, update Node via nvm or your package manager. - Command not found: If
claudeisn't recognized, ensure npm's global bin directory is in your PATH. On macOS/Linux, this is usually/usr/local/binor~/.npm-global/bin. - Slow responses: The default model may be slow on large contexts. Try using a faster model like
--model haikufor simple tasks. - Permission prompts getting annoying: Use
--allowedToolsor configure permissions in settings to auto-approve common tools likeReadandWrite.
Claude Code has limitations: it can't access the internet unless you give it a tool to do so, and it may sometimes produce incorrect code, so always review its changes. It also has a token limit per conversation, so very long sessions might need to be split into multiple --continue sessions.
Frequently Asked Questions
How do I update Claude Code?
Simply run npm update -g @anthropic-ai/claude-code to get the latest version. You can check your current version with claude --version.
Can I use Claude Code without an API key?
No, you need an Anthropic account and either a paid API key or a subscription that includes CLI access. The tool does not work without authentication.
Does Claude Code work on Windows?
Yes, but it's recommended to use Windows Subsystem for Linux (WSL) for the best experience, as some tools and scripts may assume a Unix-like environment.
How do I stop Claude from running a command?
You can press Ctrl+C to interrupt the current action. If you want to deny permission for a specific tool, just type 'no' when prompted.
Can I use Claude Code with other Anthropic models?
Yes, you can switch between Opus, Sonnet, and Haiku using the --model flag or by setting the ANTHROPIC_MODEL environment variable.
Is Claude Code free?
Claude Code requires a paid Anthropic API plan or a Claude Pro/Max subscription that includes CLI access. There is no free tier for the CLI itself.