aider.el icon indicating copy to clipboard operation
aider.el copied to clipboard

aider emacs plugin for https://github.com/paul-gauthier/aider

#+TITLE: aider.el : aider (AI Pair Programming) Inside Emacs

  • Introduction
  • Do you enjoy the AI features of the [[https://www.cursor.com/][Cursor AI Code Editor]] but prefer working within Emacs?

  • [[https://github.com/paul-gauthier/aider][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)

[[file:./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-write-unit-test): Generate comprehensive unit tests for the current function or file. The generated tests will include normal cases, edge cases, and error handling scenarios.
  • (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

  • [[https://aider.chat/docs/install.html][Install aider]]
  • Install the dependency [[https://github.com/magit/transient][Transient]] using your package manager.
  • Install the dependency [[https://github.com/magit/magit][Magit]] using your package manager.
  • Install aider.el with the following code:

#+BEGIN_SRC emacs-lisp (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" ) ;; Or use gemini v2 model since it is very good and free ;; (setq aider-args '("--model" "gemini/gemini-exp-1206")) ;; (setenv "GEMINI_API_KEY" ) ;; ;; Optional: Set a key binding for the transient menu (global-set-key (kbd "C-c a") 'aider-transient-menu)) #+END_SRC

** Doom Installation and Configuration

  • Add the following code to your doom/packages.el

#+BEGIN_SRC emacs-lisp (package! aider :recipe (:host github :repo "tninja/aider.el" :files ("aider.el" "aider-doom.el"))) #+END_SRC

  • Adjust and add the following code to your doom/config.el

#+BEGIN_SRC emacs-lisp (use-package aider :config (setq aider-args '("--model" "gpt-4o-mini"))) #+END_SRC

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= [[file:./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:

#+BEGIN_SRC emacs-lisp (use-package aider :straight (:host github :repo "tninja/aider.el" :files ("aider.el" "aider-helm.el"))) #+END_SRC

*** 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:

#+BEGIN_SRC emacs-lisp (add-hook 'find-file-hook (lambda () (when (and (buffer-file-name) (string-match-p "aider" (buffer-file-name))) (aider-minor-mode 1)))) #+END_SRC

  • Screenshot

[[file:./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).
  • [[./examples][Example Application Written with aider.el]]

  • Other Emacs AI coding tool

  • Inspired by, and Thanks to:
    • [[https://github.com/shouya/ancilla.el][ancilla.el]]: AI Coding Assistant support code generation / code rewrite / discussion
    • [[https://github.com/xenodium/chatgpt-shell][chatgpt-shell]]: ChatGPT and DALL-E Emacs shells + Org Babel
    • [[https://github.com/copilot-emacs/copilot.el][copilot.el]]: Emacs plugin for GitHub Copilot
    • [[https://github.com/chep/copilot-chat.el][copilot-chat.el]]: Chat with GitHub Copilot in Emacs