论如何用neovim优雅的写代码

NekoNekoNiko120 发布于 13 天前 58 次阅读


其实我写这篇文章主要是想分享一下我的neovim工作流 主要是抄ai写的代码

我的模板使用的是astronvim,它非常简单易用,从官网安装即可

系统要求:nerd字体 neovim0.10+ tree-sitter(如果需要auto_install的话) 一个好用的终端(建议iterm2/warp 但是warp没赞助我node python git

建议使用homebrew安装:

首先安装brew

直接安装(需要特殊网络环境)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果需要国内源的话:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

然后安装上面的依赖:

brew tap homebrew/cask-fonts
brew install --cask font-hack-nerd-font
brew install neovim
brew install tree-sitter
brew install node
brew install python@3
brew install git

然后安装astronvim

mv ~/.config/nvim ~/.config/nvim.bak

mv ~/.local/share/nvim ~/.local/share/nvim.bak
mv ~/.local/state/nvim ~/.local/state/nvim.bak
mv ~/.cache/nvim ~/.cache/nvim.bak

git clone --depth 1 https://github.com/AstroNvim/template ~/.config/nvim

# remove template's git connection to set up your own later

rm -rf ~/.config/nvim/.git

nvim

然后是安装我常用的一些语言:

nvim ~/.config/nvim/lua/community.lua

把最上面的那一行删掉,然后在下面加上:

{ import = "astrocommunity.pack.cpp" },
{ import = "astrocommunity.pack.swift" },
{ import = "astrocommunity.pack.rust" },
{ import = "astrocommunity.pack.java" },
{ import = "astrocommunity.pack.rust" },

然后安装yetone大佬写的avante.nvim插件(一个把neovim变成cursor的插件)

nvim ~/.config/nvim/lua/plugins/avante.lua

在里面写入

vim.env.DEEPSEEK_API_KEY = "your-key"
vim.env.AIHUBMIX_API_KEY = "your-key"
vim.env.TAVILY_API_KEY = "your-key"

return {
{
"yetone/avante.nvim",
event = "VeryLazy",
lazy = false,
version = false, -- set this if you want to always pull the latest change
opts = {
provider = "copilot",
providers = {
deepseek = {
__inherited_from = "openai",
api_key_name = "DEEPSEEK_API_KEY",
endpoint = "https://api.deepseek.com",
model = "deepseek-coder",
max_tokens = 8192,
},
aihubmix = {
model = "gemini-2.5-pro",
},
},
mode = "agentic",
web_search_engine = {
provider = "tavily", -- tavily, serpapi, searchapi, google, kagi, brave 或 searxng
proxy = nil, -- proxy support, e.g., http://127.0.0.1:789
},
},
-- if you want to build from source then do make BUILD_FROM_SOURCE=true
build = "make BUILD_FROM_SOURCE=true",
-- build = "powershell -ExecutionPolicy Bypass -File Build.ps1 -BuildFromSource false" -- for windows
dependencies = {
"nvim-treesitter/nvim-treesitter",
"stevearc/dressing.nvim",
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
--- The below dependencies are optional,
"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" },
},
},
},
}

别忘了把your-key改成你的API KEY

然后去配置venv-selector:

nvim ~/.config/nvim/lua/plugins/venv-selector.lua

return {
"linux-cultist/venv-selector.nvim",
dependencies = { "neovim/nvim-lspconfig", "nvim-telescope/telescope.nvim", "mfussenegger/nvim-dap-python" },
opts = {
-- Your options go here
-- name = "venv",
-- auto_refresh = false
},
event = "VeryLazy",
branch = "regexp",
-- Optional: needed only if you want to type :VenvSelect without a keymapping
keys = {
-- Keymap to open VenvSelector to pick a venv.
{ "vs", "VenvSelect" },
-- Keymap to retrieve the venv from a cache (the one previously used for the same project directory).
{ "vc", "VenvSelectCached" },
},
}

好了,现在你应该可以拥有一个很好的nvim ide了

好孩子别学我抄ai的代码