Skip to content

tninja/aider.el

Repository files navigation

aider.el : aider (AI Pair Programming) Inside Emacs

Introduction

  • Do you like the AI features of the Cursor AI Code Editor but prefer to stay within Emacs?
  • Aider is a well-known and highly effective AI pair programming tool for the terminal.
  • The `aider.el` package offers an interactive interface to communicate with Aider in Emacs.
    • Most of the Elisp code in this repository was generated by Aider or `aider.el`.

Why Use `aider.el` in Emacs?

  • Pop-up Menu: No need to remember commands. (aider-transient-menu)

./transient_menu.png

  • Git Repository-Specific Aider Sessions in Emacs: Automatically identify the Git repository of the current file and create a new Aider session for it. Multiple Aider sessions can exist for different Git repositories.
  • More ways to add files to the Aider buffer:
    • Add the current buffer file: (`aider-add-current-file`)
    • Add all buffers in the current window: (`aider-add-files-in-current-window`)
    • Batch add files from the Dired buffer: (`aider-batch-add-dired-marked-files`) Add multiple Dired marked files to the Aider buffer.
  • Refactor region / function:
    • (`aider-region-refactor`): Ask Aider to refactor it given your input instruction.
    • (`aider-function-refactor`): Ask Aider to refactor the function under the cursor given your instruction.
  • Explain code
    • (`aider-region-explain`): Select a region (e.g., a code block) in a file and ask Aider to explain it.
    • (`aider-function-explain`): Place the cursor on a function and ask Aider to explain it.
    • (`aider-explain-symbol-under-point`): Ask Aider to explain the symbol under cursor, given the line as context.
  • Support for Test Driven Development:
    • Fix Tests: (`aider-fix-failing-test-under-cursor`): Place cursor on a failing test function and ask Aider to analyze and fix the code to make tests pass.
  • And More: Add your own Elisp functions to support your use case. You can certainly ask Aider / `aider.el` to do that.

Installation

Vanilla Emacs Installation

  • Install aider
  • Install the dependency Transient using your package manager.
  • Install aider.el with the following code:
(use-package aider
  :straight (:host github :repo "tninja/aider.el" :files ("aider.el"))
  :config
  ;; Use claude-3-5-sonnet cause it is best in aider benchmark 
  (setq aider-args '("--model" "anthropic/claude-3-5-sonnet-20241022"))
  (setenv "ANTHROPIC_API_KEY" anthropic-api-key)
  ;; Or use chatgpt model since it is most well known
  ;; (setq aider-args '("--model" "gpt-4o-mini"))
  ;; (setenv "OPENAI_API_KEY" <your-openai-api-key>)
  ;; Or use gemini v2 model since it is very good and free
  ;; (setq aider-args '("--model" "gemini/gemini-exp-1206"))
  ;; (setenv "GEMINI_API_KEY" <your-gemini-api-key>)
  ;; ;;
  Optional: Set a key binding for the transient menu
  (global-set-key (kbd "C-c a") 'aider-transient-menu))

Doom Installation and Configuration

  • Add the following code to your doom/packages.el
(package! aider :recipe (:host github :repo "tninja/aider.el" :files ("*.el")))
  • Adjust and add the following code to your doom/config.el
(use-package aider
  :config
  (setq aider-args '("--model" "gpt-4o-mini")))

The aider prefix is “A”.

  • Start and open the aider buffer: [SPC] A o
  • Add the current file with [SPC] A a c

./doom-menus.png

Optional

Helm Support

Helm enables fuzzy searching functionality for command history prompts

You can enable Helm-based completion in two ways:

  • 1. Using use-package:
;; Basic aider installation
(use-package aider
  :straight (:host github :repo "tninja/aider.el" :files ("aider.el")))

;; Optional helm support
(use-package aider-helm
  :straight (:host github :repo "tninja/aider.el" :files ("aider-helm.el"))
  :after (aider helm))
  • 2. Manual loading:
;; Load helm support after both aider and helm are loaded
(with-eval-after-load 'helm
  (require 'aider-helm))

Aider script interactive mode: aider-minor-mode

  • If you enjoy writing aider command in a separate file and send them to aider session, just like working on python or R script and send code block into REPL, you might want to try aider-minor-mode. It by default bind C-c C-n to send current line to aider session, and C-c C-c to send current region to aider session.
    • Enable aider-minor-mode for your editing buffer
    • To automatically enable aider-minor-mode to any file with aider inside filename
(add-hook 'find-file-hook
          (lambda ()
            (when (and (buffer-file-name)
                       (string-match-p "aider" (buffer-file-name)))
              (aider-minor-mode 1))))

Screenshot

./screenshot.png

  • In the above screenshot, aider was asked to generate an aider-help function and add the corresponding entry to the menu (top right window).
  • aider received the command and generated the commit for it (bottom left).

Other Emacs AI coding tool

  • Inspired by, and Thanks to: