-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathinit.lua
More file actions
1157 lines (1022 loc) · 35.7 KB
/
init.lua
File metadata and controls
1157 lines (1022 loc) · 35.7 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
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", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
-- run :GoBuild or :GoTestCompile based on the go file
local function build_go_files()
if vim.endswith(vim.api.nvim_buf_get_name(0), "_test.go") then
vim.cmd("GoTestCompile")
else
vim.cmd("GoBuild")
end
end
----------------
--- plugins ---
----------------
require("lazy").setup({
-- colorscheme
{
"ellisonleao/gruvbox.nvim",
priority = 1000, -- make sure to load this before all the other start plugins
config = function ()
require("gruvbox").setup({
contrast = "hard"
})
vim.cmd([[colorscheme gruvbox]])
end,
},
-- automatic dark mode
-- requires: brew install cormacrelf/tap/dark-notify
{
"cormacrelf/dark-notify",
config = function ()
require("dark_notify").run()
end,
},
-- statusline
{
"nvim-lualine/lualine.nvim",
dependencies = { 'nvim-tree/nvim-web-devicons' },
config = function ()
require("lualine").setup({
options = { theme = 'gruvbox' },
sections = {
lualine_c = {
{
'filename',
file_status = true, -- displays file status (readonly status, modified status)
path = 2 -- 0 = just filename, 1 = relative path, 2 = absolute path
}
}
}
})
end,
},
-- you know the drill
{
"fatih/vim-go",
config = function ()
-- we disable most of these features because treesitter and nvim-lsp
-- take care of it
vim.g['go_gopls_enabled'] = 0
vim.g['go_code_completion_enabled'] = 0
vim.g['go_fmt_autosave'] = 0
vim.g['go_imports_autosave'] = 0
vim.g['go_mod_fmt_autosave'] = 0
vim.g['go_doc_keywordprg_enabled'] = 0
vim.g['go_def_mapping_enabled'] = 0
vim.g['go_textobj_enabled'] = 0
vim.g['go_list_type'] = 'quickfix'
end,
},
-- Highlight, edit, and navigate code
{
'nvim-treesitter/nvim-treesitter',
dependencies = {
'nvim-treesitter/nvim-treesitter-textobjects',
},
build = ":TSUpdate",
config = function()
require('nvim-treesitter.configs').setup({
ensure_installed = {
'go',
'gomod',
'proto',
'lua',
'vimdoc',
'vim',
'bash',
'fish',
'json',
'markdown',
'markdown_inline',
'mermaid',
},
indent = { enable = true },
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<space>", -- maps in normal mode to init the node/scope selection with space
node_incremental = "<space>", -- increment to the upper named parent
node_decremental = "<bs>", -- decrement to the previous node
scope_incremental = "<tab>", -- increment to the upper scope (as defined in locals.scm)
},
},
autopairs = {
enable = true,
},
highlight = {
enable = true,
-- Disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 100 * 1024 -- 100 KB
local ok, stats = pcall(vim.uv.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end,
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
['aa'] = '@parameter.outer',
['ia'] = '@parameter.inner',
['af'] = '@function.outer',
['if'] = '@function.inner',
['ac'] = '@class.outer',
['ic'] = '@class.inner',
["iB"] = "@block.inner",
["aB"] = "@block.outer",
},
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
[']]'] = '@function.outer',
},
goto_next_end = {
[']['] = '@function.outer',
},
goto_previous_start = {
['[['] = '@function.outer',
},
goto_previous_end = {
['[]'] = '@function.outer',
},
},
swap = {
enable = true,
swap_next = {
['<leader>sn'] = '@parameter.inner',
},
swap_previous = {
['<leader>sp'] = '@parameter.inner',
},
},
},
})
end,
},
-- search selection via *
{ 'bronson/vim-visual-star-search' },
{
'dinhhuy258/git.nvim',
config = function ()
require("git").setup()
end,
},
-- file explorer
{
"nvim-tree/nvim-tree.lua",
version = "*",
dependencies = { 'nvim-tree/nvim-web-devicons' },
config = function()
require("nvim-tree").setup({
sort_by = "case_sensitive",
filters = {
dotfiles = true,
},
on_attach = function(bufnr)
local api = require('nvim-tree.api')
local function opts(desc)
return {
desc = 'nvim-tree: ' .. desc,
buffer = bufnr,
noremap = true,
silent = true,
nowait = true,
}
end
api.config.mappings.default_on_attach(bufnr)
vim.keymap.set('n', 's', api.node.open.vertical, opts('Open: Vertical Split'))
vim.keymap.set('n', 'i', api.node.open.horizontal, opts('Open: Horizontal Split'))
vim.keymap.set('n', 'u', api.tree.change_root_to_parent, opts('Up'))
end
})
end,
},
-- save my last cursor position
{
"ethanholz/nvim-lastplace",
config = function()
require("nvim-lastplace").setup({
lastplace_ignore_buftype = {"quickfix", "nofile", "help"},
lastplace_ignore_filetype = {"gitcommit", "gitrebase", "svn", "hgcommit"},
lastplace_open_folds = true
})
end,
},
{
"AndrewRadev/splitjoin.vim"
},
{
"windwp/nvim-autopairs",
config = function()
local npairs = require("nvim-autopairs")
npairs.setup {
check_ts = true,
}
-- Remove backtick pairing
local Rule = require('nvim-autopairs.rule')
npairs.remove_rule('`')
end
},
{
"coder/claudecode.nvim",
lazy = false,
opts = {
terminal_cmd = "/Users/fatih/.local/bin/claude",
terminal = {
provider = "none", -- no UI actions; server + tools remain available
},
},
cmd = {
"ClaudeCode",
"ClaudeCodeFocus",
"ClaudeCodeSelectModel",
"ClaudeCodeAdd",
"ClaudeCodeSend",
"ClaudeCodeTreeAdd",
"ClaudeCodeDiffAccept",
"ClaudeCodeDiffDeny",
},
keys = {
{ "<leader>c", nil, desc = "AI/Claude Code" },
{ "<C-t>", "<cmd>ClaudeCode<cr>", desc = "Toggle Claude" },
{ "<leader>cf", "<cmd>ClaudeCodeFocus<cr>", desc = "Focus Claude" },
{ "<leader>cr", "<cmd>ClaudeCode --resume<cr>", desc = "Resume Claude" },
{ "<leader>cC", "<cmd>ClaudeCode --continue<cr>", desc = "Continue Claude" },
{ "<leader>cm", "<cmd>ClaudeCodeSelectModel<cr>", desc = "Select Claude model" },
{ "<leader>ca", "<cmd>ClaudeCodeAdd %<cr>", desc = "Add current buffer" },
{ "<leader>cs", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
{
"<leader>cs",
"<cmd>ClaudeCodeTreeAdd<cr>",
desc = "Add file",
ft = { "NvimTree", "neo-tree", "oil" },
},
{ "<leader>da", "<cmd>ClaudeCodeDiffAccept<cr>", desc = "Accept diff" },
{ "<leader>dd", "<cmd>ClaudeCodeDiffDeny<cr>", desc = "Deny diff" },
}
},
{
"sourcegraph/amp.nvim",
branch = "main",
lazy = false,
opts = { auto_start = true, log_level = "info" },
},
{
'brianhuster/live-preview.nvim',
dependencies = {
'ibhagwan/fzf-lua',
'folke/snacks.nvim',
},
},
-- { -- Fuzzy Finder (files, lsp, etc)
{
"ibhagwan/fzf-lua",
dependencies = {
"nvim-tree/nvim-web-devicons",
"elanmed/fzf-lua-frecency.nvim",
},
opts = {},
config = function()
require("fzf-lua").register_ui_select()
require('fzf-lua').setup {
oldfiles = {
-- include current sessions in old_files mode
include_current_session = true,
},
winopts = {
-- split = "belowright 10new",
backdrop = 100,
border = "single",
preview = {
hidden = true,
default = "bat",
border = "rounded",
title = false,
layout = "vertical",
horizontal = "right:50%",
},
},
git = {
files = {
cwd_header = false,
prompt = '❯ ',
cmd = 'git ls-files --exclude-standard',
multiprocess = true, -- run command in a separate process
git_icons = false, -- show git icons?
file_icons = false, -- show file icons (true|"devicons"|"mini")?
color_icons = false, -- colorize file|git icons
},
},
files = {
git_files = false,
cwd_header = false,
cwd_prompt = true,
file_icons = false,
}
}
-- Setup frecency for fzf-lua (tracks frequently + recently used files)
require('fzf-lua-frecency').setup()
vim.keymap.set("n", "<C-p>", function()
-- Get git root to use as cwd (handles autochdir)
local git_root = vim.fn.systemlist("git rev-parse --show-toplevel")[1]
if vim.v.shell_error ~= 0 then
git_root = vim.fn.getcwd()
end
require('fzf-lua-frecency').frecency({
file_icons = false,
git_icons = false,
cwd_only = true, -- only show files from current repo
cwd = git_root, -- use git root, not autochdir path
})
end, {})
vim.keymap.set("n", "<C-b>", require("fzf-lua").files, {})
vim.keymap.set("n", "<C-g>", require("fzf-lua").lsp_document_symbols, {})
end
},
-- LSP Plugins
{
-- `lazydev` configures Lua LSP for your Neovim config, runtime and plugins
-- used for completion, annotations and signatures of Neovim apis
'folke/lazydev.nvim',
ft = 'lua',
opts = {
library = {
-- Load luvit types when the `vim.uv` word is found
{ path = 'luvit-meta/library', words = { 'vim%.uv' } },
},
},
},
{ 'Bilal2453/luvit-meta', lazy = true },
-- Useful status updates for LSP
{ 'j-hui/fidget.nvim', opts = {} },
-- Extra capabilities for nvim-cmp
{ 'hrsh7th/cmp-nvim-lsp' },
{
"L3MON4D3/LuaSnip",
dependencies = { "rafamadriz/friendly-snippets" },
config = function()
require("luasnip.loaders.from_vscode").lazy_load()
end
},
-- autocompletion
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"onsails/lspkind-nvim",
"lukas-reineke/cmp-under-comparator",
},
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
local types = require("cmp.types")
local compare = require("cmp.config.compare")
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
luasnip.config.setup {}
local modified_priority = {
[types.lsp.CompletionItemKind.Variable] = types.lsp.CompletionItemKind.Method,
[types.lsp.CompletionItemKind.Snippet] = 0, -- top
[types.lsp.CompletionItemKind.Keyword] = 0, -- top
[types.lsp.CompletionItemKind.Text] = 100, -- bottom
}
local function modified_kind(kind)
return modified_priority[kind] or kind
end
require('cmp').setup({
preselect = false,
completion = {
completeopt = "menu,menuone,preview,noselect",
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
formatting = {
format = lspkind.cmp_format {
with_text = true,
menu = {
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
nvim_lua = "[Lua]",
},
},
},
sorting = {
priority_weight = 1.0,
comparators = {
compare.offset,
compare.exact,
compare.score,
compare.locality,
function(entry1, entry2) -- sort by length ignoring "=~"
local len1 = string.len(string.gsub(entry1.completion_item.label, "[=~()_]", ""))
local len2 = string.len(string.gsub(entry2.completion_item.label, "[=~()_]", ""))
if len1 ~= len2 then
return len1 - len2 < 0
end
end,
compare.recently_used,
function(entry1, entry2) -- sort by compare kind (Variable, Function etc)
local kind1 = modified_kind(entry1:get_kind())
local kind2 = modified_kind(entry2:get_kind())
if kind1 ~= kind2 then
return kind1 - kind2 < 0
end
end,
require("cmp-under-comparator").under,
compare.kind,
},
},
matching = {
disallow_fuzzy_matching = true,
disallow_fullfuzzy_matching = true,
disallow_partial_fuzzy_matching = true,
disallow_partial_matching = false,
disallow_prefix_unmatching = true,
},
mapping = cmp.mapping.preset.insert {
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<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" }),
},
window = { documentation = cmp.config.window.bordered(), completion = cmp.config.window.bordered() },
view = {
entries = {
name = "custom",
selection_order = "near_cursor",
},
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Insert,
},
sources = {
{ name = 'nvim_lsp' },
{ name = "luasnip", keyword_length = 2},
{ name = "buffer", keyword_length = 5},
},
performance = {
max_view_entries = 20,
},
})
end,
},
})
----------------
--- LSP Setup (Neovim 0.11 native) ---
----------------
-- Get capabilities from cmp-nvim-lsp for better completion
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
-- gopls (install: go install golang.org/x/tools/gopls@latest)
vim.lsp.config('gopls', {
cmd = { 'gopls' },
filetypes = { 'go', 'gomod', 'gowork', 'gotmpl' },
root_markers = { 'go.mod', 'go.work', '.git' },
capabilities = capabilities,
})
-- lua-language-server (install: brew install lua-language-server)
vim.lsp.config('lua_ls', {
cmd = { 'lua-language-server' },
filetypes = { 'lua' },
root_markers = { '.luarc.json', '.luarc.jsonc', '.stylua.toml', 'stylua.toml', '.git' },
capabilities = capabilities,
settings = {
Lua = {
completion = {
callSnippet = 'Replace',
},
},
},
})
-- Enable the LSP servers
vim.lsp.enable({ 'gopls', 'lua_ls' })
----------------
--- SETTINGS ---
----------------
-- disable netrw at the very start of our init.lua, because we use nvim-tree
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.termguicolors = true -- Enable 24-bit RGB colors
vim.opt.number = true -- Show line numbers
vim.opt.showmatch = true -- Highlight matching parenthesis
vim.opt.splitright = true -- Split windows right to the current windows
vim.opt.splitbelow = true -- Split windows below to the current windows
vim.opt.autowrite = true -- Automatically save before :next, :make etc.
vim.opt.autochdir = true -- Change CWD when I open a file
vim.opt.mouse = 'a' -- Enable mouse support
vim.opt.clipboard = 'unnamedplus' -- Copy/paste to system clipboard
vim.opt.swapfile = false -- Don't use swapfile
vim.opt.ignorecase = true -- Search case insensitive...
vim.opt.smartcase = true -- ... but not it begins with upper case
vim.opt.completeopt = 'menuone,noinsert,noselect' -- Autocomplete options
vim.opt.undofile = true
vim.opt.undodir = vim.fn.stdpath("data") .. "undo"
-- Indent Settings
-- I'm in the Spaces camp (sorry Tabs folks), so I'm using a combination of
-- settings to insert spaces all the time.
vim.opt.expandtab = true -- expand tabs into spaces
vim.opt.shiftwidth = 2 -- number of spaces to use for each step of indent.
vim.opt.tabstop = 2 -- number of spaces a TAB counts for
vim.opt.autoindent = true -- copy indent from current line when starting a new line
vim.opt.wrap = true
-- This comes first, because we have mappings that depend on leader
-- With a map leader it's possible to do extra key combinations
-- i.e: <leader>w saves the current file
vim.g.mapleader = ','
-- Fast saving
vim.keymap.set('n', '<Leader>w', ':write!<CR>')
vim.keymap.set('n', '<Leader>q', ':q!<CR>', { silent = true })
-- Some useful quickfix shortcuts for quickfix
vim.keymap.set('n', '<C-n>', '<cmd>cnext<CR>zz')
vim.keymap.set('n', '<C-m>', '<cmd>cprev<CR>zz')
vim.keymap.set('n', '<leader>a', '<cmd>cclose<CR>')
-- Exit on jj and jk
vim.keymap.set('n', 'j', 'gj')
vim.keymap.set('n', 'k', 'gk')
-- Exit on jj and jk
vim.keymap.set('i', 'jj', '<ESC>')
vim.keymap.set('i', 'jk', '<ESC>')
-- Copy current filepath to system clipboard (relative to git root, fallback to absolute path)
vim.keymap.set('n', '<Leader>e', function()
local git_prefix = vim.fn.system('git rev-parse --show-prefix'):gsub('\n', '')
local path
if vim.v.shell_error == 0 then
path = git_prefix .. vim.fn.expand('%')
else
path = vim.fn.expand('%:p')
end
vim.fn.setreg('+', path)
print('Copied to clipboard: ' .. path)
end, { silent = true })
-- Copy absolute filepath to system clipboard
vim.keymap.set('n', '<Leader>r', function()
local path = vim.fn.expand('%:p')
vim.fn.setreg('+', path)
print('Copied to clipboard: ' .. path)
end, { silent = true })
-- Remove search highlight
vim.keymap.set('n', '<Leader><space>', ':nohlsearch<CR>')
-- Search mappings: These will make it so that going to the next one in a
-- search will center on the line it's found in.
vim.keymap.set('n', 'n', 'nzzzv', {noremap = true})
vim.keymap.set('n', 'N', 'Nzzzv', {noremap = true})
-- Don't jump forward if I higlight and search for a word
local function stay_star()
local sview = vim.fn.winsaveview()
local args = string.format("keepjumps keeppatterns execute %q", "sil normal! *")
vim.api.nvim_command(args)
vim.fn.winrestview(sview)
end
vim.keymap.set('n', '*', stay_star, {noremap = true, silent = true})
-- We don't need this keymap, but here we are. If I do a ctrl-v and select
-- lines vertically, insert stuff, they get lost for all lines if we use
-- ctrl-c, but not if we use ESC. So just let's assume Ctrl-c is ESC.
vim.keymap.set('i', '<C-c>', '<ESC>')
-- If I visually select words and paste from clipboard, don't replace my
-- clipboard with the selected word, instead keep my old word in the
-- clipboard
vim.keymap.set("x", "p", "\"_dP")
-- Better split switching
vim.keymap.set('', '<C-j>', '<C-W>j')
vim.keymap.set('', '<C-k>', '<C-W>k')
vim.keymap.set('', '<C-h>', '<C-W>h')
vim.keymap.set('', '<C-l>', '<C-W>l')
-- Terminal mode window switching
vim.keymap.set('t', '<C-h>', '<C-\\><C-N><C-w>h')
vim.keymap.set('t', '<C-j>', '<C-\\><C-N><C-w>j')
vim.keymap.set('t', '<C-k>', '<C-\\><C-N><C-w>k')
vim.keymap.set('t', '<C-l>', '<C-\\><C-N><C-w>l')
-- Visual linewise up and down by default (and use gj gk to go quicker)
vim.keymap.set('n', '<Up>', 'gk')
vim.keymap.set('n', '<Down>', 'gj')
-- Yanking a line should act like D and C
vim.keymap.set('n', 'Y', 'y$')
if vim.fn.getenv("TERM_PROGRAM") == "ghostty" then
vim.opt.title = true
local function update_title()
local root = vim.fn.systemlist('git rev-parse --show-toplevel 2>/dev/null')
if vim.v.shell_error == 0 and #root > 0 and root[1] ~= '' then
vim.opt.titlestring = vim.fn.fnamemodify(root[1], ':t')
else
vim.opt.titlestring = vim.fn.fnamemodify(vim.fn.getcwd(), ':t')
end
end
update_title()
vim.api.nvim_create_autocmd({'DirChanged', 'VimEnter'}, {
callback = update_title,
})
end
-- Open help window in a vertical split to the right.
vim.api.nvim_create_autocmd("BufWinEnter", {
group = vim.api.nvim_create_augroup("help_window_right", {}),
pattern = { "*.txt" },
callback = function()
if vim.o.filetype == 'help' then vim.cmd.wincmd("L") end
end
})
-- git.nvim
vim.keymap.set('n', '<leader>gb', '<CMD>lua require("git.blame").blame()<CR>')
vim.keymap.set('n', '<leader>go', "<CMD>lua require('git.browse').open(false)<CR>")
vim.keymap.set('x', '<leader>go', ":<C-u> lua require('git.browse').open(true)<CR>")
-- old habits
vim.api.nvim_create_user_command("GBrowse", 'lua require("git.browse").open(true)<CR>', {
range = true,
bang = true,
nargs = "*",
})
-- File-tree mappings
vim.keymap.set('n', '<leader>n', ':NvimTreeToggle<CR>', { noremap = true })
vim.keymap.set('n', '<leader>f', ':NvimTreeFindFileToggle!<CR>', { noremap = true })
-- vim-go
vim.keymap.set('n', '<leader>b', build_go_files)
vim.api.nvim_create_user_command("A", ":lua vim.api.nvim_call_function('go#alternate#Switch', {true, 'edit'})<CR>", {})
vim.api.nvim_create_user_command("AV", ":lua vim.api.nvim_call_function('go#alternate#Switch', {true, 'vsplit'})<CR>", {})
vim.api.nvim_create_user_command("AS", ":lua vim.api.nvim_call_function('go#alternate#Switch', {true, 'split'})<CR>", {})
-- Go uses gofmt, which uses tabs for indentation and spaces for aligment.
-- Hence override our indentation rules.
vim.api.nvim_create_autocmd('Filetype', {
group = vim.api.nvim_create_augroup('setIndent', { clear = true }),
pattern = { 'go' },
command = 'setlocal noexpandtab tabstop=4 shiftwidth=4'
})
-- -- ClaudeCode mapping
-- vim.keymap.set('n', '<C-t>', ':ClaudeCode<CR>', { noremap = true, silent = true })
-- vim.keymap.set('n', '<leader>aa', '<cmd>ClaudeCodeAdd %<cr>', { desc = "Add current buffer" })
-- vim.keymap.set({'n', 'v'}, '<leader>as', '<cmd>ClaudeCodeSend<cr>', { desc = "Send to Claude" })
-- -- Amp mapping
vim.keymap.set('n', '<leader>ab', '<cmd>AmpBuffer<cr>', { desc = "Create Amp buffer" })
vim.keymap.set('x', '<leader>ab', ":'<,'>AmpBuffer<CR>", { desc = "Create Amp buffer from selection" })
vim.keymap.set('n', '<leader>as', '<cmd>AmpSendBuffer<cr>', { desc = "Send buffer to Amp" })
vim.keymap.set('v', '<leader>as', ":'<,'>AmpPromptRef<CR>", { desc = "Send selection to Prompt" })
-- vim.keymap.set('n', '<leader>am', '<cmd>AmpMessage %<cr>', { desc = "Send message to Amp" })
vim.keymap.set('x', '<leader>aa', ":'<,'>AmpAppendBuffer<CR>", { desc = "Append selection to Amp buffer" })
-- Add selected text directly to prompt
vim.api.nvim_create_user_command("AmpPromptSelection", function(opts)
local lines = vim.api.nvim_buf_get_lines(0, opts.line1 - 1, opts.line2, false)
local text = table.concat(lines, "\n")
local amp_message = require("amp.message")
amp_message.send_to_prompt(text)
end, {
range = true,
desc = "Add selected text to Amp prompt",
})
-- Add file+selection reference to prompt
vim.api.nvim_create_user_command("AmpPromptRef", function(opts)
local bufname = vim.api.nvim_buf_get_name(0)
if bufname == "" then
print("Current buffer has no filename")
return
end
local relative_path = vim.fn.fnamemodify(bufname, ":.")
local ref = "@" .. relative_path
if opts.line1 ~= opts.line2 then
ref = ref .. "#L" .. opts.line1 .. "-" .. opts.line2
elseif opts.line1 > 1 then
ref = ref .. "#L" .. opts.line1
end
local amp_message = require("amp.message")
amp_message.send_to_prompt(ref)
end, {
range = true,
desc = "Add file reference (with selection) to Amp prompt",
})
vim.api.nvim_create_user_command("AmpMessage", function(opts)
local message = opts.args
if message == "" then
print("Please provide a message to send")
return
end
local amp_message = require("amp.message")
amp_message.send_message(message)
end, {
nargs = "*",
desc = "Send a message to Amp",
})
-- Open new scratch buffer for Amp prompts
vim.api.nvim_create_user_command("AmpBuffer", function(opts)
local lines = {}
-- Only get lines if we have a valid range
local has_range = (opts.range > 0) and (opts.line2 > 0) and (opts.line2 >= opts.line1)
if has_range then
lines = vim.api.nvim_buf_get_lines(0, opts.line1 - 1, opts.line2, false)
end
-- Check if amp-scratch buffer already exists
local existing_buf = nil
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buf) then
local buf_name = vim.api.nvim_buf_get_name(buf)
if buf_name:match("amp%-scratch$") then
existing_buf = buf
break
end
end
end
local target_buf
if existing_buf then
-- Check if existing buffer is already visible in a window
local existing_win = nil
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == existing_buf then
existing_win = win
break
end
end
if existing_win then
-- Switch to existing window
vim.api.nvim_set_current_win(existing_win)
else
-- Open existing buffer in a vertical split
vim.cmd("vsplit")
vim.api.nvim_win_set_buf(0, existing_buf)
end
-- Only append new lines if we have a selection
if #lines > 0 then
local existing_lines = vim.api.nvim_buf_get_lines(existing_buf, 0, -1, false)
if #existing_lines > 0 and existing_lines[#existing_lines] ~= "" then
table.insert(existing_lines, "") -- Add blank line separator
end
for _, line in ipairs(lines) do
table.insert(existing_lines, line)
end
vim.api.nvim_buf_set_lines(existing_buf, 0, -1, false, existing_lines)
end
target_buf = existing_buf
else
-- Create new buffer
vim.cmd("vsplit")
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_win_set_buf(0, buf)
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "hide"
vim.bo[buf].swapfile = false
vim.api.nvim_buf_set_name(buf, "amp-scratch")
-- Populate buffer with selected lines
if #lines > 0 then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
end
target_buf = buf
end
-- Add empty lines at end and move cursor there
local current_lines = vim.api.nvim_buf_get_lines(target_buf, 0, -1, false)
-- Only add newlines if buffer has content
if #current_lines > 0 and current_lines[1] ~= "" then
vim.api.nvim_buf_set_lines(target_buf, -1, -1, false, { "", "" })
end
local line_count = vim.api.nvim_buf_line_count(target_buf)
vim.api.nvim_win_set_cursor(0, { line_count, 0 })
end, {
nargs = 0,
desc = "Open scratch buffer for Amp prompts",
range = true,
})
-- Send entire buffer contents and close the buffer
vim.api.nvim_create_user_command("AmpSendBuffer", function(opts)
local buf = vim.api.nvim_get_current_buf()
local buf_name = vim.api.nvim_buf_get_name(buf)
-- Check if we're in an amp-scratch buffer
if not buf_name:match("amp%-scratch$") then
print("AmpSendBuffer can only be used in amp-scratch buffers")
return
end
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local content = table.concat(lines, "\n")
-- Check if content is empty
if content:match("^%s*$") then
print("Buffer is empty, nothing to send")
return
end
-- Check if Amp server is running
local amp = require("amp")
if not amp.state.server then
print("Amp server is not running - start it first with :AmpStart")
return
end
-- Check if there are actually connected clients
local server_status = amp.state.server.get_status and amp.state.server.get_status()
if not server_status or server_status.client_count == 0 then
print("No Amp clients connected")
return
end
-- Store buffer info before attempting send (failsafe)
local should_close_buffer = false
local amp_message = require("amp.message")
-- Send message and check return value
local success = amp_message.send_message(content)
if success then
should_close_buffer = true
print("Message sent to Amp")
else
print("Failed to send to Amp - connection failed")
end
-- Only close buffer if we explicitly marked it as safe to close
if should_close_buffer then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
vim.api.nvim_buf_delete(buf, { force = true })
end
end, {
nargs = "?",
desc = "Send current buffer contents to Amp",
})
-- Append selected lines to amp-scratch buffer in @filename#L12-46 format
vim.api.nvim_create_user_command("AmpAppendBuffer", function(opts)
local bufname = vim.api.nvim_buf_get_name(0)
if bufname == "" then
print("Current buffer has no filename")
return
end
local relative_path = vim.fn.fnamemodify(bufname, ":.")
local ref = "@" .. relative_path
if opts.line1 ~= opts.line2 then
ref = ref .. "#L" .. opts.line1 .. "-" .. opts.line2
elseif opts.line1 > 1 then
ref = ref .. "#L" .. opts.line1
end
-- Find amp-scratch buffer
local scratch_buf = nil
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local buf_name = vim.api.nvim_buf_get_name(buf)
if buf_name:match("amp%-scratch$") then
scratch_buf = buf
break