Skip to content

tninja/aider.el

Repository files navigation

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

Introduction

  • Do you enjoy the AI features of the Cursor AI Code Editor but prefer working 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

  • The following commands are integrated into the aider menu:

Aider session management:

  • (`aider-run-aider`): 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
  • (`aider-switch-to-buffer`): 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

More ways to add files to the Aider buffer:

  • use “@” in the menu to toggle add file between read-write mode and read-only mode
  • (`aider-add-current-file`): Add the current buffer file.
  • (`aider-add-files-in-current-window`): Add all buffers in the current window.
  • (`aider-add-same-type-files-under-dir`): Add all files with the same suffix as the current file under the current directory to Aider.
  • (`aider-batch-add-dired-marked-files`): Add multiple Dired marked files to the Aider buffer.

Refactor code:

  • (`aider-function-or-region-refactor`): If a region is selected, ask Aider to refactor the selected region. Otherwise, ask Aider to refactor the function under the cursor.

Explain code:

  • (`aider-ask-question`): Ask Aider a question about the code in the current context. If a region is selected, use the region as context.
  • (`aider-function-or-region-explain`): If a region is selected, ask Aider to explain the selected region. Otherwise, ask Aider to explain the function under the cursor.
  • (`aider-explain-symbol-under-point`): Ask Aider to explain the symbol under cursor, given the line as context.

Support for Test Driven Development:

  • (`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:

You can add your own Elisp functions to support your specific use cases. Feel free to ask Aider/`aider.el` to help you create them.

Installation

  • Emacs need to be >= 26.1

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 ("aider.el" "aider-doom.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
  • Reset the aider session with [SPC] A r

./doom-menus.png

Optional

Helm Support

Helm enables fuzzy searching functionality for command history prompts

You can enable Helm-based completion with the following code:

(use-package aider
  :straight (:host github :repo "tninja/aider.el" :files ("aider.el" "aider-helm.el")))

Aider script interactive mode: aider-minor-mode

  • If you prefer writing Aider commands in a separate file and sending them to an Aider session (similar to working with Python or R scripts and sending code blocks to a REPL), you might want to try aider-minor-mode. It provides the following key bindings:
    • C-c C-n: Send current line to aider session
    • C-c C-c: Send current paragraph line by line to aider session
    • C-c C-r: Send current region as a single block to aider session
    • Enable aider-minor-mode for your editing buffer
    • To automatically enable aider-minor-mode for any file with “aider” in its 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

  • The screenshot above shows Aider being asked to generate an aider-help function and add its corresponding entry to the menu (top right window).
  • Aider successfully received the command and generated the appropriate commit (bottom left).

Other Emacs AI coding tool

  • Inspired by, and Thanks to: