Skip to content
DocsUse AItoolsCLI Mastery
Chapter 7 of 15·tools·9 min read

CLI Mastery

Thành Thạo CLI

Advanced CLI usage, flags, and configuration options

Hover or tap any paragraph to see Vietnamese translation

CLI Basics

The Claude Code CLI is the primary interface for interacting with Claude. Understanding commands and options helps you work more efficiently.

Starting Claude

Terminal
1# Start interactive session in current directory2claude34# Start with an initial prompt5claude "explain this codebase"67# Start in a specific directory8claude --cwd /path/to/project910# Resume last session11claude --resume1213# Start with a specific model14claude --model opus

Important Flags

Model Selection

Claude Code supports multiple models with different characteristics. Choose the appropriate model for your task to optimize cost and quality.

Terminal
1# Use Opus (most capable, best for complex tasks)2claude --model opus34# Use Sonnet (balanced, good for most tasks)5claude --model sonnet67# Use Haiku (fastest, cheapest, good for simple tasks)8claude --model haiku910# Short aliases11claude -m opus12claude -m sonnet13claude -m haiku

Permission Modes

Control what Claude is allowed to do automatically. Different modes provide different levels of automation.

Terminal
1# Default mode - asks for permission on risky actions2claude34# Auto mode - automatically approve safe actions5claude --mode auto67# Plan mode - requires approval before execution8claude --mode plan910# Bypass mode - skip all permission prompts (use carefully!)11claude --dangerously-skip-permissions
Warning
Bypass mode is dangerous because Claude can perform any action without confirmation. Only use when you fully trust what Claude will do.

Output Control

Terminal
1# Print output in JSON format (for scripting)2claude --print "list all files" --output-format json34# Stream output (see responses as they generate)5claude --stream67# Quiet mode (minimal output)8claude -q "fix the bug"910# Verbose mode (detailed logging)11claude -v "explain this"

Slash Commands

In an interactive session, you can use slash commands to perform special tasks.

Slash Commands
1# Get help2/help34# Clear conversation history5/clear67# Exit the session8/exit910# Change model mid-session11/model opus12

Piping and Scripting

Claude Code can be integrated into shell scripts and pipelines to automate tasks.

Scripting
1# Pipe file content to Claude2cat error.log | claude "explain this error"34# Use in scripts5OUTPUT=$(claude --print "generate a random UUID")6echo $OUTPUT78# Process multiple files9for file in src/*.ts; do10  claude --print "review $file for bugs"11done1213# Combine with other tools14git diff | claude "write a commit message for these changes"1516# CI/CD integration17claude --print --mode auto "run all tests and fix failures"

Configuration Files

Claude Code reads configuration from multiple sources with a defined priority order.

Configuration
1# Configuration priority (highest to lowest):2# 1. Command line flags3# 2. Environment variables4# 3. Project .claude/settings.json5# 4. User ~/.claude/settings.json67# Example user settings8# ~/.claude/settings.json9{10  "model": "sonnet",11  "theme": "dark",12  "autoApprove": ["read", "glob", "grep"],

Environment Variables

Environment Variables
1# API key (required if not logged in)2export ANTHROPIC_API_KEY="sk-ant-..."34# Default model5export CLAUDE_MODEL="opus"67# Disable telemetry8export CLAUDE_DISABLE_TELEMETRY="1"910# Custom config directory11export CLAUDE_CONFIG_DIR="/path/to/config"1213# Debug mode14export CLAUDE_DEBUG="1"

Tips and Tricks

  • Use Ctrl+R to search through command history.
  • Add alias to shell profile: alias c='claude' for faster typing.
  • Use --resume to continue previous session, keeping context.
  • Combine with tmux to run multiple Claude sessions in parallel.
  • Use /fast to toggle between fast and detailed output.
Shell Aliases
1# Useful aliases for .bashrc/.zshrc2alias c="claude"3alias cr="claude --resume"4alias co="claude --model opus"5alias ch="claude --model haiku"6alias cq="claude -q"78# Quick explain function9explain() {10  cat "$1" | claude "explain this code"11}1213# Quick fix function14fix() {15  claude --mode auto "fix all errors in $1"16}

Key Takeaways

Điểm Chính

  • Use --model to select opus/sonnet/haiku based on task complexitySử dụng --model để chọn opus/sonnet/haiku tùy theo độ phức tạp
  • Permission modes control automation level (auto, plan, default)Permission modes kiểm soát mức độ tự động (auto, plan, default)
  • Slash commands provide quick actions in interactive modeSlash commands cung cấp các action nhanh trong chế độ tương tác
  • CLI integrates with scripts and pipelines for automationCLI tích hợp với scripts và pipelines để tự động hóa

Practice

Test your understanding of this chapter

Quiz

Which model is best for complex architectural decisions?

Model nào tốt nhất cho các quyết định kiến trúc phức tạp?

True or False

The --resume flag starts a new session with the same model.

Flag --resume bắt đầu session mới với cùng model.

Code Challenge

Complete the command to start Claude in auto-approve mode

Hoàn thành lệnh để khởi động Claude với chế độ auto-approve

claude --mode 
Quiz

What is the slash command to clear conversation history?

Slash command để xóa lịch sử hội thoại là gì?

← → to navigate chapters
Built: 4/8/2026, 12:01:11 PM