Claude Code CLI Guide: Command-Line Options and Slash Commands
Learn how to install and configure Claude Code, use its command-line options and slash commands, manage conversation context, and apply practical safety and troubleshooting practices. Examples show how to use natural-language prompts for code review, test debugging, and refactoring.
As large language models move into the terminal, developers can ask AI tools to read and edit code or run tests directly from the command line. Unlike IDE extensions, command-line tools are not tied to a particular editor. They can run on servers, in containers, and on remote machines, and can also be incorporated into scripts and CI pipelines.
This guide covers the basics of Claude Code. Slash commands and command-line options can change between versions, so consult the official documentation when in doubt. You can also check the commands available in your installed version with claude --help and /help.
1. Installation and First-Time Setup
Install Claude Code by following the official documentation. A common option is a global installation through npm. Before installing, confirm that your Node.js version meets the requirements.
Once installed, run claude from your project directory. On first launch, you’ll be asked to sign in. You can authenticate in either of two ways:
- Enter
/loginin the interactive interface and follow the prompts. - Configure an API key using an environment variable, rather than placing the key in source code or a configuration file.
Before using the tool, understand its permission model. Claude Code will typically ask for confirmation before modifying files or running shell commands. Start it only in repositories you trust, and avoid letting it freely read or write in directories that contain secrets or production configuration.
2. Command-Line Options: From One-Off Prompts to Resuming Sessions
Command-line options determine how Claude Code runs. These four are among the most commonly used:
-p— Non-interactive output: Run a single prompt, print the result, and exit. This is useful in scripts and CI.cat data.json | claude -p "Format this JSON with two-space indentation and output only the result"
Non-interactive mode may still trigger file changes or command execution. In a pipeline, configure permissions and restrict the available tools to reduce the risk of unintended changes when no one is supervising the run.
-c— Continue the most recent conversation: Resume the latest session in the current directory. This is useful when returning to work after closing a terminal, since you don’t need to describe the background again.-r— Resume a previous conversation: Select from earlier sessions. This can help with longer projects, such as a migration completed over several days.--model— Choose a model: Select a model when starting Claude Code. A lightweight model may be sufficient for simple syntax questions; consider a more capable model for architecture work or complex refactoring.
3. Slash Commands: Sessions, Diagnostics, and Accounts
In the interactive interface, enter / to access system-level commands. The commands below are grouped by purpose.
Sessions and Context
/init analyzes the project and creates a project guidance file, such as CLAUDE.md, where you can record build commands, directory structure, and coding conventions. Claude Code reads this information in future sessions. The command does not clear the current conversation. To reset the conversation, use /clear or start a new terminal session.
/clear clears the current conversation history. It can be faster than quitting and reopening Claude Code when the AI is stuck on an incorrect approach or the discussion has drifted. The trade-off is that all conversation context is lost. Before clearing, save any conclusions you still need in a file.
/compact compresses the conversation history into a summary to free up context while retaining the main points. This is a lossy process: the summary may omit details, such as a specific error message or a rejected approach. Review the summary after compacting, and record important design decisions in CLAUDE.md or other project documentation.
Status and Diagnostics
/status displays information such as the current model, working directory, and account, so you can check that the environment is set up as expected.
/doctor checks the installation, network connectivity, and configuration. If the tool behaves unexpectedly, a command cannot be found, or an environment variable does not take effect, run this command to help identify the problem.
/help lists the available commands. Use it when you forget a command’s spelling or want to check what your version supports.
Account and Model
/login and /logout let you switch accounts or authenticate again. On a shared development machine, log out when you’re finished.
/model switches models during a conversation. It has the same purpose as --model, but applies to the current session.
/cost displays usage and cost for the current session. If your account is billed based on API usage, keep an eye on this information. The details shown may differ for subscription users; check your account page for the applicable information.
4. Choosing Between /clear and /compact
Both commands make a conversation shorter, but they serve different purposes. Use /clear when a task is finished and the next one is unrelated. Use /compact when a task is still in progress and the conversation contains useful decisions or background. You can specify what to preserve, for example:
/compact Preserve the database migration rollback plan
| Command | Purpose | Main risk | Alternative |
|---|---|---|---|
/clear |
Clears conversation history | Unsaved conclusions are lost | Save conclusions to a file first |
/compact |
Summarizes conversation history | Details may be omitted | Review the summary and add key information manually |
Start a new terminal and use -c |
Retains the most recent session | Old context may interfere with a new task | Separate work by task and project directory |
5. Practical Natural-Language Prompts
Claude Code accepts requests written in natural language, so you don’t need to memorize a fixed syntax. Here are three common types of task.
Understand a codebase
When joining an unfamiliar project, you can ask:
Read the project's README and deployment scripts. Summarize the deployment steps and list the environment variables that need to be prepared in advance.
The tool will read relevant files and organize the information. Check that it is reading files from the current repository, not files with the same names in another directory.
Investigate failing tests
Describe the task and the constraints clearly:
Run npm test, identify the failing tests, and analyze the causes. First propose a fix without changing any code. After I confirm, make the changes and run the tests again.
Asking for a proposal before edits can help prevent the AI from changing code before it understands the problem.
Refactor a module
Break large refactoring tasks into smaller steps:
Extract repeated database queries in src/services into a repository layer. For the first step, output only the directory structure and interface definitions. Do not modify existing files.
Review the diff after each step and proceed only when the changes look correct.
6. Common Errors and Troubleshooting
- Command not found: The installation may be incomplete or the executable may not be on
PATH. Restart the terminal or run/doctorto check the installation. - Not signed in or authentication failed: Run
/loginagain, or check that the API key environment variable is correct and has taken effect. - Model unavailable: Your account or plan may not include the selected model. Use
/modelto switch to an available model. - Context limit reached: Long conversations can slow responses or reduce their quality. Try
/compactfirst; if that is not enough, use/clear. - Conversation cleared by mistake: A conversation cannot be recovered after
/clear. Make a habit of saving important conclusions before clearing the history.
7. Recommended Practices
- State your intent in clear, separate points. Include the goal, constraints, and desired output format. More specific requests are more likely to produce relevant results.
- Ask the tool to read the relevant files and tests before asking it to make changes. More context helps it follow the project’s conventions.
- Put long-term project rules in
CLAUDE.md, such as test commands, coding style, and directories that must not be changed. This avoids repeating the same instructions in every session. - Review operations that modify files or run commands. Inspect each diff before deciding whether to accept the changes.
The main benefit of a command-line assistant is reducing repeated input and the effort of rebuilding context. The time saved depends on the task and your working habits, so assess the results on real projects over a week or two.
