-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.lua
More file actions
1199 lines (1052 loc) · 35.3 KB
/
init.lua
File metadata and controls
1199 lines (1052 loc) · 35.3 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Modern Neovim config (Lua-first), updated from an older init.vim.
-- LARGELY AI-GENERATED
-- ============================================================
-- Rough guideline of packages to install...
-- ============================================================
-- sudo apt update
-- sudo apt install -y \
-- git curl unzip ca-certificates \
-- ripgrep fd-find \
-- python3 python3-venv python3-pip \
-- clangd clang-tidy \
-- shfmt\
-- lua5.4 luarocks
--
-- # Install node and go: ...
--
-- npm i @fsouza/prettierd
-- npm i -D vscode-langservers-extracted
-- go install github.com/sqls-server/sqls@latest
--
-- python3 -m venv ~/.virtualenvs/nvim
-- ~/.virtualenvs/nvim/bin/python -m pip install --upgrade pip
-- ~/.virtualenvs/nvim/bin/python -m pip install --upgrade black isort
-- ~/.virtualenvs/nvim/bin/python -m pip install --upgrade pynvim
----------------------------------------------------------------------
-- Neovim configuration (Lua)
--
-- Design goals:
-- • Replace old Vimscript init.vim with modern Neovim APIs
-- • Use built-in LSP instead of external linters/completion engines
-- • Automatically use project-local tooling (node_modules, venvs)
-- • Provide a "polyglot workstation":
-- SvelteKit + TypeScript + Docker + SQL + Python + C++
--
-- Major subsystems in this file:
-- 1. Plugin manager bootstrap (lazy.nvim)
-- 2. Editor behavior (options)
-- 3. Keybindings and QoL behavior (ported from original Vim config)
-- 4. Completion (nvim-cmp)
-- 5. LSP configuration (mason + vim.lsp)
-- 6. Formatter integration (conform.nvim)
-- 7. Automatic project environment detection
--
-- IMPORTANT:
-- This config assumes Neovim ≥ 0.11.
-- Older versions will fail because vim.lsp.config() is used.
----------------------------------------------------------------------
----------------------------------------------------------------------
-- Pre-plugin globals (must be set before plugin loads)
----------------------------------------------------------------------
-- Yoink + Cutlass coexistence: include delete/cut ops in Yoink history
vim.g.yoinkIncludeDeleteOperations = 1
-- We use BOTH:
-- vim-cutlass → changes delete/cut behavior (doesn't clobber default yank)
-- vim-yoink → yank history browser
--
-- Without this setting, "cut" operations (d/x) would NOT appear in
-- yank history. That makes undoing deletes painful.
-- So we explicitly include delete operations in yoink history.
-- === Neovim Python provider auto-detection ===
-- Put this near the top of your init.lua (before plugins load)
local uv = vim.loop
local fn = vim.fn
-- Order matters: first valid venv wins
local candidate_envs = {
"dev",
"nvim",
"neovim",
"py3",
"main",
"default",
}
local function is_executable(path)
return fn.executable(path) == 1
end
local function file_exists(path)
local stat = uv.fs_stat(path)
return stat and stat.type or false
end
local function is_valid_venv(venv_path)
local python = venv_path .. "/bin/python"
local cfg = venv_path .. "/pyvenv.cfg"
-- must look like a venv
if not (file_exists(cfg) and is_executable(python)) then
return false
end
-- must have pynvim installed
local cmd = string.format("%s -c 'import pynvim'", python)
local result = os.execute(cmd)
return result == 0
end
for _, name in ipairs(candidate_envs) do
local path = fn.expand("~/.virtualenvs/" .. name)
if file_exists(path) and is_valid_venv(path) then
vim.g.python3_host_prog = path .. "/bin/python"
vim.notify("Neovim Python provider: " .. name, vim.log.levels.INFO)
break
end
end
----------------------------------------------------------------------
-- Plugin manager bootstrap
--
-- lazy.nvim automatically installs itself the first time Neovim runs.
-- This eliminates the old "install vim-plug manually" step.
--
-- Plugins are then declared declaratively below.
----------------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
----------------------------------------------------------------------
-- Plugins
----------------------------------------------------------------------
require("lazy").setup({
-- Colorschemes (kept from your old set)
-- We keep several available so we can quickly switch during long coding
-- sessions. Catppuccin is default because it has excellent Treesitter
-- and LSP semantic token support.
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
{ "dracula/vim", name = "dracula" },
{ "bluz71/vim-moonfly-colors" },
{ "bluz71/vim-nightfly-guicolors" },
{ "jaredgorski/spacecamp" },
{ "tomasr/molokai" },
{ "rhysd/vim-color-spring-night" },
{ "flrnprz/plastic.vim" },
{ "larsbs/vimterial_dark" },
{ "erichdongubler/vim-sublime-monokai" },
{ "euclio/vim-nocturne" },
-- QoL
{ "tpope/vim-eunuch" },
{ "tpope/vim-repeat" },
{ "tpope/vim-unimpaired" },
-- Telescope: modern fuzzy finder and search UI
-- Replaces:
-- :grep
-- fzf.vim
-- file browser plugins
--
-- Uses ripgrep and fd under the hood (external tools installed via apt)
{ "nvim-lua/plenary.nvim" },
{
"nvim-telescope/telescope.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {
defaults = {
layout_strategy = "horizontal",
sorting_strategy = "ascending",
},
},
},
-- Text manipulation helpers
-- sideways.vim → swap function arguments
-- cutlass → safe delete (doesn't overwrite default register)
-- yoink → yank history
-- subversive → powerful substitute operator
{ "drzel/vim-split-line" },
{ "AndrewRadev/sideways.vim" },
{ "svermeulen/vim-cutlass" },
{ "svermeulen/vim-yoink" },
{ "svermeulen/vim-subversive" },
-- Statusline
{ "nvim-lualine/lualine.nvim" },
-- UI
{ "mbbill/undotree" },
{ "lukas-reineke/indent-blankline.nvim", main = "ibl", opts = {} },
-- File explorer
{
"nvim-tree/nvim-tree.lua",
dependencies = { "nvim-tree/nvim-web-devicons" },
opts = {
view = { width = 28, side = "left" },
filters = { dotfiles = false },
git = { enable = true },
},
},
{ "nvim-tree/nvim-web-devicons" },
-- Git
{ "lewis6991/gitsigns.nvim", opts = {} },
-- Commenting
{ "numToStr/Comment.nvim", opts = {} },
-- Built-in LSP stack
-- We do NOT use ALE, coc.nvim, or deoplete.
-- Modern Neovim provides:
-- LSP diagnostics
-- code actions
-- rename
-- goto definition
-- completion
-- natively.
{ "neovim/nvim-lspconfig" },
{ "williamboman/mason.nvim", opts = {} },
{ "williamboman/mason-lspconfig.nvim" },
{ "hrsh7th/nvim-cmp" },
{ "hrsh7th/cmp-nvim-lsp" },
{ "hrsh7th/cmp-buffer" },
{ "hrsh7th/cmp-path" },
{ "L3MON4D3/LuaSnip" },
{ "saadparwaiz1/cmp_luasnip" },
-- Formatting
{ "stevearc/conform.nvim" },
}, {
ui = { border = "rounded" },
})
----------------------------------------------------------------------
-- Editor behavior (equivalent to old :set options in Vimscript)
--
-- These control:
-- • editing behavior
-- • search behavior
-- • UI behavior
-- • file handling
--
-- We intentionally keep this conservative: predictable > clever.
----------------------------------------------------------------------
local opt = vim.opt
opt.timeoutlen = 1000
opt.ttimeoutlen = 10
opt.updatetime = 500
-- Time before CursorHold triggers.
-- Affects LSP diagnostics refresh speed.
opt.history = 500
opt.autoread = true
opt.encoding = "utf-8"
opt.fileformats = { "unix", "dos", "mac" }
-- Use modern state dirs instead of ~/.vim/*
do
local state = vim.fn.stdpath("state")
local backup = state .. "/backup"
local swp = state .. "/swap"
local undo = state .. "/undo"
vim.fn.mkdir(backup, "p")
vim.fn.mkdir(swp, "p")
vim.fn.mkdir(undo, "p")
opt.backupdir = { backup, "." }
opt.directory = { swp, "." }
opt.undodir = undo
opt.undofile = true
end
opt.swapfile = true
opt.backup = false
opt.writebackup = false
opt.linebreak = true
opt.list = false
opt.listchars = { tab = ">-", trail = "~", extends = ">", precedes = "<" }
opt.emoji = false
-- Indentation
opt.tabstop = 8
opt.softtabstop = 2
opt.shiftwidth = 2
opt.shiftround = true
opt.smarttab = true
opt.expandtab = true
opt.autoindent = true
opt.joinspaces = false
-- Searching
opt.incsearch = true
opt.hlsearch = true
opt.ignorecase = true
opt.smartcase = true
opt.magic = true
opt.infercase = true
opt.inccommand = "nosplit"
-- Shows live preview of :substitute.
-- UI
opt.backspace = { "eol", "start", "indent" }
opt.laststatus = 2
opt.scrolloff = 0
opt.hidden = true
-- Allows switching buffers without saving (essential for LSP workflows).
opt.showcmd = true
opt.ruler = true
opt.wrap = false
opt.wildmode = { "list:longest" }
opt.pumblend = 20
-- Makes completion popup slightly transparent.
opt.lazyredraw = true
opt.showmatch = true
opt.matchtime = 2
opt.splitbelow = true
opt.splitright = true
opt.synmaxcol = 300
opt.foldenable = true
opt.foldmethod = "indent"
opt.foldlevelstart = 99
opt.number = true
opt.relativenumber = true
opt.cursorline = true
opt.termguicolors = true
opt.background = "dark"
-- netrw: kept minimal; we use nvim-tree instead
vim.g.netrw_liststyle = 3
vim.g.loaded_matchparen = 1
-- ============================================================
-- Colorscheme
-- ============================================================
vim.cmd.colorscheme("catppuccin")
-- ============================================================
-- Autocommands + Filetype local options
-- ============================================================
local aug = vim.api.nvim_create_augroup("MichaelAutocmds", { clear = true })
vim.api.nvim_create_autocmd("VimResized", {
group = aug,
callback = function()
vim.cmd("wincmd =")
end,
})
local function ft(ftname, settings)
vim.api.nvim_create_autocmd("FileType", {
group = aug,
pattern = ftname,
callback = function()
for k, v in pairs(settings) do
vim.opt_local[k] = v
end
end,
})
end
----------------------------------------------------------------------
-- Filetype-specific indentation rules
--
-- These intentionally override the global indentation settings.
-- Example:
-- Python uses 4 spaces
-- JSON uses 2 spaces
-- C++ kept narrow to avoid wide diffs
----------------------------------------------------------------------
ft("python", { softtabstop = 4, shiftwidth = 4, expandtab = true, colorcolumn = "88" })
ft("c", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("cpp", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("cmake", { softtabstop = 4, shiftwidth = 4, expandtab = true, colorcolumn = "100" })
ft("json", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "80" })
ft("sh", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "80" })
ft("bash", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("vim", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100", textwidth = 100 })
ft("lua", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("zsh", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("yaml", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("dockerfile", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("svelte", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("typescript", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("javascript", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("typescriptreact", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("javascriptreact", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("html", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("css", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("scss", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("jsonc", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "80" })
ft("sql", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("markdown", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
ft("toml", { softtabstop = 2, shiftwidth = 2, expandtab = true, colorcolumn = "100" })
-- ============================================================
-- Helpers (ported Vimscript behaviors)
-- ============================================================
-- Smart Home key:
-- First press → first non-whitespace
-- Second press → true column 0
-- (matches behavior of many IDEs)
local function line_home()
local x = vim.fn.col(".")
vim.cmd.normal({ args = { "^" }, bang = true })
if x == vim.fn.col(".") then
vim.cmd.normal({ args = { "0" }, bang = true })
end
end
-- Toggle preferred text wrapping widths
-- Useful when editing:
-- 80 → commit messages
-- 88 → Python (Black)
-- 120 → code
-- 0 → disabled
local function cycle_textwidth()
local tw = vim.o.textwidth
local next_tw
if tw == 0 then
next_tw = 80
elseif tw == 80 then
next_tw = 88
elseif tw == 88 then
next_tw = 120
elseif tw == 120 then
next_tw = 0
else
next_tw = 0
end
vim.o.textwidth = next_tw
vim.o.colorcolumn = (next_tw == 0) and "" or tostring(next_tw)
vim.notify(("Set textwidth and colorcolumn to %d"):format(next_tw))
end
local function cycle_numbering()
local n, r = vim.wo.number, vim.wo.relativenumber
if not n and not r then
vim.wo.number = true
elseif n and not r then
vim.wo.relativenumber = true
elseif n and r then
vim.wo.number = false
elseif (not n) and r then
vim.wo.relativenumber = false
end
vim.notify(("Set number=%s, relativenumber=%s"):format(tostring(vim.wo.number), tostring(vim.wo.relativenumber)))
end
-- Clears every register.
-- Useful after large pastes or macros.
-- Command: :DeleteRegisters
local function delete_all_registers()
local regs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-"'
for r in regs:gmatch(".") do
vim.fn.setreg(r, {})
end
end
vim.api.nvim_create_user_command("DeleteRegisters", delete_all_registers, {})
local function visual_star_search(cmdtype)
local temp = vim.fn.getreg('"')
vim.cmd.normal({ args = { "gvy" }, bang = true })
local text = vim.fn.getreg('"')
text = vim.fn.escape(text, cmdtype .. "\\*")
text = text:gsub("\n", "\\n"):gsub("%[", "\\["):gsub("~", "\\~")
vim.fn.setreg("/", text)
vim.fn.setreg('"', temp)
end
----------------------------------------------------------------------
-- Keybindings
--
-- Philosophy:
-- • Keep core Vim motions intact
-- • Fix historically annoying defaults
-- • Add IDE-like navigation where helpful
----------------------------------------------------------------------
local map = vim.keymap.set
local nore_silent = { noremap = true, silent = true }
vim.g.mapleader = " "
vim.g.maplocalleader = " "
map({ "n", "v" }, "<Space>", "<Nop>", { silent = true })
map("i", "§", "<Esc>", nore_silent)
map({ "n", "v" }, "K", "<Nop>", nore_silent) -- LSP rebinds hover
map("n", "Q", "<Nop>", nore_silent)
map("n", "ZZ", "<Nop>", nore_silent)
map("n", "ZQ", "<Nop>", nore_silent)
map("v", ".", ":normal .<CR>", nore_silent)
map("n", "c*", "*Ncgn", nore_silent)
map("x", "*", function()
visual_star_search("/")
end, nore_silent)
map("x", "#", function()
visual_star_search("?")
end, nore_silent)
map({ "n", "x" }, "j", "gj", { noremap = true })
map({ "n", "x" }, "k", "gk", { noremap = true })
map({ "n", "x" }, "<Up>", "5k", nore_silent)
map({ "n", "x" }, "<Down>", "5j", nore_silent)
map("n", "}", function()
local c = vim.v.count1
vim.cmd(("keepjumps normal! %d}"):format(c))
end, nore_silent)
map("n", "{", function()
local c = vim.v.count1
vim.cmd(("keepjumps normal! %d{"):format(c))
end, nore_silent)
map({ "n", "v" }, "0", line_home, nore_silent)
map("i", "<Home>", function()
line_home()
end, nore_silent)
-- -------------------------------------------------------------------
-- Cutlass "move" operator mappings
-- Restores your old init.vim behavior:
-- m = cut/move (delete into the "black hole", then be ready to paste)
-- mm = move current line
-- M = move to end of line (cut to EOL)
--
-- These are the standard cutlass mappings (per project instructions).
-- -------------------------------------------------------------------
map("n", "m", "d", { noremap = true })
map("x", "m", "d", { noremap = true })
map("n", "mm", "dd", { noremap = true })
map("n", "M", "D", { noremap = true })
-- Cutlass: keep access to Vim's native mark command
map("n", "gm", "m", { noremap = true })
-- -------------------------------------------------------------------
-- Yoink: restore old init.vim behavior (yank history + paste variants)
-- -------------------------------------------------------------------
vim.g.yoinkMoveCursorToEndOfPaste = 1
-- history swap
map("n", "[y", "<Plug>(YoinkPostPasteSwapBack)", {})
map("n", "]y", "<Plug>(YoinkPostPasteSwapForward)", {})
-- replace default paste with yoink paste
map("n", "p", "<Plug>(YoinkPaste_p)", {})
map("n", "P", "<Plug>(YoinkPaste_P)", {})
map("n", "gp", "<Plug>(YoinkPaste_gp)", {})
map("n", "gP", "<Plug>(YoinkPaste_gP)", {})
-- preserve cursor position when yanking (matches old config)
map("n", "y", "<Plug>(YoinkYankPreserveCursorPosition)", {})
map("x", "y", "<Plug>(YoinkYankPreserveCursorPosition)", {})
-- toggle paste formatting (this mapping existed in your old file;
-- the exact key on the left is hard to see in the raw dump, so I’m using a sane one)
map("n", "<Leader>yp", "<Plug>(YoinkPostPasteToggleFormat)", { silent = true })
-- NERDCommenter-like mappings using <Leader>
do
local api = require("Comment.api")
local map = vim.keymap.set
local opts = { silent = true, remap = true }
-- Toggle comment (linewise)
map("n", "<Leader>cc", "<Plug>(comment_toggle_linewise_current)", opts)
map("x", "<Leader>cc", "<Plug>(comment_toggle_linewise_visual)", opts)
-- Toggle comment (NERDCommenter-style alternative binding)
map("n", "<Leader>c<Space>", "<Plug>(comment_toggle_linewise_current)", opts)
map("x", "<Leader>c<Space>", "<Plug>(comment_toggle_linewise_visual)", opts)
-- Optional: blockwise toggle
map("n", "<Leader>cb", "<Plug>(comment_toggle_blockwise_current)", opts)
map("x", "<Leader>cb", "<Plug>(comment_toggle_blockwise_visual)", opts)
vim.keymap.set("n", "<Leader>cu", api.uncomment.linewise.current, { silent = true })
vim.keymap.set("x", "<Leader>cu", function()
api.uncomment.linewise(vim.fn.visualmode())
end, { silent = true })
end
-- Sideways (argument swapping)
map("n", "g<", ":SidewaysLeft<CR>", { noremap = true, silent = true })
map("n", "g>", ":SidewaysRight<CR>", { noremap = true, silent = true })
-- -------------------------------------------------------------------
-- Subversive: restore old init.vim substitute workflow
-- -------------------------------------------------------------------
map("n", "s", "<Plug>(SubversiveSubstitute)", {})
map("n", "ss", "<Plug>(SubversiveSubstituteLine)", {})
map("n", "S", "<Plug>(SubversiveSubstituteToEndOfLine)", {})
-- SplitLine: pick a stable mapping that doesn't fight Subversive
map("n", "<Leader>S", ":SplitLine<CR>", { noremap = true, silent = true })
-- Enter clears search highlighting (instead of :nohlsearch)
-- but preserves ability to search again.
map("n", "<CR>", function()
if vim.o.hlsearch and vim.v.hlsearch == 1 then
vim.cmd("nohlsearch")
else
vim.o.hlsearch = true
end
end, nore_silent)
map("i", "<C-w>", "<C-g>u<C-w>", nore_silent)
map("i", "<C-u>", "<C-g>u<C-u>", nore_silent)
map("x", "<", "<gv", nore_silent)
map("x", ">", ">gv", nore_silent)
map("n", "<S-h>", ":bprevious<CR>", nore_silent)
map("n", "<S-l>", ":bnext<CR>", nore_silent)
map("n", "<Leader>\\", ":b#<CR>", nore_silent)
map("n", "<Leader>wc", ":close<CR>", nore_silent)
map("n", "|", function()
if vim.v.count == 0 then
return "v"
end
return (":normal! 0" .. vim.v.count .. "|<CR>")
end, { expr = true, noremap = true, silent = true })
map("n", "_", function()
if vim.v.count == 0 then
return "s"
end
return (":normal! " .. vim.v.count .. "_<CR>")
end, { expr = true, noremap = true, silent = true })
map("n", "<Leader>qk", ":cprevious<CR>", nore_silent)
map("n", "<Leader>qj", ":cnext<CR>", nore_silent)
map("n", "<Leader>qh", ":cpfile<CR>", nore_silent)
map("n", "<Leader>ql", ":cnfile<CR>", nore_silent)
map("n", "<F1>", cycle_numbering, nore_silent)
map("n", "<F2>", cycle_textwidth, nore_silent)
map("n", "<F3>", ":set list!<CR>:set list?<CR>", nore_silent)
map("n", "<F4>", ":set wrap!<CR>:set wrap?<CR>", nore_silent)
map("n", "<F5>", ":setlocal paste!<CR>:setlocal paste?<CR>", nore_silent)
map("n", "<F12>", ":source $MYVIMRC<CR>", nore_silent)
map("n", "vc", ":edit $MYVIMRC<CR>", nore_silent)
-- map("n", "vv", ":source $MYVIMRC<CR>", nore_silent)
map("n", "<Leader>di", ":DevInfo<CR>", nore_silent)
map("c", "%%", function()
if vim.fn.getcmdtype() == ":" then
return vim.fn.expand("%:h") .. "/"
end
return "%%"
end, { expr = true, noremap = true })
-- ============================================================
-- Plugin keymaps/config
-- ============================================================
do
local builtin = require("telescope.builtin")
-- Fuzzy search shortcuts
-- ff → files
-- fg → full text search (ripgrep)
-- fb → buffers
-- fh → recent files
map("n", "<Leader>ff", builtin.find_files, nore_silent)
map("n", "<Leader>fg", builtin.live_grep, nore_silent)
map("n", "<Leader>fb", builtin.buffers, nore_silent)
map("n", "<Leader>fh", builtin.oldfiles, nore_silent)
map("n", "<Leader>fl", builtin.current_buffer_fuzzy_find, nore_silent)
map("n", "<Leader>fm", builtin.marks, nore_silent)
map("n", "<Leader>fo", builtin.commands, nore_silent)
map("n", "<Leader>f:", builtin.command_history, nore_silent)
map("n", "<Leader>f/", builtin.search_history, nore_silent)
map("n", "<Leader>fc", builtin.git_commits, nore_silent)
map("n", "<Leader>fG", builtin.git_files, nore_silent)
-- sibling files (directory of current file)
map("n", "<Leader>fs", function()
builtin.find_files({ cwd = vim.fn.expand("%:p:h") })
end, nore_silent)
-- Window picker
-- Telescope does not provide a window selector.
-- This creates an IDE-like "jump to split" chooser.
local function pick_window()
local wins = vim.api.nvim_list_wins()
local items = {}
for _, w in ipairs(wins) do
local b = vim.api.nvim_win_get_buf(w)
local name = vim.api.nvim_buf_get_name(b)
if name == "" then
name = "[No Name]"
end
local ft = vim.bo[b].filetype
local line = vim.api.nvim_win_get_cursor(w)[1]
table.insert(items, {
win = w,
label = string.format("win %d buf %d %s (%s:%d)", w, b, name, ft, line),
})
end
vim.ui.select(items, {
prompt = "Go to window:",
format_item = function(item)
return item.label
end,
}, function(choice)
if choice then
vim.api.nvim_set_current_win(choice.win)
end
end)
end
map("n", "<Leader>fw", pick_window, nore_silent)
end
map("n", "<C-n>", ":NvimTreeToggle<CR>", nore_silent)
map("n", "<Leader>n", ":NvimTreeFindFile<CR>", nore_silent)
map("n", "<Leader>u", ":UndotreeToggle<CR>", nore_silent)
vim.g._indent_guides = true
map("n", "<Leader>i", function()
vim.g._indent_guides = not vim.g._indent_guides
if vim.g._indent_guides then
require("ibl").setup({})
else
require("ibl").setup({ enabled = false })
end
end, nore_silent)
----------------------------------------------------------------------
-- Autocompletion engine
--
-- nvim-cmp is ONLY a UI.
-- Actual intelligence comes from LSP servers.
--
-- Sources:
-- LSP → language server suggestions
-- path → filesystem paths
-- buffer → words in current file
-- luasnip → snippets
----------------------------------------------------------------------
local cmp = require("cmp")
local luasnip = require("luasnip")
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "path" },
{ name = "buffer" },
{ name = "luasnip" },
}),
})
-- ============================================================
-- LSP helpers: roots, PATH injection, venv detection
-- ============================================================
local function path_exists(p)
return p and (vim.uv.fs_stat(p) ~= nil)
end
local function is_win()
return vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1
end
local sep = is_win() and ";" or ":"
local function find_root_from_fname(fname)
local root = vim.fs.root(fname, {
"package.json",
"pnpm-lock.yaml",
"yarn.lock",
"package-lock.json",
"pyproject.toml",
"requirements.txt",
"Pipfile",
".python-version",
".git",
})
return root or vim.fn.getcwd()
end
local function detect_python(root)
-- Priority:
-- 1) $VIRTUAL_ENV (if nvim was launched from an activated venv)
-- 2) common venv dirs in project root: .venv, venv, env, .env
local venv = vim.env.VIRTUAL_ENV
if venv and venv ~= "" then
local p = is_win() and vim.fs.joinpath(venv, "Scripts", "python.exe")
or vim.fs.joinpath(venv, "bin", "python")
if path_exists(p) then
return p
end
end
for _, d in ipairs({ ".venv", "venv", "env", ".env" }) do
local p = is_win() and vim.fs.joinpath(root, d, "Scripts", "python.exe")
or vim.fs.joinpath(root, d, "bin", "python")
if path_exists(p) then
return p
end
end
return nil
end
local function prepend_path(dir)
if not dir or dir == "" or not path_exists(dir) then
return
end
local cur = vim.env.PATH or ""
-- avoid duplicate-prepending
if cur:find(dir, 1, true) then
return
end
vim.env.PATH = dir .. sep .. cur
end
----------------------------------------------------------------------
-- Project environment detection
--
-- This is one of the most important parts of the config.
--
-- It makes Neovim behave like VSCode:
--
-- When you open a project:
-- • node_modules/.bin tools are preferred
-- • Python virtualenv tools are preferred
--
-- That means:
-- eslint, prettier, tsserver, black, isort
-- come from the PROJECT — not global system versions.
--
-- Without this, LSP and formatting frequently break.
----------------------------------------------------------------------
local function apply_project_paths(root)
-- Prefer project-local Node tools (typescript-language-server, eslint, prettier, etc.)
prepend_path(vim.fs.joinpath(root, "node_modules", ".bin"))
-- Prefer project-local Python venv tools (python, black, isort, etc.)
local py = detect_python(root)
if py then
prepend_path(vim.fs.dirname(py))
end
end
----------------------------------------------------------------------
-- Language Server configurations
--
-- mason installs servers
-- vim.lsp.config configures them
--
-- Servers used:
-- ts_ls → TypeScript / Node backend
-- svelte → SvelteKit frontend
-- eslint → linting
-- pyright → Python analysis
-- clangd → C/C++
-- sqls → SQL validation
-- dockerls → Dockerfiles
----------------------------------------------------------------------
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = {
-- Frontend / SvelteKit
"svelte",
"ts_ls",
"eslint",
"tailwindcss",
"html",
"cssls",
-- Config / docs
"jsonls",
"yamlls",
"marksman",
-- Backend / DB
"sqls",
-- Infra
"dockerls",
"docker_compose_language_service",
"bashls",
-- General programming languages (restored)
"pyright",
"clangd",
-- Neovim config
"lua_ls",
},
})
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local function on_attach(_, bufnr)
local function bmap(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, silent = true, noremap = true })
end
vim.keymap.set({ "n", "v" }, "K", vim.lsp.buf.hover, { buffer = bufnr, silent = true, noremap = true })
bmap("n", "<F6>", vim.lsp.buf.rename)
bmap("n", "<F7>", vim.lsp.buf.type_definition)
bmap("n", "<F8>", vim.lsp.buf.definition)
bmap("n", "<F9>", vim.lsp.buf.references)
bmap("n", "<F10>", vim.lsp.buf.implementation)
end
-- Keep PATH in sync with current project so "cmd" can remain static
local lsp_aug = vim.api.nvim_create_augroup("ProjectPaths", { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged" }, {
group = lsp_aug,
callback = function(args)
local fname = (args.buf and vim.api.nvim_buf_get_name(args.buf)) or ""
if fname == "" then
fname = vim.api.nvim_buf_get_name(0)
end
if fname == "" then
apply_project_paths(vim.fn.getcwd())
return
end
local root = find_root_from_fname(fname)
apply_project_paths(root)
end,
})
-- Server configs via vim.lsp.config()
vim.lsp.config("ts_ls", {
cmd = { "typescript-language-server", "--stdio" },
capabilities = capabilities,
on_attach = on_attach,
root_dir = find_root_from_fname,
settings = {
typescript = {
inlayHints = {
includeInlayParameterNameHints = "all",
includeInlayFunctionParameterTypeHints = true,
includeInlayVariableTypeHints = true,
},
},
},
})
vim.lsp.config("eslint", {
cmd = { "vscode-eslint-language-server", "--stdio" },
capabilities = capabilities,
on_attach = on_attach,
root_dir = find_root_from_fname,
settings = {