CLI Basics
Install with npm, launch with the claude command. Use flags to customize behavior, slash commands to control the session, and keyboard shortcuts for speed.
How It Flows
Here is the journey from zero to productive. One install, one command, and you are off to the races.
Explain Like I'm 12
claude to start talking to it. From there, you just type what you want in plain English.
Installation
You need two things before you start: Node.js 18 or later and an Anthropic API key. If you have got those, the install is a single command.
npm install -g @anthropic-ai/claude-code
That is it. The -g flag means "install globally" so the claude command is available everywhere on your system, not just in one project folder.
Next, set your API key. You can do this with an environment variable:
export ANTHROPIC_API_KEY=sk-ant-...
Or add it to your shell profile (~/.bashrc, ~/.zshrc, etc.) so it persists across terminal sessions. Claude Code will pick it up automatically.
To verify everything works:
claude --version
Starting a Session
There are three ways to start Claude Code. Pick the one that fits the moment.
Interactive mode — the default. You get a back-and-forth conversation, like a chat in your terminal.
claude
One-shot mode — pass a prompt directly. Claude answers, then exits. Great for quick questions.
claude "explain what this project does"
Pipe mode — feed input from another command. Perfect for scripting and automation.
cat error.log | claude -p "explain these errors"
Think of interactive mode as a phone call, one-shot as a text message, and pipe mode as a voicemail you leave for Claude to process.
Key Flags
Flags let you tweak Claude's behavior without touching any config files. Here are the ones you will use most.
| Flag | What It Does | Example |
|---|---|---|
--model |
Choose which Claude model to use | claude --model claude-sonnet-4-20250514 |
--allowedTools |
Restrict which tools Claude can use | claude --allowedTools "Read,Write" |
--max-turns |
Limit how many turns Claude takes | claude --max-turns 5 |
--output-format |
Set output format (text, json, stream-json) | claude -p --output-format json "..." |
-p |
Pipe mode — read from stdin, print to stdout | echo "hi" | claude -p |
--verbose |
Show detailed logging for debugging | claude --verbose |
You can combine flags freely. For instance, pipe mode with JSON output and a turn limit is perfect for CI pipelines:
claude -p --output-format json --max-turns 3 "find unused imports"
Slash Commands
Once you are inside an interactive session, slash commands give you quick controls. Type them at the prompt instead of a regular message.
| Command | What It Does |
|---|---|
/help |
Show all available commands and usage info |
/clear |
Wipe the conversation and start fresh |
/compact |
Compress the conversation to save context window space |
/model |
Switch to a different Claude model mid-session |
/cost |
Show how many tokens and dollars this session has used |
/permissions |
View and change tool permission settings |
/memory |
Open and edit your memory files |
The most useful one early on is /compact. Long conversations eat up your context window. When things start feeling slow or Claude loses track of earlier details, hit /compact to compress everything down. It is like clearing your desk so you can think more clearly.
Keyboard Shortcuts
These work inside the interactive session. They are not fancy, but they save you time every single day.
| Shortcut | What It Does |
|---|---|
| Escape | Cancel the current generation (Claude stops typing) |
| Ctrl+C | Interrupt the current operation |
| Tab | Autocomplete file paths and slash commands |
| Up Arrow | Cycle through previous messages |
The Escape key is your best friend. If Claude starts heading in the wrong direction, hit Escape immediately. You do not have to wait for it to finish. Cancel, rephrase your prompt, and try again. Fast iteration beats waiting for a wrong answer.
Tips for Beginners
Practical Tips to Get Started
- Start with small tasks. Ask Claude to explain a function, write a test, or fix a typo. Build confidence before tackling big refactors.
- Be specific in your prompts. "Fix the bug in auth" is vague. "Fix the null pointer exception in
auth/login.tsline 42" gives Claude a clear target. - Use
/compactoften. Long conversations degrade quality. Compress regularly to keep Claude sharp. - Check the diff before accepting. Claude shows you exactly what it changed. Read it. This is your code, and you are the final reviewer.
Test Yourself
Q: How do you install Claude Code?
npm install -g @anthropic-ai/claude-codeQ: What command starts Claude Code?
claudeQ: How do you send a one-shot prompt?
claude "your prompt here"Q: What does /compact do?