-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
363 lines (319 loc) · 14.9 KB
/
Copy pathinit.lua
File metadata and controls
363 lines (319 loc) · 14.9 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- =============================================================================
-- Starter Neovim Config
--
-- Drop this file at ~/.config/nvim/init.lua, or run install.sh.
-- Plugins auto-install on first launch. No manual steps needed.
--
-- Requires Neovim >= 0.12
-- =============================================================================
-- Common first tweaks (lines to edit when you're ready to personalize):
-- Line 72 — colorscheme: swap 'tokyonight' for 'gruvbox' (or your own)
-- Line 65 — indent size: change shiftwidth/tabstop from 2 to 4
-- Line 25 — plugins: add or remove entries in vim.pack.add()
--
-- The fallback indent (2 spaces) is overridden per-file by guess-indent.nvim
-- which auto-detects the project's indentation. Adjust both values together.
-- 0 ─ Loader (faster startup) ────────────────────────────────────────────────
vim.loader.enable()
-- 1 ─ Leader keys ────────────────────────────────────────────────────────────
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- 2 ─ Plugins (auto-installed by Neovim's built-in vim.pack.add) ─────────────
vim.pack.add({
{ src = "https://github.com/folke/tokyonight.nvim", name = "tokyonight.nvim" },
-- alternative theme — see colorscheme section below
-- { src = 'https://github.com/ellisonleao/gruvbox.nvim', name = 'gruvbox.nvim' },
{ src = "https://github.com/folke/which-key.nvim", name = "which-key.nvim" },
{ src = "https://github.com/folke/snacks.nvim", name = "snacks.nvim" },
{ src = "https://github.com/williamboman/mason.nvim", name = "mason.nvim" },
{ src = "https://github.com/williamboman/mason-lspconfig.nvim", name = "mason-lspconfig.nvim" },
{ src = "https://github.com/neovim/nvim-lspconfig", name = "nvim-lspconfig" },
{ src = "https://github.com/saghen/blink.cmp", name = "blink.cmp" },
{ src = "https://github.com/saghen/blink.lib", name = "blink.lib" },
{ src = "https://github.com/lewis6991/gitsigns.nvim", name = "gitsigns.nvim" },
{ src = "https://github.com/NMAC427/guess-indent.nvim", name = "guess-indent.nvim" },
{ src = "https://github.com/L3MON4D3/LuaSnip", name = "LuaSnip", version = vim.version.range("2.*") },
}, { confirm = false })
-- Build hook: LuaSnip needs 'make install_jsregexp' for advanced snippet transforms
vim.api.nvim_create_autocmd("PackChanged", {
callback = function(ev)
local name = ev.data.spec.name
local kind = ev.data.kind
if kind ~= "install" and kind ~= "update" then
return
end
if name == "LuaSnip" and vim.fn.has("win32") ~= 1 and vim.fn.executable("make") == 1 then
vim.system({ "make", "install_jsregexp" }, { cwd = ev.data.path }):wait()
end
end,
})
-- 3 ─ Which-key: keymap popup after pressing <Space> ─────────────────────────
require("which-key").setup({
spec = {
{ "<leader>s", group = "[S]earch" },
{ "<leader>b", group = "[B]uffer" },
{ "<leader>t", group = "[T]oggle" },
{ "<leader>c", group = "[C]ode" },
{ "<leader>f", group = "[F]ormat" },
{ "<leader>r", group = "[R]ename" },
},
})
-- 4 ─ Colorscheme ────────────────────────────────────────────────────────────
-- If icons look broken or missing, install a Nerd Font: https://www.nerdfonts.com/
-- Only one colorscheme at a time — the last uncommented line wins.
vim.cmd.colorscheme("tokyonight")
-- vim.cmd.colorscheme("gruvbox")
-- 5 ─ Options ────────────────────────────────────────────────────────────────
vim.opt.number = true -- line numbers
vim.opt.mouse = "a" -- allow mouse (helpful early on)
vim.opt.clipboard = "unnamedplus" -- sync with system clipboard
vim.opt.ignorecase = true -- case-insensitive search...
vim.opt.smartcase = true -- ...unless you type capitals
vim.opt.termguicolors = true -- 24-bit color support
vim.opt.expandtab = true -- spaces instead of tabs
vim.opt.shiftwidth = 2 -- fallback indent (guess-indent overrides per-file)
vim.opt.tabstop = 2
vim.opt.signcolumn = "yes" -- always show gutter
vim.opt.splitright = true -- vertical splits go right
vim.opt.splitbelow = true -- horizontal splits go below
vim.opt.cursorline = true -- highlight current line
vim.opt.scrolloff = 4 -- keep some context around cursor
vim.opt.confirm = true -- dialog on unsaved changes instead of error
vim.opt.inccommand = "split" -- live preview of :s substitutions
vim.opt.updatetime = 250 -- faster LSP diagnostics
vim.opt.timeoutlen = 300 -- faster which-key popup
vim.opt.showmode = false -- already shown in the statusline
vim.opt.breakindent = true -- wrapped lines keep their indentation
vim.opt.wrap = false -- no line wrapping in code
-- Persistent undo history — files accumulate in ~/.local/state/nvim/undo/
-- Clean up periodically: rm -rf ~/.local/state/nvim/undo/
vim.opt.undofile = true
vim.opt.completeopt = "menu,menuone,noselect"
vim.opt.list = true -- show invisible characters
vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "␣" }
-- Auto-detect indentation per file — overrides shiftwidth/tabstop above
require("guess-indent").setup({})
-- 6 ─ Escape Insert mode with jk ─────────────────────────────────────────────
vim.keymap.set("i", "jk", "<Esc>")
-- 7 ─ Yank highlight — flash yanked text briefly ─────────────────────────────
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Flash yanked text",
group = vim.api.nvim_create_augroup("starter-yank", { clear = true }),
callback = function()
vim.highlight.on_yank()
end,
})
-- 8 ─ Essential keymaps (press <Space> and wait for which-key menu) ──────────
-- Search
vim.keymap.set("n", "<leader>sf", function()
Snacks.picker.files()
end, { desc = "[S]earch [F]iles" })
vim.keymap.set("n", "<leader>sg", function()
Snacks.picker.grep()
end, { desc = "[S]earch [G]rep" })
vim.keymap.set("n", "<leader>sh", function()
Snacks.picker.help()
end, { desc = "[S]earch [H]elp" })
vim.keymap.set("n", "<leader>sk", function()
Snacks.picker.keymaps()
end, { desc = "[S]earch [K]eymaps" })
vim.keymap.set("n", "<leader>s.", function()
Snacks.picker.recent()
end, { desc = '[S]earch recent files ("." for repeat)' })
-- Buffers
vim.keymap.set("n", "<leader>bb", function()
Snacks.picker.buffers()
end, { desc = "[B]uffer [B]rowse" })
-- Toggle
vim.keymap.set("n", "<leader>e", function()
Snacks.explorer()
end, { desc = "[E]xplorer (file tree)" })
vim.keymap.set("n", "<leader>tt", function()
Snacks.terminal()
end, { desc = "[T]erminal [T]oggle" })
-- Exit terminal mode (easier for beginners than <C-\><C-n>)
vim.keymap.set("t", "<Esc><Esc>", "<C-\\><C-n>", { desc = "Exit terminal mode" })
-- Config / Mason
vim.keymap.set("n", "<leader>cm", "<cmd>Mason<CR>", { desc = "[C]heck [M]ason (install LSPs)" })
-- Format
vim.keymap.set("n", "<leader>f", function()
vim.lsp.buf.format({ async = true })
end, { desc = "[F]ormat buffer" })
-- File
vim.keymap.set("n", "<leader>w", "<cmd>write<CR>", { desc = "[W]rite (save)" })
vim.keymap.set("n", "<leader>q", "<cmd>close<CR>", { desc = "[Q]uit buffer" })
-- Clear search highlights with Escape
vim.keymap.set("n", "<Esc>", "<cmd>nohlsearch<CR>")
-- Split navigation — Ctrl + hjkl to move between windows
vim.keymap.set("n", "<C-h>", "<C-w><C-h>", { desc = "Focus left window" })
vim.keymap.set("n", "<C-l>", "<C-w><C-l>", { desc = "Focus right window" })
vim.keymap.set("n", "<C-j>", "<C-w><C-j>", { desc = "Focus lower window" })
vim.keymap.set("n", "<C-k>", "<C-w><C-k>", { desc = "Focus upper window" })
-- Better indent — keep selection in visual mode
vim.keymap.set("v", "<", "<gv")
vim.keymap.set("v", ">", ">gv")
-- 9 ─ Diagnostics: better error display ──────────────────────────────────────
vim.diagnostic.config({
update_in_insert = false, -- less visual noise while typing
severity_sort = true,
float = {
border = "rounded",
source = "if_many",
},
virtual_text = true,
jump = {
on_jump = function(_, bufnr)
vim.diagnostic.open_float({ bufnr = bufnr, scope = "cursor", focus = false })
end,
},
})
-- 10 ─ LSP: Language Server Protocol ─────────────────────────────────────────
--
-- Language servers power go-to-definition, hover docs, diagnostics, code
-- actions, and auto-completion. The flow:
--
-- Mason (install LS binaries via :Mason UI)
-- → mason-lspconfig (detects installed servers, calls vim.lsp.enable)
-- → Neovim built-in LSP client (vim.lsp.*)
-- → blink.cmp (surfaces LSP results as completion popup)
--
-- Run :checkhealth vim.lsp to see which servers are running and which
-- buffers they're attached to. (Neovim ≥0.12 removed :LspInfo; use
-- :checkhealth as the equivalent.)
--
-- The LspAttach autocmd below fires each time a server connects to a
-- buffer. It sets up keymaps (gd, gr, K, rename, code actions) and
-- document-highlighting on cursor hold.
-- Auto-attach keymaps and features when an LSP server connects to a buffer
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("starter-lsp-attach", { clear = true }),
callback = function(event)
local map = function(keys, func, desc)
vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
end
map("gd", vim.lsp.buf.definition, "[G]oto [D]efinition")
map("gr", vim.lsp.buf.references, "[G]oto [R]eferences")
map("K", vim.lsp.buf.hover, "Hover Documentation")
map("[d", vim.diagnostic.goto_prev, "Previous Diagnostic")
map("]d", vim.diagnostic.goto_next, "Next Diagnostic")
map("<leader>rn", vim.lsp.buf.rename, "[R]e[n]ame Symbol")
map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction")
-- Highlight references on cursor hold
local client = vim.lsp.get_client_by_id(event.data.client_id)
if client and client.server_capabilities.documentHighlightProvider then
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
buffer = event.buf,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = event.buf,
callback = vim.lsp.buf.clear_references,
})
end
end,
})
-- Merge blink.cmp capabilities into all LSP server configs.
-- Without this, blink.cmp can't read completion, signature-help, or
-- snippet results from the LSP server.
vim.lsp.config("*", {
capabilities = require("blink.cmp").get_lsp_capabilities(vim.lsp.protocol.make_client_capabilities()),
})
-- 11 ─ Mason: install and auto-configure language servers ────────────────────
--
-- Mason provides a UI (:Mason) to browse and install language servers,
-- formatters, linters, and debug adapters. It downloads binaries into
-- ~/.local/share/nvim/mason/ and manages them.
--
-- mason-lspconfig bridges Mason to Neovim's built-in LSP client. Whenever
-- you install a server through Mason, mason-lspconfig auto-enables it
-- (automatic_enable = true) so it starts the next time you open a matching
-- file. No per-server setup blocks needed.
--
-- To install a server, press <Space>cm to open the :Mason UI, press 2 for
-- LSP servers, search, and press i. Restart Neovim — the server attaches
-- automatically.
--
-- For per-server overrides (e.g. root_dir, extra settings), add a
-- vim.lsp.config("server_name", {...}) block BEFORE this setup call.
-- See docs/customizing.md.
require("mason").setup()
require("mason-lspconfig").setup({
automatic_enable = true, -- auto-start every installed server (Neovim ≥0.11)
})
-- 12 ─ LuaSnip: snippet engine (powers the 'snippets' source in blink.cmp) ──
require("luasnip").setup({})
-- 13 ─ blink.cmp: auto-completion ────────────────────────────────────────────
--
-- blink.cmp replaces the older nvim-cmp. Sources (in priority order):
-- lsp — language server results (the main source)
-- path — filesystem paths
-- snippets — LuaSnip snippets
-- buffer — words already in the current buffer
--
-- Completion appears automatically as you type. Press <C-Space> to
-- manually open the menu. <C-y> accepts; <C-n>/<C-p> cycle through
-- suggestions; <Tab> jumps between snippet placeholders.
require("blink.cmp").setup({
keymap = {
preset = "none",
["<C-Space>"] = { "show", "show_documentation", "hide_documentation" },
["<C-y>"] = { "select_and_accept" },
["<C-p>"] = { "select_prev", "fallback" },
["<C-n>"] = { "select_next", "fallback" },
["<C-b>"] = { "scroll_documentation_up", "fallback" },
["<C-f>"] = { "scroll_documentation_down", "fallback" },
["<Tab>"] = { "snippet_forward", "select_next", "fallback" },
["<S-Tab>"] = { "snippet_backward", "select_prev", "fallback" },
},
fuzzy = { implementation = "lua" },
sources = {
default = { "lsp", "path", "snippets", "buffer" },
},
snippets = {
preset = "luasnip",
},
completion = {
menu = { border = "rounded" },
documentation = { window = { border = "rounded" }, auto_show = true },
},
signature = { enabled = true },
})
-- 14 ─ Gitsigns: git change indicators in the gutter ─────────────────────────
require("gitsigns").setup()
-- 15 ─ Snacks: fuzzy finder, terminal, UI polish ─────────────────────────────
require("snacks").setup({
explorer = { enabled = true },
terminal = { enabled = true },
picker = {
matcher = {
fuzzy = true,
smartcase = true,
filename_bonus = true,
},
},
})
-- 16 ─ Treesitter: better syntax highlighting (built into Neovim >= 0.12) ────
--
-- Neovim 0.12 ships parsers for C, Lua, Markdown, Vimscript, Vimdoc, and
-- Query files. Treesitter highlighting works automatically for those.
-- Install additional parsers via your OS package manager or tree-sitter CLI:
-- tree-sitter install python
-- tree-sitter install rust
-- tree-sitter install bash
--
-- The autocmd below tries to enable treesitter for every filetype and
-- silently skips languages without a parser installed.
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("starter-treesitter", { clear = true }),
callback = function(args)
local lang = vim.treesitter.language.get_lang(args.match)
if not lang then
return
end
local has_parser = vim.treesitter.language.add(lang)
if not has_parser then
return
end
pcall(vim.treesitter.start, args.buf, lang)
end,
})