Skip to content

joshuavial/aider.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

72 Commits

Repository files navigation

Aider Plugin for Neovim

This is a simple plugin for Neovim that allows you to open a terminal window inside Neovim and runAider.I wrote it as an experiment in using Aider which is by far the best AI coding assistant I've seen, and now just a few keystrokes away in vim.

Installation

You can install the Aider Plugin for Neovim using various package managers. Here are the instructions for some common ones:

Usingpacker.nvim

use'joshuavial/aider.nvim'

Usingvim-plug

Plug'joshuavial/aider.nvim'

Usingdein

calldein#add('joshuavial/aider.nvim')

Usage

The Aider Plugin for Neovim provides theAiderOpenandAiderBackgroundlua functions.

TheAiderOpenfunction opens a terminal window with the Aider command. It accepts the following arguments:

  • args:The command line arguments to pass toaider- defaults to ""
  • window_type:The window style to use 'vsplit' (default), 'hsplit' or 'editor'

NOTE: if an Aider job is already running calling AiderOpen will reattach to it, even if it is called with different flags

TheAiderBackgroundfunction runs the Aider command in the background. It accepts the following arguments:

  • args:The command line arguments to pass toaider- defaults to ""
  • message:The message to pass to the Aider command - defaults to "Complete as many todo items as you can and remove the comment for any item you complete."

When Aider opens (through either function), it will automatically add all open buffers to both commands. If you are going to use this plugin you will want to actively manage open buffers with commands like:lsand:bd.

Here are some examples of how to use theAiderOpenandAiderBackgroundfunctions:

:luaAiderOpen()
:luaAiderOpen("-3","hsplit")
:luaAiderOpen("AIDER_NO_AUTO_COMMITS=1 aider -3","editor")
:luaAiderBackground()
:luaAiderBackground("-3")
:luaAiderBackground("AIDER_NO_AUTO_COMMITS=1 aider -3")

You can also set keybindings for theAiderOpenandAiderBackgroundfunctions in Lua. Here's an example:

--set a keybinding for the AiderOpen function
vim.api.nvim_set_keymap('n','<leader>oa','<cmd>lua AiderOpen()<cr>',{noremap=true,silent=true})
--set a keybinding for the AiderBackground function
vim.api.nvim_set_keymap('n','<leader>ob','<cmd>lua AiderBackground()<cr>',{noremap=true,silent=true})

In this example, pressing<leader>oain normal mode will call theAiderOpenfunction, and<leader>obwill call theAiderBackgroundfunction.

Runaider --helpto see all the options you can pass to the cli.

The plugin provides the following default keybindings:

  • <leader><Space><Space>to open a terminal window with the Aider defaults (gpt-4).
  • <leader><Space>3to open a terminal window with the Aider command using the gpt-3.5-turbo-16k model for chat.
  • <leader><space>bto run the Aider command in the background with the defaults.
  • <leader><space>b3to run the Aider command in the background using the gpt-3.5-turbo-16k model for chat.

Setup

The Aider Plugin for Neovim provides asetupfunction that you can use to configure the plugin. This function accepts a table with the following keys:

  • auto_manage_context:A boolean value that determines whether the plugin should automatically manage the context. If set totrue,the plugin will automatically add and remove buffers from the context as they are opened and closed. Defaults totrue.
  • default_bindings:A boolean value that determines whether the plugin should use the default keybindings. If set totrue,the plugin will require the keybindings file and set the default keybindings. Defaults totrue.

Here is an example of how to use thesetupfunction:

require('aider').setup({
auto_manage_context=false,
default_bindings=false
})

In this example, thesetupfunction is called with a table that setsauto_manage_contexttofalseanddefault_bindingstofalse.This means that the plugin will not automatically manage the context and will not use the default keybindings.

aider_background_status

The plugin exposes a global variable calledaider_background_statusthat you can use to check the status of the Aider background process. Here is a snippet which will create a A that will change colours based on whether the background process is running or not.

lualine_x={{
function()
return'A'
end,
color={fg='#8FBCBB'},--green
cond=function()
return_G.aider_background_status=='idle'
end
},
{
function()
return'A'
end,
color={fg='#BF616A'},--red
cond=function()
return_G.aider_background_status=='working'
end
}
}

Reloading buffers

Because the AiderOnBufferOpen command is bound to BufReadPost it will fire whenever a buffer is reloaded if you just use a:e!.The ReloadBuffer function below will prevent a file from being added to aider every time it's openeed.

functionReloadBuffer()
localtemp_sync_value=vim.g.aider_buffer_sync
vim.g.aider_buffer_sync=0
vim.api.nvim_exec2('e!',{output=false})
vim.g.aider_buffer_sync=temp_sync_value
end

To use this function, simply call:lua ReloadBuffer()(or bind it to your favourite shortcut). This will refresh the current buffer and display any changes made by Aider.

Tips for Working with Buffers in Vim

If you're not familiar with buffers in Vim, here are some tips:

  • Use:lsor:buffersto see all open buffers.
  • Use:b <number>or:buffer <number>to switch to a specific buffer. Replace<number>with the buffer number.
  • Use:bdor:bdeleteto close the current buffer.
  • Use:bd <number>or:bdelete <number>to close a specific buffer. Replace<number>with the buffer number.
  • Use:bufdo bdto close all buffers.

This helper function may be useful for closing all buffers that are hidden

_G.close_hidden_buffers=function()
localcurr_buf_num=vim.api.nvim_get_current_buf()
localall_buf_nums=vim.api.nvim_list_bufs()

for_,buf_numinipairs(all_buf_nums)do
ifbuf_num~=curr_buf_numandvim.api.nvim_buf_is_valid(buf_num)andvim.api.nvim_buf_is_loaded(buf_num)andvim.fn.bufwinnr(buf_num)==-1then
ifvim.fn.getbufvar(buf_num,'&buftype')~='terminal'then
vim.api.nvim_buf_delete(buf_num,{force=true})
end
end
end
end

NOTE

if you resize a split the nvim buffer can truncate the text output, chatGPT tells me there isn't an easy work around for this. Feel free to make a PR if you think it's easy to solve without rearchitecting and using tmux or something similar.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages