Neovim Plugin
MyDeskBot for Neovim brings AI-powered assistance to your Neovim environment.
Installation
Plugin Manager Installation
vim-plug
Add to your ~/.vimrc or ~/.config/nvim/init.vim:
Plug 'mydeskbot/mydeskbot-nvim'Run:
:PlugInstallpacker.nvim
Add to your ~/.config/nvim/init.lua:
use {
'mydeskbot/mydeskbot-nvim',
requires = { 'nvim-lua/plenary.nvim' },
config = function()
require('mydeskbot').setup({
-- Configuration options
})
end
}Run:
:PackerSynclazy.nvim
Add to your ~/.config/nvim/init.lua:
{
'mydeskbot/mydeskbot-nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
require('mydeskbot').setup({
-- Configuration options
})
end
}dein.vim
Add to your ~/.config/nvim/init.vim:
call dein#add('mydeskbot/mydeskbot-nvim')Run:
:call dein#install()Configuration
Basic Setup
require('mydeskbot').setup({
api_key = 'sk-...',
model = 'gpt-4o',
temperature = 1.0,
max_tokens = 4096
})Environment Variables
Set API key via environment variables:
# In your shell profile (.bashrc, .zshrc, etc.)
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."Then in Neovim:
require('mydeskbot').setup({
model = 'gpt-4o'
})Full Configuration
require('mydeskbot').setup({
-- API Configuration
api_key = os.getenv('OPENAI_API_KEY'),
model = 'gpt-4o',
temperature = 1.0,
max_tokens = 4096,
-- Completion Settings
completions = {
enabled = true,
trigger = 'manual', -- 'auto' or 'manual'
debounce_ms = 150,
},
-- Chat Settings
chat = {
open_command = 'split',
open_direction = 'right', -- 'right', 'left', 'above', 'below'
width = 80,
height = 20,
},
-- Context Settings
context = {
files = 5,
include_line_numbers = true,
use_gitignore = true,
},
-- UI Settings
ui = {
theme = 'dark',
virtual_text = true,
inline_indicator = true,
},
-- Keybindings
keymaps = {
open_chat = '<leader>mc',
inline_chat = '<leader>mi',
explain_code = '<leader>me',
refactor = '<leader>mr',
complete = '<Tab>',
toggle = '<leader>mt',
},
})Keybindings
Default Keybindings
| Action | Keybinding |
|---|---|
| Open Chat Panel | <leader>mc |
| Inline Chat | <leader>mi |
| Explain Code | <leader>me |
| Refactor Code | <leader>mr |
| Trigger Completion | <Tab> |
| Toggle MyDeskBot | <leader>mt |
Custom Keybindings
Override default keybindings:
require('mydeskbot').setup({
keymaps = {
open_chat = '<C-m>',
inline_chat = '<C-i>',
explain_code = '<C-e>',
refactor = '<C-r>',
complete = '<C-y>',
}
})Commands
MyDeskBot Commands
All commands start with :MyDeskBot:
:MyDeskBotChat- Open chat panel:MyDeskBotInline- Start inline chat:MyDeskBotExplain- Explain selected text:MyDeskBotRefactor- Refactor selected text:MyDeskBotComplete- Trigger completion:MyDeskBotToggle- Toggle MyDeskBot on/off
Features
1. Inline Completions
Get code suggestions as you type:
-- Start typing
function calculateA
-- MyDeskBot suggests (shown as virtual text)
function calculateAverage(numbers)
return numbers.reduce((sum, num) => sum + num, 0) / numbers.length
endTrigger: <Tab> or configure auto-trigger
2. Chat Panel
Open a dedicated chat window:
-- Press <leader>mc
-- Chat panel opens on the right side
-- Ask questions:
You: How do I reverse a string in Lua?
AI: Here are two ways:
-- Method 1: Using string.reverse
local reversed = string.reverse("hello") -- "olleh"
-- Method 2: Manual iteration
local function reverse_str(s)
local result = ""
for i = #s, 1, -1 do
result = result .. s:sub(i, i)
end
return result
end3. Inline Chat
Ask questions inline without leaving your editor:
- Select code or place cursor
- Press
<leader>mi - Type your question
- Get inline response
4. Code Explanation
- Select code (visual mode
v) - Press
<leader>me - MyDeskBot explains selected code
5. Code Refactoring
- Select code to refactor
- Press
<leader>mr - MyDeskBot suggests improvements
Workflows
1. Code Review
" Select code in visual mode
v
" Press <leader>me to explain
" Or <leader>mr to refactor2. Debugging
-- Ask MyDeskBot to analyze error:
You: I'm getting "nil value" error in this function
function processData(data)
return data.value -- Error: data might be nil
end
AI: The issue is that `data` might be nil. You should add nil checking:
function processData(data)
if not data then return nil end
return data.value
end3. Writing Documentation
-- Ask: Generate documentation for this function
function calculate_total(items)
local total = 0
for _, item in ipairs(items) do
total = total + item.price * item.quantity
end
return total
end
-- MyDeskBot generates:
--- Calculate total price of items
-- @param items table Array of items with price and quantity
-- @return number Total price
function calculate_total(items)
...
end4. Test Generation
-- Ask: Generate tests for this function
function is_valid_email(email)
return email:match("[^@]+@[^@]+") ~= nil
end
-- MyDeskBot generates:
describe('is_valid_email', function()
it('should return true for valid email', function()
assert.is_true(is_valid_email('test@example.com'))
end)
it('should return false for invalid email', function()
assert.is_false(is_valid_email('invalid'))
end)
end)Advanced Features
Telescope Integration
If you use Telescope:
require('telescope').load_extension('mydeskbot')
-- Available commands
:Telescope mydeskbot chat
:Telescope mydeskbot completionsLSP Integration
MyDeskBot works alongside LSP:
require('mydeskbot').setup({
lsp_integration = {
enabled = true,
priority = 5, -- Lower than LSP completions
combine_with_lsp = true,
}
})Git Integration
MyDeskBot can analyze Git context:
require('mydeskbot').setup({
git = {
enabled = true,
show_uncommitted_changes = true,
include_branch_info = true,
}
})Multi-file Context
Analyze multiple files:
-- In chat, reference other files:
You: Look at utils/auth.lua and explain how authentication works
AI: [Analyzes both files and explains auth flow]Lua API
Programmatic Usage
local mydeskbot = require('mydeskbot')
-- Chat programmatically
mydeskbot.chat("How do I create a table in Lua?", function(response)
print(response)
end)
-- Get completion
mydeskbot.complete(current_line, function(suggestions)
-- Handle suggestions
end)
-- Explain code
mydeskbot.explain(selected_text, function(explanation)
print(explanation)
end)Custom Commands
Create custom MyDeskBot commands:
vim.api.nvim_create_user_command('MyDeskBotFix', function(opts)
local text = table.concat(opts.args, ' ')
mydeskbot.inline_chat(text, function(response)
-- Handle response
end)
end, { nargs = '*' })
-- Usage:
:MyDeskBotFix This code has a bugConfiguration Examples
Neovim 0.9+ (Lua)
-- init.lua
require('mydeskbot').setup({
api_key = os.getenv('OPENAI_API_KEY'),
model = 'gpt-4o',
keymaps = {
open_chat = '<leader>ai',
inline_chat = '<leader>ai',
}
})
-- With LSP config
local lspconfig = require('lspconfig')
lspconfig.lua_ls.setup({
settings = {
Lua = {
completion = {
enable = true,
}
}
}
})Classic Neovim (Vimscript)
" init.vim
lua << EOF
require('mydeskbot').setup({
api_key = os.getenv('OPENAI_API_KEY'),
})
EOFUsing Different Providers
-- OpenAI
require('mydeskbot').setup({
api_key = 'sk-...',
provider = 'openai',
model = 'gpt-4o',
})
-- Anthropic
require('mydeskbot').setup({
api_key = 'sk-ant-...',
provider = 'anthropic',
model = 'claude-3-5-sonnet-20240620',
})Troubleshooting
Plugin Not Loading
Check plugin manager installation:
vim:PlugStatus (vim-plug) :PackerStatus (packer.nvim)Check Neovim version (requires 0.7+):
vim:versionCheck for errors:
vim:lua print(vim.inspect(package.loaded['mydeskbot']))
API Key Issues
Verify API key is set:
vim:lua print(require('mydeskbot').config.api_key)Check environment variable:
vim:lua print(vim.inspect(os.getenv('OPENAI_API_KEY')))
No Completions
Check if completions are enabled:
vim:lua print(require('mydeskbot').config.completions.enabled)Try manual trigger:
vim:MyDeskBotComplete
Performance Issues
Reduce context files:
luacontext = { files = 3 }Increase debounce time:
luacompletions = { debounce_ms = 300 }
Tips and Tricks
1. Use with Autopairs
require('nvim-autopairs').setup({
-- MyDeskBot works seamlessly with autopairs
})2. Custom Prompts
Create custom prompt templates:
require('mydeskbot').setup({
prompts = {
refactor = "Improve the code for better readability and performance",
explain = "Explain what this code does in detail",
test = "Write comprehensive unit tests for this code",
}
})3. File Type Specific Settings
require('mydeskbot').setup({
filetypes = {
lua = {
model = 'gpt-4o',
temperature = 0.7,
},
python = {
model = 'gpt-4o',
temperature = 0.8,
},
}
})4. Status Line Integration
-- Using lualine
require('lualine').setup({
sections = {
lualine_a = {
{ 'mydeskbot_status' }
}
}
})