前言
前段时间, 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↗ 。
Warning
注意,如果要使用
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 之旅。
Tip
现在除了 DeepSeek 官方的 API,其余家的都很稳定,打算购买第三方 API 的访客可以先参考下:
务实测试:DeepSeek 各家 API 真实速度(附:测试脚本)↗
。
核心操作指南
快捷键映射
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 ...?