nvchad自用配置文件存档

一直有把vim配置成一个宇宙无敌的IDE的冲动,但是每次配置的过程都无比繁琐(总是在莫名其妙的地方出BUG),而且最后出来的结果并不是很好用。。。

所以还是要有一个开箱即用的版本,目前我选择的Nvim配置是基于NvChad项目。为了能在新的设备上能有一个手熟的编辑器,准备在这里放一下现在用的一些配置。

由于这个项目更新迭代非常快,所以现在的这套配置文件也不知道什么时候就会失效吧…

配置文件目录结构

目前用的这套config是跟着一个油管的大佬学的,感觉做的还是很不错的。大致的config文件结构如下:

本来是想直接上传到git仓库中的,clone下来也方便。但是主要考虑到有时候使用设备的地区网路有时候连hub都连不上,所以还是用这种比较蠢的方法贴在博客上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

 .
├──  init.lua
├──  lua
│ ├──  chadrc.lua
│ ├──  configs
│ │ ├──  conform.lua
│ │ ├──  dap-python.lua
│ │ ├──  dap-ui.lua
│ │ ├──  dap.lua
│ │ ├──  lazy.lua
│ │ ├──  lint.lua
│ │ ├──  lspconfig.lua
│ │ ├──  mason-conform.lua
│ │ ├──  mason-dap.lua
│ │ ├──  mason-lint.lua
│ │ ├──  mason-lspconfig.lua
│ │ └──  treesitter.lua
│ ├──  mappings.lua
│ ├──  options.lua
│ └──  plugins
│ └──  init.lua

各细分文件内容

init.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

vim.g.base46_cache = vim.fn.stdpath "data" .. "/base46/"
vim.g.mapleader = " "

-- bootstrap lazy and all plugins
local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim"

if not vim.uv.fs_stat(lazypath) then
local repo = "https://github.com/folke/lazy.nvim.git"
vim.fn.system { "git", "clone", "--filter=blob:none", repo, "--branch=stable", lazypath }
end

vim.opt.rtp:prepend(lazypath)

local lazy_config = require "configs.lazy"

-- load plugins
require("lazy").setup({
{
"NvChad/NvChad",
lazy = false,
branch = "v2.5",
import = "nvchad.plugins",
},

{ import = "plugins" },
}, lazy_config)

-- load theme
dofile(vim.g.base46_cache .. "defaults")
dofile(vim.g.base46_cache .. "statusline")

require "options"
require "nvchad.autocmds"

vim.schedule(function()
require "mappings"
end)

lua

chadrc.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- This file needs to have same structure as nvconfig.lua 
-- https://github.com/NvChad/ui/blob/v3.0/lua/nvconfig.lua
-- Please read that file to know all available options :(

---@type ChadrcConfig
local M = {}

M.base46 = {
theme = "onedark",

-- hl_override = {
-- Comment = { italic = true },
-- ["@comment"] = { italic = true },
-- },
}

return M

mappings.lua

1
2
3
4
5
6
7
8
9
10
11
require "nvchad.mappings"

-- add yours here

local map = vim.keymap.set

map("n", ";", ":", { desc = "CMD enter command mode" })
map("i", "jk", "<ESC>")

-- map({ "n", "i", "v" }, "<C-s>", "<cmd> w <cr>")

options.lua

1
2
3
4
5
6
7
8
9
10
11
12
13

require "nvchad.options"

-- add yours here!

local o = vim.o

-- Indenting
o.shiftwidth = 4
o.tabstop = 4
o.softtabstop = 4

-- o.cursorlineopt ='both' -- to enable cursorline!

configs

conform.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

local options = {
formatters_by_ft = {
lua = { "stylua" },
c = { "clang_format" },
cpp = { "clang_format" },
python = { "black" },
-- css = { "prettier" },
-- html = { "prettier" },
},

formatters = {
clang_format = {
prepend_args = {
"-style={IndentWidth: 4, TabWidth: 4, UseTab: Never, AccessModifierOffset: 0, IndentAccessModifiers: true, PackConstructorInitializers: Never}",
},
},
black = {
prepend_args = {
"--fast",
"--line-length",
"80",
},
},
},

format_on_save = {
-- These options will be passed to conform.format()
timeout_ms = 500,
lsp_fallback = true,
},
}

return options

dap-python.lua

1
2
3
4
5
6
7
8
9
local path = "~/.local/share/nvim/mason/packages/debugpy/venv/bin/python"
require("dap-python").setup(path)

local map = vim.keymap.set

map("n", "<leader>dpr", function()
require("dap-python").test_method()
end, { desc = "Run DAP Python test method" })

dap-ui.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local dap = require("dap")
local dapui = require("dapui")
dapui.setup()

local map = vim.keymap.set
map("n", "<leader>du", "<cmd>lua require('dapui').toggle()<CR>", { desc = "Toggle DAP UI" })

dap.listeners.after.attach.dapui_config = function()
dapui.open()
end
dap.listeners.after.launch.dapui_config = function()
dapui.open()
end
dap.listeners.before.event_terminated.dapui_config = function()
dapui.close()
end
dap.listeners.before.event_exited.dapui_config = function()
dapui.close()
end

dap.lua

1
2
3
4
5
6
local map = vim.keymap.set

map("n", "<leader>db", "<cmd> DapToggleBreakpoint <CR>", { desc = "Toggle DAP Breakpoint" })

map("n", "<leader>dr", "<cmd> DapContinue <CR>", { desc = "Start or continue DAP" })

lazy.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
return {
defaults = { lazy = true },
install = { colorscheme = { "nvchad" } },

ui = {
icons = {
ft = "",
lazy = "󰂠 ",
loaded = "",
not_loaded = "",
},
},

performance = {
rtp = {
disabled_plugins = {
"2html_plugin",
"tohtml",
"getscript",
"getscriptPlugin",
"gzip",
"logipat",
"netrw",
"netrwPlugin",
"netrwSettings",
"netrwFileHandlers",
"matchit",
"tar",
"tarPlugin",
"rrhelper",
"spellfile_plugin",
"vimball",
"vimballPlugin",
"zip",
"zipPlugin",
"tutor",
"rplugin",
"syntax",
"synmenu",
"optwin",
"compiler",
"bugreport",
"ftplugin",
},
},
},
}

lint.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local lint = require("lint")

lint.linters_by_ft = {
lua = { "luacheck" },
python = { "mypy", "ruff" },
}

lint.linters.luacheck.args = {
-- "--globals",
-- "vim",
-- "lvim",
-- "reload",
-- "--",
"--globals",
-- "love",
"vim",
"--formatter",
"plain",
"--codes",
"--ranges",
"-",
}

vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, {
callback = function()
lint.try_lint()
end,
})

lspconfig.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- -- load defaults i.e lua_lsp
-- require("nvchad.configs.lspconfig").defaults()
--
-- local lspconfig = require "lspconfig"
--
-- -- EXAMPLE
-- local servers = { "html", "cssls" }
-- local nvlsp = require "nvchad.configs.lspconfig"
--
-- -- lsps with default config
-- for _, lsp in ipairs(servers) do
-- lspconfig[lsp].setup {
-- on_attach = nvlsp.on_attach,
-- on_init = nvlsp.on_init,
-- capabilities = nvlsp.capabilities,
-- }
-- end
--
-- -- configuring single server, example: typescript
-- -- lspconfig.ts_ls.setup {
-- -- on_attach = nvlsp.on_attach,
-- -- on_init = nvlsp.on_init,
-- -- capabilities = nvlsp.capabilities,
-- -- }
-- --

require("nvchad.configs.lspconfig").defaults()

local lspconfig = require("lspconfig")
local nvlsp = require("nvchad.configs.lspconfig")

-- list of all servers configured.
lspconfig.servers = {
"lua_ls",
"clangd",
"pyright",
}

-- list of servers configured with default config.
local default_servers = {
"lua_ls",
-- "clangd",
-- "pyright",
}

-- lsps with default config
for _, lsp in ipairs(default_servers) do
lspconfig[lsp].setup({
on_attach = nvlsp.on_attach,
on_init = nvlsp.on_init,
capabilities = nvlsp.capabilities,
})
end

lspconfig.clangd.setup({
on_attach = function(client)
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
nvlsp.on_attach(client)
end,
on_init = nvlsp.on_init,
capabilities = nvlsp.capabilities,
})

lspconfig.pyright.setup({
on_attach = nvlsp.on_attach,
on_init = nvlsp.on_init,
capabilities = nvlsp.capabilities,

settings = {
python = {
analysis = {
typeCheckingMode = "off", --Disable type checking diagnostics
-- diagnosticMode = "openFilesOnly",
-- reportUnusedVariable = "none",
-- reportUnusedImport = "none",
},
},
},
})

-- configuring single server, example: typescript
-- lspconfig.ts_ls.setup {
-- on_attach = nvlsp.on_attach,
-- on_init = nvlsp.on_init,
-- capabilities = nvlsp.capabilities,
-- }
--

mason-conform.lua

1
2
3
4
5
6
7
require("mason-conform").setup({
-- list of formatters to ignore during install
ignore_install = {
"clang-format",
},
})

mason-dap.lua

1
2
3
4
5
require("mason-nvim-dap").setup({
ensure_installed = { "python" },
automatic_installation = { exclude = {} },
})

mason-lint.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local lint = package.loaded["lint"]

-- List of linters to ignore during install
local ignore_install = {}

-- Helper function to find if value is in table.
local function table_contains(table, value)
for _, v in ipairs(table) do
if v == value then
return true
end
end
return false
end

-- Build a list of linters to install minus the ignored list.
local all_linters = {}
for _, v in pairs(lint.linters_by_ft) do
for _, linter in ipairs(v) do
if not table_contains(ignore_install, linter) then
table.insert(all_linters, linter)
end
end
end

require("mason-nvim-lint").setup({
ensure_installed = all_linters,
automatic_installation = false,
})

mason-lspconfig.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local lspconfig = package.loaded["lspconfig"]

--list of servers to ignore during install
local ignore_install = {
"clangd",
}

-- Helper function to find if value is in table
local function table_contains(table, value)
for _, v in ipairs(table) do
if v == value then
return true
end
end
return false
end

-- build a list of lsp servers to install minus the ignored list
local all_servers = {}
for _, s in ipairs(lspconfig.servers) do
if not table_contains(ignore_install, s) then
table.insert(all_servers, s)
end
end

require("mason-lspconfig").setup({
ensure_installed = all_servers,
automatic_installation = false,
})

treesitter.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local options = {
ensure_installed = {
"bash",
"c",
"cmake",
"cpp",
"fish",
"lua",
"luadoc",
"make",
"markdown",
"printf",
"python",
"toml",
"vim",
"vimdoc",
"yaml",
},

highlight = {
enable = true,
use_languagetree = true,
},

indent = { enable = true },
}

require("nvim-treesitter.configs").setup(options)

plugins

init.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
return {

{
"nvim-treesitter/nvim-treesitter",
event = { "BufWritePre", "BufNewFile" },
config = function()
require("configs.treesitter")
end,
},

{
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
config = function()
require("nvchad.configs.lspconfig").defaults()
require("configs.lspconfig")
end,
},

{
"williamboman/mason-lspconfig.nvim",
event = "VeryLazy",
dependencies = { "nvim-lspconfig" },
config = function()
require("configs.mason-lspconfig")
end,
},

{
"mfussenegger/nvim-lint",
event = { "BufReadPre", "BufNewFile" },
config = function()
require("configs.lint")
end,
},

{
"rshkarin/mason-nvim-lint",
event = "VeryLazy",
dependencies = { "nvim-lint" },
config = function()
require("configs.mason-lint")
end,
},

{
"stevearc/conform.nvim",
event = "BufWritePre", -- uncomment for format on save
opts = require("configs.conform"),
},

{
"zapling/mason-conform.nvim",
event = "VeryLazy",
dependencies = { "conform.nvim" },
config = function()
require("configs.mason-conform")
end,
},

{
"mfussenegger/nvim-dap",
config = function()
require("configs.dap")
end,
},

{
"nvim-neotest/nvim-nio",
},

{
"rcarriga/nvim-dap-ui",
dependencies = {
"mfussenegger/nvim-dap",
"nvim-neotest/nvim-nio",
},
config = function()
require("configs.dap-ui")
end,
},

{
"mfussenegger/nvim-dap-python",
ft = "python",
dependencies = {
"mfussenegger/nvim-dap",
"rcarriga/nvim-dap-ui",
},
config = function()
require("configs.dap-python")
end,
},

{
"jay-babu/mason-nvim-dap.nvim",
event = "VeryLazy",
config = function()
require("configs.mason-dap")
end,
},

-- These are some examples, uncomment them if you want to see them work!
-- {
-- "neovim/nvim-lspconfig",
-- config = function()
-- require("configs.lspconfig")
-- end,
-- },

-- {
-- "nvim-treesitter/nvim-treesitter",
-- opts = {
-- ensure_installed = {
-- "vim",
-- "lua",
-- "vimdoc",
-- "html",
-- "css",
-- },
-- },
-- },
--
}

作者

Jhuoer Yen

发布于

2024-12-02

更新于

2024-12-02

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×