Skip to content

tninja/aider.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI assisted programming in Emacs with Aider

./icon.png

https://melpa.org/packages/aider-badge.svg https://stable.melpa.org/packages/aider-badge.svg https://img.shields.io/github/contributors/tninja/aider.el.svg

中文版

Table of Contents

Introduction

./transient_menu.png

Installation

  • Emacs need to be >= 26.1

Melpa + package-install (recommended)

Enable installation of packages from MELPA by adding an entry to package-archives after (require ‘package) and before the call to package-initialize in your init.el or .emacs file:

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; Comment/uncomment this line to enable MELPA Stable if desired.  See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
;;(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)
  • Use M-x package-refresh-contents or M-x package-list-packages to ensure that Emacs has fetched the MELPA package list
  • Use M-x package-install to install aider package
  • Import and configure aider.el in your init.el or .emacs file:
(use-package aider
  :config
  ;; For latest claude sonnet model
  (setq aider-args '("--model" "sonnet" "--no-auto-accept-architect")) ;; add --no-auto-commits if you don't want it
  (setenv "ANTHROPIC_API_KEY" anthropic-api-key)
  ;; Or chatgpt model
  ;; (setq aider-args '("--model" "o4-mini"))
  ;; (setenv "OPENAI_API_KEY" <your-openai-api-key>)
  ;; Or use your personal config file
  ;; (setq aider-args `("--config" ,(expand-file-name "~/.aider.conf.yml")))
  ;; ;;
  ;; Optional: Set a key binding for the transient menu
  (global-set-key (kbd "C-c a") 'aider-transient-menu) ;; for wider screen
  ;; or use aider-transient-menu-2cols / aider-transient-menu-1col, for narrow screen
  (aider-magit-setup-transients)) ;; add aider magit function to magit menu
  • aider-args just passed directly to aider CLI, aider options reference
    • to avoid introducing unnecessary complexity and learning cost, configuration in aider.el side is minimized.
  • if aider-args is empty (default), it will use ~/.aider.conf.yml file. In this way, aider CLI and aider.el share same configuration
  • The example models: sonnet, gemini, o4-mini, they charge money, and you need to ask for api key add fund to your api account firstly

Helm Support

Helm enables fuzzy searching functionality for command history prompts. Since it is very possible that we use prompt written before, it could potentially save lots of time typing. This plugin is recommended if you use helm.

If you used installed aider.el through melpa and package-install, just need to (require 'aider-helm)

Frequently used features

Aider session management

aider-run-aider (C-c a a)
Creates a comint-based, git repo-specific Aider session for interactive conversation.
  • Git repository identification is based on the current file’s path
  • Multiple Aider sessions can run simultaneously for different Git repositories
  • When being called with the universal argument (C-u), a prompt will offer the user to change the content of aider-args for this session.
  • When run it in a dired, eshell, or shell buffer, it will ask you if you want to add –subtree-only flag, which only consider files in that directory, to make it faster
aider-switch-to-buffer (C-c a z)
Switch to the Aider buffer.
  • use ^ in the menu to toggle open aider session in other window inside current frame, or open a dedicate frame for aider session. This is useful when there is more than one monitor, and one frame / monitor is used to hold multi buffers for code, and another frame / monitor hold aider session.
aider-reset (C-c a s)
Reset the aider session, drop all files and clear the chat history.
  • It is recommended before start a new task in your repo, without restart session. So the old discussion / context won’t interfere the new task.
aider-open-prompt-file (C-c a p)
Open the repo specific aider prompt file. This is a good place to write prompt / organize tasks. Prompt can be sent to the Aider session with shortcut.

More ways to add files to the Aider buffer

aider-add-current-file-or-dired-marked-files (C-c a f)
Add the current buffer file. If it is used in dired buffer, add all dired marked files.
  • C-u prefix to add files read-only.
aider-add-files-in-current-window (C-c a w)
Add all buffers in the current window. Good for add 2-3 files
aider-add-module (C-c a M)
Recursively add all files with certain suffix list (eg. *.py and *.java) for given directory. You can specify a regex pattern to match the file content to be added. Useful to add files for given topic.
  • C-u prefix to add files read-only.
aider-drop-file (C-c a O)
Drop a file from the Aider session.
  • when it is triggered on a code buffer, it will drop that file
  • when the cursor is on a filename in the repo, it will drop that file
  • /drop command in aider session buffer or aider prompt file will have file completion on current added files

Write code

aider-implement-todo (C-c a i)
Implement requirement in comments in-place, in current context.
  • If cursor is on a comment line, implement that specific comment in-place.
  • If there is a selection region of multi-line comments, implement code for those comments in-place.
  • If cursor is inside a function, implement TODOs for that function, otherwise implement TODOs for the entire current file.
    • The keyword (TODO by default) can be customized with the variable aider-todo-keyword-pair. One example is to use AI! comment, which is as same as aider AI comment feature.
aider-code-change (C-c a c)
If a region is selected, ask Aider to change the selected region. Otherwise, ask Aider to change / change the function under the cursor, or general code change on all added files.
  • A couple common used prompts provided when you are using aider-helm.el
  • It supports inline comment based existing code change requirement. To use this feature, trigger the command when:
    • The current line is a comment describing the change
    • the current selected region is multi-line comments describing the change
  • All the above commands follows code review / apply process

Support for Agile Development

aider-refactor-book-method (C-c a r)
for code refactoring using techniques from Martin Flower’s Refactoring book, you can also let AI make the decision on how to refactor, example: this commit addressing this comment
aider-write-unit-test (C-c a U)
If the current buffer is main source code file, generate comprehensive unit tests for the current function or file. If the cursor is in a test source code file, when the cursor is on a test function, implement the test function. Otherwise, provide description to implement the test function (or spec).
  • If main source code break and test function fails, use aider-function-or-region-change on the failed test function to ask Aider to fix the code to make the test pass.
aider-pull-or-review-diff-file (C-c a v)
let aider to pull and review the code change, it support pull diff for staged files, branch comparison, commits.

Questions on code

aider-ask-question (C-c a q)
Ask Aider a question about the code in the current context. If a region is selected, use the region as context.
  • You can ask any question on the code. Eg. Explain the function, review the code and find the bug, etc
  • A couple common used prompts provided when you are using aider-helm.el
aider-go-ahead (C-c a y)
When you are asking aider to suggest a change using above command, maybe even after several round of discussion, when you are satisfied with the solution, you can use this command to ask Aider to go ahead and implement the change.
aider-code-read (C-c a d)
Choose the method from the book, Code Reading: The Open Source Perspective, by Diomidis Spinellis, to analyze the region / function / file / module.
aider-start-software-planning (C-c a P)
Start an interactive software planning discussion session with Aider, through a question-based sequential thinking process.

Inside comint buffer

  • / key to trigger aider command completion
  • file path completion will be triggered automatically after certain command
  • use TAB key to enter prompt from mini-buffer, or helm with completion

Aider prompt file - Good place to write and organize prompt

  • Syntax highlight, aider command completion, file path completion supported
  • Use C-c a p to open the repo specific prompt file. You can use this file to organize tasks, and write prompt and send them to the Aider session. multi-line prompts are supported.
  • People happy with sending code from editor buffer to comint buffer (eg. ESS, python-mode, scala-mode) might like this. This is a interactive and reproducible way
  • C-c C-n key can be used to send the current prompt line to the comint buffer. Or batch send selected region line by line (C-u C-c C-n). To my experience, this is the most used method in aider prompt file.
  • C-c C-c key is for multi-line prompt. The following example shows C-c C-c key pressed when cursor is on the prompt.

./aider_prompt_file.png

  • start aider session in a sub-tree inside aider prompt file:
    • Use subtree-only <dir> to start aider session in a sub-tree, where <dir> is the directory to start the session.
    • This is useful when you want to work on a sub-directory of a large mono repo, and don’t want to wait for aider to scan the entire repo.

FAQ

  • transient-define-group undefined error:
  • How to review / accept the code change?
    • Comparing to cursor, aider have a different way to do that. Discussion
    • Note: Aider v0.77.0 automatically accept changes for /architect command. If you want to review the code change before accepting it like before for many commands in aider.el, you can disable that flag with “–no-auto-accept-architect” in either aider-args or .aider.conf.yml.
  • How to disable to aider auto-commit?
  • What kind of model aider support? Can aider support local model?
  • How to add file to aider session using menu?
    • single file, in that file buffer, C-c a f
    • two or three files, open all of them in current window as different buffer, C-c a w
    • a few files, in same directory, or have same regex pattern: mark them in dired buffer (or find-grep-dired result with regex), C-c a f.
    • whole project / module, certain types of suffix file (eg. *.py, *.java), C-c a M
  • In large mono repo, aider take long time to scan the repo. How to improve?
    • Aider use .aiderignore file to handle this, detail, or, you can turn off git with –no-git in aider-args.
    • Or, use the –subtree-only with following way in emacs:
      • Used dired, eshell, or shell buffer to go to the directory (subtree) to be included
      • C-c a a to trigger aider-run-aider
      • Answer yes about –subtree-only question, it will add the flag
    • Or, in aider prompt file, use subtree-only <dir> to specify where to start, and use C-c C-n to start aider session at that directory, it start with –subtree-only
  • How to let aider work with your speaking language?
    • use aider coding conventions. In my case, I added “- reply in Chinese” to the CONVENTIONS.md file, and load work through .aider.conf.yml. Or, put sth like following into aider-args variable.
      • “–read” (expand-file-name “~/.emacs.d/.emacs/aider/CONVENTIONS.md”)
  • How to enter multi-line prompts in aider session buffer?
    • aider itself support that, doc.
    • C-c RET: MatthewZMD/aidermacs#139
    • use aider prompt file (aider-open-prompt-file, C-c a p) to write multi-line prompts
  • Can aider.el work with tramp? (aider running on remote machine)
    • artyom-smushkov make aider-add-current-file support tramp file: #36
    • mgcyung said it can work in this way: #85
  • My screen is narrow, the transient menu is too wide, how to make it more readable? (#157)
    • Use aider-transient-menu-1col or aider-transient-menu-2cols to use 1 column or 2 columns transient menu.
  • How to customize the aider-comint-mode prompt and input color?
  • Why aider-code-change got disabled in transient menu?
    • It bypass code review and is not recommended. The code quality is not as good as /architect. Discussed here: #128

Future work

Feature

  • More thinking on improving code quality tool such as unit-test [4/4]
    • [X] Code refactoring functions
    • [X] TDD functions
    • [X] Code reading functions
    • [X] Legacy code support
  • [X] Bootstrap code or document from scratch
  • [-] Import useful MCP feature to aider [1/2]
    • [X] Software planning discussion
    • [ ] Other
  • [ ] Learn and migrate useful feature from popular AI coding tool / MCP
  • [-] Better way to batch add relevant files from repo to aider session [1/2]
    • [X] Dependencies and dependent on current file
    • [ ] Relevant files for current context
  • [-] Consider AI + solid / widely used package [1/4]
    • [-] magit [5/6]
      • [X] show last commit
      • [X] show commits history
      • [X] git-blame analysis
      • [X] git-log analysis
      • [X] diff pull / code review
      • [ ] suggest code change given code review feedback
    • [X] flycheck
    • [ ] compile / test output
    • [ ] projectile
  • [ ] Thinking on how to simplify the menu / commands
    • Only keep frequently used items in the first level

Code quality

  • Better unit-test / integration test of this package. Hopefully it is automated.

AI Assisted Programming related books

The following books introduce how to use AI to assist programming and potentially be helpful to aider / aider.el users.

Other Emacs AI coding tool

  • Inspired by, and Thanks to:
    • ancilla.el: AI Coding Assistant support code generation / code rewrite / discussion
    • chatgpt-shell: ChatGPT and DALL-E Emacs shells + Org Babel, comint session based idea
    • copilot.el: Emacs plugin for GitHub Copilot
    • copilot-chat.el: Chat with GitHub Copilot in Emacs
    • gptel: Most stared / widely used LLM client in Emacs
  • Package depends on this
    • ob-aider.el: Org Babel functions for Aider.el integration
  • Other tools

Contributing

  • Contributions are welcome! Please feel free to submit a Pull Request.