Skip to content

Efficiency Hacks

The developer who saves 10 minutes per task does not seem impressive. But across 30 tasks per day, that is 5 hours per week. These efficiency hacks are the small techniques that compound into massive productivity gains.

  • Context management techniques that keep sessions lean and fast
  • Output format tricks for different workflows
  • Shell aliases and functions that reduce keystrokes
  • Patterns for avoiding common time sinks

The single biggest efficiency win is clearing context when switching tasks:

/rename payment-bug-investigation
/clear

Stale context from a previous task makes every subsequent message more expensive (more tokens processed) and less accurate (Claude uses irrelevant context).

When you need to stay in the same session but context is bloated:

/compact Keep: file paths I edited, test results, error traces. Remove: exploratory file reads, discussion about alternatives, my questions.
# Compact instructions
When compacting, preserve:
- Test results and error output
- File paths and code changes made
- Key decisions and their rationale
Remove:
- Exploratory file reads that did not lead to changes
- Verbose command output that has been summarized
- Discussion of rejected approaches

Add to ~/.zshrc or ~/.bashrc:

Terminal window
# Quick question -- ask Claude without starting a session
alias cq='claude -p'
# Continue last session
alias cc='claude -c'
# Review current changes
alias cr='git diff | claude -p "Quick review: only flag CRITICAL and HIGH issues."'
# Explain a file
explain() {
claude -p "Explain what $1 does, its key functions, and how it fits into the project."
}
# Generate tests for a file
gentest() {
claude -p "Generate comprehensive tests for $1. Follow existing test patterns in this project."
}
# Debug an error
debug() {
echo "$@" | claude -p "Analyze this error and suggest a fix. Read the relevant source files."
}

When you need to wire Claude into a shell pipeline, ask for free-form text and read .result. Compare the two workflows for the same task:

Terminal window
# Before: prose answer you have to copy out of the terminal by hand
claude -p "List all API endpoints with their HTTP method, path, and handler file"
# After: machine-readable text you can pipe into grep, wc, or another command
claude -p "List all API endpoints with their HTTP method, path, and handler file" \
--output-format json | jq -r '.result'

Use plain --output-format json (read from .result) when you want Claude’s natural-language answer in a script; reach for --json-schema (next section) only when you need the answer parsed into specific fields.

For guaranteed output structure, pair --json-schema with --output-format json. The schema-conformant result lands in the top-level structured_output field (alongside session metadata), so pipe to .structured_output, not .result:

Terminal window
claude -p "Analyze the test coverage of src/api/" \
--output-format json \
--json-schema '{"type":"object","properties":{"total_files":{"type":"number"},"covered_files":{"type":"number"},"uncovered_files":{"type":"array","items":{"type":"string"}}}}' \
| jq '.structured_output'

This is the reliable way to feed Claude’s output into another script: the schema forces shape, and jq '.structured_output' strips the wrapper so you get clean JSON. Without --output-format json, the --json-schema flag is ignored and you get plain text back.

Claude’s default behavior is to read many files before acting. For focused tasks, constrain the scope:

Fix the null pointer in src/api/users.ts line 42.
Only read src/api/users.ts and its direct imports.
Do not explore other files.

Claude often spends tokens summarizing changes after making them. Add to your CLAUDE.md:

After making changes, do not provide a summary unless I ask for one.
Instead, just show the git diff of what changed.

Running Full Test Suites After Every Change

Section titled “Running Full Test Suites After Every Change”
# In CLAUDE.md
When running tests after a change, run only the tests related to the modified file.
Use: npx jest [file] --no-coverage
Only run the full test suite when I ask for it or before committing.
# In CLAUDE.md
Never read these files:
- package-lock.json
- yarn.lock
- any file in dist/ or build/
- any file in node_modules/
- any .map file
Terminal window
# Work on a frontend app with access to the shared library
claude --add-dir ../shared-components
Terminal window
claude -p "Quick task" --fallback-model sonnet

If the default model (Opus 4.8) is overloaded, this automatically falls back to Sonnet 4.6 instead of failing the run — invaluable inside cron jobs and CI pipelines where an unattended error means a missed deploy. The flag is print-mode only.

Terminal window
# Resume the local session that was linked when you ran gh pr create
claude --from-pr 123

This resumes the local session that was automatically linked to the PR when you created it with gh pr create, so you can pick that work back up to address review comments. It does not fetch the PR contents itself — it only works for a PR whose originating session still exists on this machine.

Aliases conflict with other tools: Check for name conflicts with which cq before adding aliases. Choose unique names that do not shadow existing commands.

Context gets cleared accidentally: Get in the habit of using /rename before /clear. Sessions persist even after clearing, so you can resume them later.

Efficiency rules in CLAUDE.md are too restrictive: If Claude seems unable to explore when needed, use “ignore efficiency rules for this task” to override temporarily. The rules are guidance, not hard blocks.