前言
前段时间, avante.nvim↗ 插件支持了 DeepSeek的API↗ 截止2025年2月18日,由于官方服务器资源紧张,暂时停止API的充值服务。 ,这边将配置的全流程记录一下,供大家参考。
avante.nvim
avante.nvim是一个 Neovim↗ 的插件,旨在模仿AI开发环境 Cursor↗ 的工作模式。它给用户提供了AI驱动的代码建议,并且能够以花费最小的努力将这些建议直接应用于其源文件。
由于,我这里使用的是
LazyVim↗
来管理我的NeoVim插件配置,我们可以很方便的进行我的anate.nvim的安装。
~/.config/nvim/lua/plugins/avante.lua
129 lines
vim.env.ANTHROPIC_API_KEY = "sk-xxxx"
vim.env.DEEPSEEK_API_KEY = "sk-xxxx"
return {
"yetone/avante.nvim",
event = "VeryLazy",
lazy = false,
version = false, -- Set this to "*" to always pull the latest release version, or set it to false to update to the latest code changes.
opts = {
provider = "deepseek_r1", -- 指定使用 DeepSeek 作为 provider
auto_suggestions_provider = "deepseek",
vendors = {
deepseek = {
__inherited_from = "openai",
disable_tools = true,
api_key_name = "DEEPSEEK_API_KEY",
endpoint = "https://api.deepseek.com",
model = "deepseek-coder",
},
deepseek_r1 = {
__inherited_from = "openai",
disable_tools = true,
api_key_name = "DEEPSEEK_API_KEY",
endpoint = "https://api.deepseek.com",
model = "deepseek-reasoner",
},
behaviour = {
auto_suggestions = true, -- Experimental stage
auto_set_highlight_group = true,
auto_set_keymaps = true,
auto_apply_diff_after_generation = true,
support_paste_from_clipboard = true,
},
mappings = {
--- @class AvanteConflictMappings
diff = {
ours = "co",
theirs = "ct",
all_theirs = "ca",
both = "cb",
cursor = "cc",
next = "]x",
prev = "[x",
},
suggestion = {
accept = "<M-l>",
next = "<M-]>",
prev = "<M-[>",
dismiss = "<C-]>",
},
jump = {
next = "]]",
prev = "[[",
},
submit = {
normal = "<CR>",
insert = "<C-s>",
},
sidebar = {
apply_all = "A",
apply_cursor = "a",
switch_windows = "<Tab>",
reverse_switch_windows = "<S-Tab>",
},
},
highlights = {
---@type AvanteConflictHighlights
diff = {
current = "DiffText",
incoming = "DiffAdd",
},
},
--- @class AvanteConflictUserConfig
diff = {
autojump = true,
---@type string | fun(): any
list_opener = "copen",
--- Override the 'timeoutlen' setting while hovering over a diff (see :help timeoutlen).
--- Helps to avoid entering operator-pending mode with diff mappings starting with `c`.
--- Disable by setting to -1.
override_timeoutlen = 500,
},
suggestion = {
debounce = 600,
throttle = 600,
},
},
-- if you want to build from source then do `make BUILD_FROM_SOURCE=true`
build = "make",
-- build = "powershell -ExecutionPolicy Bypass -File Build.ps1 -BuildFromSource false" -- for windows
dependencies = {
"stevearc/dressing.nvim",
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
--- The below dependencies are optional,
"echasnovski/mini.pick", -- for file_selector provider mini.pick
"nvim-telescope/telescope.nvim", -- for file_selector provider telescope
"hrsh7th/nvim-cmp", -- autocompletion for avante commands and mentions
"ibhagwan/fzf-lua", -- for file_selector provider fzf
"nvim-tree/nvim-web-devicons", -- or echasnovski/mini.icons
"zbirenbaum/copilot.lua", -- for providers='copilot'
{
-- support for image pasting
"HakonHarnes/img-clip.nvim",
event = "VeryLazy",
opts = {
-- recommended settings
default = {
embed_image_as_base64 = false,
prompt_for_file_name = false,
drag_and_drop = {
insert_mode = true,
},
-- required for Windows users
use_absolute_path = true,
},
},
},
{
-- Make sure to set this up properly if you have lazy=true
"MeanderingProgrammer/render-markdown.nvim",
opts = {
file_types = { "markdown", "Avante" },
},
ft = { "markdown", "Avante" },
},
},
},
}
:Lazy install yetone/avante.nvim
来进行此插件的安装。我在这边对于auto_suggestions和chat的providers使用了不同的模型,参考于: avante.nvim#774↗ 和 avante.nvim#1115↗ 。
auto_suggestions
功能,记得调整一下debounce
和throttle
设置,即防抖和节流,单位为ms。否则,你在写文章的后,你会发现你一天用了373w tokens,不要问我为什么知道的。获取DeepSeek API Key
注册DeepSeek账户
- 访问 DeepSeek官网↗
- 点击"Sign Up"注册新账户
- 完成邮箱验证
创建API Key
- 登录后进入控制台
- 导航至"API Keys"页面
- 点击"Create new API Key"按钮
- 为API Key设置一个描述性名称(如"nvim-avante")
- 复制生成的API Key并妥善保存
API Key使用注意事项
- API Key仅在创建时显示一次,请务必立即保存
- 建议将API Key存储在安全的地方(如密码管理器)
- 每个API Key都有使用限额,请合理使用
- 如果API Key泄露,请立即删除并创建新的Key
将你创建的DeepSeek API Key填入到上述文件开头的vim.env.DEEPSEEK_API_KEY
即可开启你的avante.nvim之旅。
核心操作指南
快捷键映射
avante.nvim 提供了丰富的快捷键操作体系,以下是主要快捷键说明:
快捷键 | 模式 | 功能描述 |
---|---|---|
<leader>aa | 普通模式 | 打开/切换侧边栏 |
<leader>ar | 普通模式 | 刷新侧边栏内容 |
<leader>af | 普通模式 | 切换侧边栏焦点 |
<leader>ae | 普通模式 | 编辑选中代码块 |
co | 差异模式 | 采用当前版本 |
ct | 差异模式 | 采用建议版本 |
ca | 差异模式 | 采用所有建议修改 |
cb | 差异模式 | 合并双方修改 |
cc | 差异模式 | 基于光标位置选择 |
]] | 结果窗口 | 跳转到下一个代码块 |
[[ | 结果窗口 | 跳转到上一个代码块 |
注意事项:
- 使用Lazy.nvim时快捷键会自动安全绑定(不与现有快捷键冲突)
- 可通过配置覆盖默认快捷键:
~/.config/nvim/lua/plugins/avante.lua
8 lines
opts = {
mappings = {
diff = {
ours = "<leader>co", -- 自定义采用当前版本
theirs = "<leader>ct"
}
}
}
核心命令
avante.nvim 提供了一系列强大的命令操作:
命令 | 功能描述 | 使用示例 |
---|---|---|
:AvanteAsk [问题] | 向AI提问代码相关问题 | :AvanteAsk 如何优化这个循环结构 |
:AvanteChat | 开启代码库对话模式 | :AvanteChat position=right |
:AvanteEdit | 编辑选中代码块 | 配合视觉模式使用 |
:AvanteRefresh | 刷新所有Avante窗口 | 解决界面卡顿时使用 |
:AvanteSwitchProvider | 切换AI服务提供商 | :AvanteSwitchProvider openai |
:AvanteToggle | 开关侧边栏 | 快速切换工作区布局 |
高级用法示例:
- 带上下文的提问:
:AvanteAsk position=float 请解释这个函数的时间复杂度
- 代码重构工作流:
- 选中目标代码块
- 执行:AvanteEdit
- 使用co/ct快捷键选择修改
- 按 :w保存变更
笑话一则
avante.nvim#1011↗ :This was my process, and I’m sure I’m not the only one in this situation.
I am a software developer
People are talking about AI; I have no idea how to use it
Friend suggests this plugin
Cool, maybe I'll learn how to use AI at work
Add the plugin to my lazy.nvim config, try it out, and it asks for something called ANTHROPIC_API_KEY
No idea what that is
Google it
Oh, looks like I need an account
Create an account
Find the key and add it to my .bashrc
Try the plugin again
I get a the response posted above
> "make sure you have credit balance"
No idea what that is
Google it
Apparently you have to pay for this ...?
Apparently I have $4.90 credit in my account
Seems like maybe I got $5 free to start and I've used 10¢ of it ...?