How to Use Aider for AI Pair Programming? Complete Terminal Guide 2026
If you are tired of copying code between ChatGPT and your editor, Aider brings AI pair programming directly into your terminal. It reads your codebase, edits files, creates atomic Git commits, and works with any LLM from Claude Sonnet to DeepSeek V4. This 2026 guide covers everything from pipx install to architect mode and multi-model configuration — get productive in under 30 minutes.
入门 · 25 分钟 · 2026年6月27日
TL;DR
Aider is an open-source, terminal-based AI pair programming tool that edits code directly in your local Git repository. You chat with it in natural language, it understands your entire codebase via repo-map, makes multi-file edits, and creates atomic Git commits for every change. It works with Claude Sonnet, GPT-5, DeepSeek V4, and local models via Ollama — you're not locked into any vendor. This guide covers installation, multi-model setup, core workflow, architect/editor mode, and best practices that will make you productive in under 30 minutes.
Prerequisites
Before you start, make sure you have:
- Python 3.10+ installed on your system (
python3 --version) - Git installed and configured (
git --version) - pipx for isolated Python tool installation (
pip install pipx && pipx ensurepath) - An API key from at least one LLM provider (OpenAI, Anthropic, DeepSeek, or a local model via Ollama)
- A Git repository to work in — Aider works best inside an existing repo (it auto-commits, so clean Git state is important)
Step 1: Install Aider
The recommended way to install Aider is via pipx, which keeps it in an isolated environment so it won't conflict with your other Python projects.
# Install pipx if you don't have it
python3 -m pip install --user pipx
python3 -m pipx ensurepath
# Install Aider
pipx install aider-chatAfter installation, verify it works:
aider --version
# Expected output: aider v0.80.x (or similar)Alternative install methods:
- pip (not recommended):
pip install aider-chat— may conflict with other packages - Docker:
docker pull paulgauthier/aider— good for CI/CD or if you don't want Python on your machine - One-click install script: Available at aider.chat/docs/install.html for macOS/Linux
Step 2: Set Up API Keys
Aider needs an API key to talk to an LLM. You can use any supported provider. Here are the most common ones:
Option A: Anthropic Claude (Recommended for Best Results)
Claude Sonnet 4 consistently produces the best code quality with Aider.
# macOS/Linux
export ANTHROPIC_API_KEY="sk-ant-..."
# Add to your shell profile for persistence
echo 'export ANTHROPIC_API_KEY="sk-ant-..."' >> ~/.zshrcThen launch with:
aider --model sonnetOption B: OpenAI (GPT-5)
export OPENAI_API_KEY="sk-..."
aider --model openai/gpt-5
# Or for a cheaper option:
aider --model openai/gpt-5-miniOption C: DeepSeek V4 (Cheapest, Open-Source)
export DEEPSEEK_API_KEY="sk-..."
aider --model deepseek/deepseek-chatOption D: Local Models via Ollama (Free, Private)
# Install Ollama and pull a model
ollama pull qwen3:32b
# Launch Aider with a local model
aider --model ollama/qwen3:32bPro tip: You can set a default model in
~/.aider.conf.ymlso you don't have to pass--modelevery time.
Step 3: Your First Aider Session
Navigate to a Git project and launch Aider:
cd ~/projects/my-app
aiderAider will start in chat mode. You'll see something like:
Aider v0.80.0
Model: claude-sonnet-4-20250514
Git repo: .git with 24 files
Repo-map: using 1024 tokens, auto-refresh
────────────────────────────────────────────────
>Core Commands
Aider has a few essential slash-commands that you'll use constantly:
| Command | What It Does |
|---|---|
/add <file> | Add files for Aider to read and edit |
/code <prompt> | Ask Aider to write or modify code |
/commit | Manually commit pending changes |
/diff | Show what Aider changed |
/undo | Undo the last change Aider made |
/clear | Clear the chat history (starts fresh) |
/run <command> | Run a shell command and show output to Aider |
/help | Show all available commands |
Real Example: Add Dark Mode to a React App
Let's walk through a real workflow. Suppose you have a React app and want to add dark mode support:
> /add src/App.tsx src/index.css src/components/ThemeToggle.tsx
Added 3 files to the chat session.
> Add a dark mode toggle using React context. Create a ThemeContext,
a ThemeProvider that wraps the app, and a ThemeToggle button component.
Use Tailwind CSS dark: classes. Store the preference in localStorage.Aider will:
- Read the files you added
- Plan the changes using its repo-map understanding
- Create/Edit the files
- Run any necessary install commands (e.g.,
npm install) - Auto-commit each logical change with a descriptive message
You'll see output like:
Added src/contexts/ThemeContext.tsx
Edited src/App.tsx
Edited src/index.css
Created src/components/ThemeToggle.tsx
Committing: feat: add dark mode with ThemeContext and localStorage persistenceStep 4: Understand the Repo-Map
One of Aider's killer features is the repo-map — an automatic, high-level index of your codebase that helps the LLM understand your project structure.
The repo-map uses Tree-sitter to parse your code and extract:
- All function and class signatures
- Type definitions
- Import/export relationships
- File structure and hierarchy
This means you don't need to add every file manually. Aider already knows what functions exist, what they're called, and how they relate. When you say "update the authenticate function to support OAuth," Aider searches the repo-map, finds the function, and edits it — even if you never explicitly added that file.
You can see how many tokens the repo-map is using:
aider --map-tokens 2048 # Increase repo-map context (default: 1024)
aider --no-map-tokens # Disable repo-map (not recommended)Step 5: Architect/Editor Mode (Advanced)
For complex, multi-file changes, Aider offers Architect/Editor mode — a two-model workflow where:
- Architect model (reasoning-focused, e.g., Claude Opus or o3) analyzes the problem and produces a detailed plan
- Editor model (code-generation-focused, e.g., Claude Sonnet or DeepSeek V4) executes the plan
This separation of "thinking" and "doing" dramatically improves results for complex refactors. The architect handles the hard reasoning once, and the editor focuses purely on implementation.
# Use o3 as architect, Sonnet as editor
aider --architect --model openai/o3 --editor-model sonnet
# Use DeepSeek R1 as architect, DeepSeek V4 as editor
aider --architect --model deepseek/deepseek-reasoner --editor-model deepseek/deepseek-chatWhen to use Architect mode:
- Large refactors spanning 5+ files
- Database schema migrations
- API redesigns (REST to GraphQL)
- Adding a new architectural layer (auth, caching, event system)
When to skip it (use normal /code):
- Single-file bug fixes
- Adding a simple feature (one component, one route)
- Updating documentation
- Small formatting/style changes
Step 6: Git Integration Deep Dive
Aider's Git integration is not an afterthought — it's core to how the tool works. Every change Aider makes is automatically committed with descriptive messages.
Automatic Git Workflow
- When you start Aider, it checks for uncommitted changes
- If there are pre-existing changes, Aider commits them first (so its edits are isolated)
- Every
/coderequest that results in file changes triggers an atomic commit - Commit messages describe what changed and why
Safety Features
# Aider automatically creates a gitignore entry for itself
# to avoid committing its own cache files
# Undo the last Aider commit
> /undo
# See what changed
> /diff
# Explicit commit (rarely needed — usually auto)
> /commitWorking with Branches
# Create a feature branch before big changes
git checkout -b feature/add-auth
# Launch Aider in that branch
aider
# Aider's commits stay on this branch
# You can review, squash, or rebase laterBest practice: Always work on a feature branch when using Aider for non-trivial changes. This gives you a clean rollback path and makes PR review easier.
Step 7: Multi-File Editing & Context Management
Aider can edit multiple files in a single request — but you need to manage context wisely.
Adding Files
# Add specific files
> /add src/auth.ts src/db.ts src/api/routes.ts
# Add all files in a directory
> /add src/components/*.tsx
# Use read-only files (Aider sees them but won't edit)
> /read docs/API_SPEC.mdContext Limits
Every LLM has a context window limit. Aider shows you how much context you're using:
Tokens: 5,234 / 200,000 (2.6%)
If you're approaching the limit:
- Use
/drop <file>to remove files from context - Use
/clearto reset the conversation (repo-map stays) - Add only the files Aider actually needs to see — not your entire project
In-Chat File Creation
You can ask Aider to create new files without adding them first:
> Create a new file src/utils/validation.ts with Zod schemas for User and Post
Aider will create the file, add it to the chat, and commit it.
Step 8: Configuration & Customization
The .aider.conf.yml File
Create ~/.aider.conf.yml for persistent settings:
# ~/.aider.conf.yml
model: sonnet
dark-mode: true
map-tokens: 2048
auto-commits: true
git: true
chat-language: EnglishPer-Project `.aider.conf.yml
Place a .aider.conf.yml in your project root for project-specific settings that override global config:
# ./my-project/.aider.conf.yml
model: openai/gpt-5
read: [docs/STYLE_GUIDE.md, docs/ARCHITECTURE.md]
auto-test: true
test-cmd: npm testUseful Config Options
| Option | What It Does |
|---|---|
auto-commits: false | Disable automatic Git commits |
auto-test: true | Run tests after each change |
test-cmd: "pytest" | Command to run for auto-testing |
dark-mode: true | Dark theme for the terminal UI |
chat-language: Spanish | Set the chat language |
cache-prompts: true | Cache system prompts (faster startup) |
Step 9: Cost Management
Aider gives you visibility into what you're spending on API calls.
Tracking Usage
After each session, Aider shows a summary:
Total cost: $0.42
Total tokens: 85,000 prompt + 12,000 completion
Session duration: 12 minutesCost-Saving Tips
- Use DeepSeek for straightforward edits — DeepSeek V4 costs a fraction of Claude/GPT and handles most coding tasks well
- Use
/clearliberally — every message in the chat history adds to the prompt tokens - Be specific in your prompts — vague prompts lead to back-and-forth corrections, multiplying costs
- Set a budget in your config:
# .aider.conf.yml
max-chat-history-tokens: 8000 # Limit accumulated contextModel Cost Comparison (June 2026, approximate)
| Model | Input (per 1M tokens) | Output (per 1M tokens) | Best For |
|---|---|---|---|
| Claude Sonnet 4 | $3.00 | $15.00 | Complex multi-file refactors |
| GPT-5 | $2.50 | $10.00 | General-purpose coding |
| DeepSeek V4 | $0.27 | $1.10 | High-volume, cost-sensitive |
| o3 (Architect) | $10.00 | $40.00 | Architecture planning only |
Step 10: Advanced Workflows
Lint-Fix Cycle
# Set up auto-linting
aider --lint-cmd "eslint --fix src/" --auto-lint
# Aider will run the linter after each change and fix issues automaticallyTest-Driven Development with Aider
# Write a failing test first, then ask Aider to make it pass
> /add tests/test_auth.py
> The test `test_login_returns_jwt` is failing. Implement the login
endpoint in `src/auth.py` to make it pass. Use JWT with HS256.PR Review Workflow
# Clone the PR branch
git fetch origin pull/42/head:pr-42
git checkout pr-42
# Ask Aider to review
aider --message "Review this PR. Check for security issues, missing error handling, and suggest improvements. Output a structured review."Multi-Repo Coordination
# Use /read to bring in files from outside the repo
> /read ../shared-library/src/types.ts
> /read ../api-docs/openapi.yaml
# Now Aider can use types and API specs from sibling projectsTroubleshooting
API key not found
Make sure you exported the correct environment variable:
- OpenAI:
OPENAI_API_KEY - Anthropic:
ANTHROPIC_API_KEY - DeepSeek:
DEEPSEEK_API_KEY
Run echo $ANTHROPIC_API_KEY to verify. If it returns nothing, re-export it.
Git repository not found
Aider requires a Git repo. Initialize one with:
git init
git add .
git commit -m "Initial commit"Aider made changes I don't like
# Undo the last Aider commit
> /undo
# Or revert manually
git log --oneline # Find the Aider commit
git revert <commit-hash>Context window full" / slow responses
- Use
/dropto remove files you no longer need - Use
/clearto start a fresh conversation (repo-map stays) - Reduce
map-tokensif your repo is very large - Add only the specific files Aider needs, not your entire project
pipx: command not found
python3 -m pip install --user pipx
python3 -m pipx ensurepath
# Restart your terminal or run:
source ~/.zshrc # or ~/.bashrcRelated Tutorials
相关推荐
How to Build an AI Agent with n8n? Complete Beginner Tutorial 2026
If you want to build AI agents that search the web, read emails, query databases, and make decisions autonomously — without writing hundreds of lines of code — n8n gives you a visual drag-and-drop editor. This tutorial takes you from zero to a production-ready agent with LLM integration, multi-tool orchestration, persistent memory, and RAG document Q&A in 60 minutes.
How to Build a Full-Stack App with Bolt.new? Complete 2026 Tutorial
If you want to build a full-stack web app without setting up a local dev environment, Bolt.new lets you describe your app in plain English and generates a complete React/Next.js application with database, auth, and one-click deployment — all inside your browser.
主题中心
2026 AI 编程工具全景指南
从 Copilot 改版到 Claude Code / DeepSeek 低成本方案——把分散资讯收成可搜索、可对比的工具矩阵。
进入「2026 AI 编程工具全景指南」 →赚钱视角
这个趋势怎么赚钱?
WayToClawEarn 的差异在可验证的赚钱案例,而不只是资讯。从这些复盘开始:
浏览全部案例 →相关教程
相关资讯
- Linux Foundation Launches Akrites: 19 Tech Giants Unite to Defend Open Source Against AI-Powered Exploits
- Agent Beacon: First Open-Source Telemetry Layer for AI Coding Agents Launches
- Why Did Anthropic Open a Seoul Office During the Fable 5 Export Ban? Korea Strategy Explained
- Can a Single Web Page Hack Your AI Coding Agent? Microsoft's AutoJack Exploit Explained