-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
1397 lines (1315 loc) · 79.9 KB
/
init.lua
File metadata and controls
1397 lines (1315 loc) · 79.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
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
-- For rewriting init.vim in lua, see https://www.notonlycode.org/neovim-lua-config/ and https://vonheikemen.github.io/devlog/tools/configuring-neovim-using-lua/
-- FOR INSTALL: Run the console commands, and run github authentification for copilot
-- sudo pacman -S git zathura zathura-pdf-mupdf pyright bash-language-server texlab ripgrep fzy gitui tree-sitter-cli
-- pip install nvr # For neovim remote opening into same window, as well as editor commands called by nvim terminal opening in parent neovim instance
-- TODO: Markdown preview does not work yet (It does work on a fresh linux installation)
-- TODO: When having initialized a new git repo in a folder in which the main latex document lies, the GIT.gitignore does not yet get generated, as per the eekhof-latex-package, when compiled with vimtex
-- TODO: A weird bug with nvim-cmp occured after typing a linebreak, which displayed some red error message in the console, inserted the last completion that was made at that wrong spot, and crashed the completion so that no other completions were suggested in that buffer, until it was restarted
-- TODO: Jumping from PDF in Zathura to latex source code with ctrl+click does not yet work, for start of solution see maybe https://www.ejmastnak.com/tutorials/vim-latex/pdf-reader/
-- TODO: Die Completionvorschläge brechen ab, wenn im vorgeschlagenen Wort ein Umlaut ist (Zumindest bei reinen Textvorschlägen)
-- TODO: Ersetze completion plugin mit https://vim.fandom.com/wiki/Improve_completion_popup_menu und https://georgebrock.github.io/talks/vim-completion/
-- TODO: Ersetze pyright mit basedpyright, ist besser, community driven und braucht kein node-js sondern nur pip zur installation
-- TODO: Ersetze surrounding pair defs mit mini.pairs, und surrounds mit mini.surround
-- TODO: Nutze die Treesitter-Plugins https://github.com/windwp/nvim-ts-autotag , https://github.com/nvim-treesitter/nvim-treesitter-context , https://github.com/nvim-treesitter/nvim-treesitter-textobjects , https://github.com/nvim-treesitter/nvim-treesitter-refactor
-- TODO: Implementiere true-false-switch mit CTRL-x/a , siehe z.b. https://vi.stackexchange.com/questions/5213/swap-values-true-and-false-via-ctrla-ctrlx
-- TODO: Fuer Jupyter-Support nutze Molten.Nvim, https://github.com/benlubas/molten-nvim , ist ein successor von https://github.com/dccsillag/magma-nvim . Eigentlich wäre https://www.vim.org/scripts/script.php?script_id=6064 also https://github.com/PhilGrunewald/vim-py-kid cooler, aber funktioniert nicht
-- TODO: Remap Ctrl+6 so that it goes to last existing buffer instead of last buffer, otherwise going back a buffer breaks when using the vim file browser
-- TODO: Use yazi as file browser
-- TODO: Make F5 execution only execute the program if compilation was successful DONE?
-- TODO: TODO: Add a autocmd BufWritePost that will copy the file over on save with w | !cp % .REMOTE_DIR/% if a softlink .REMOTE_DIR is present in the current directory
-- Colorscheme -----------------------------
-- vim.cmd.colorscheme('evening')
-- vim.api.nvim_set_hl(0, "Normal", { ctermfg=231, ctermbg=000, cterm=NONE }) -- Prevent vim from changing background color, see https://www.reddit.com/r/vim/comments/5u5ujc/suggestions_on_black_dark_colorscheme/
-- vim.api.nvim_set_hl(0, "CursorColumn", { ctermfg=NONE, ctermbg=237, cterm=NONE }) -- Change bg to darker grey to match true black
-- vim.api.nvim_set_hl(0, "CursorLine", { ctermfg=NONE, ctermbg=237, cterm=NONE }) -- Change bg to darker grey to match true black
-- vim.api.nvim_set_hl(0, "CursorLineNr", { ctermfg=226, ctermbg=237, cterm=NONE }) -- Change bg to darker grey to match true black
-- vim.api.nvim_set_hl(0, "EndOfBuffer", { ctermfg=153, ctermbg=000, cterm=NONE }) -- Change bg to true black
-- vim.api.nvim_set_hl(0, "StatusLine", { ctermfg=000, ctermbg=231, cterm=bold }) -- Change fg to true black
-- vim.api.nvim_set_hl(0, "StatusLineNC", { ctermfg=000, ctermbg=252, cterm=NONE }) -- Change fg to true black
-- vim.api.nvim_set_hl(0, "TabLineSel", { ctermfg=000, ctermbg=231, cterm=bold }) -- Change fg to true black
-- vim.api.nvim_set_hl(0, "TabLine", { ctermfg=000, ctermbg=252, cterm=NONE }) -- Change fg to true black
-- vim.api.nvim_set_hl(0, "NonText", { ctermfg=153, ctermbg=000, cterm=NONE }) -- Change bg to true black
-- vim.api.nvim_set_hl(0, "Ignore", { ctermfg=000, ctermbg=NONE, cterm=NONE }) -- Change fg to true black
-- vim.api.nvim_set_hl(0, "FloatBorder", { ctermfg=231, ctermbg=000, cterm=NONE }) -- Change window border color to black and white, this should be default, but somehow doesn't work when calling nvim_open_win
-- Transparency:
vim.api.nvim_set_hl(0, "Normal", { ctermfg=231, guibd=NONE, ctermbg=NONE, cterm=NONE }) -- For transparency of background
vim.api.nvim_set_hl(0, "NonText", { ctermfg=153, ctermbg=NONE, cterm=NONE }) -- For transparency of background
vim.api.nvim_set_hl(0, "EndOfBuffer", { ctermfg=153, ctermbg=NONE, cterm=NONE }) -- For transparency of background
-- General options -----------------------------
vim.api.nvim_create_autocmd('FileType', { -- Enable spellchecking but only for text files
pattern = { 'tex', 'bib', 'typ', 'text', 'markdown'},
callback = function()
vim.o.spell = true -- Enable spell checking per default
vim.o.spelllang = 'en_us' -- Default spell checking language to US English
vim.o.wrap = true -- Enable line wrapping
end,
group = spelling
})
vim.o.number = true -- Display line number of current line
vim.o.relativenumber = true -- Display relative line number of all other lines
vim.o.signcolumn = 'yes' -- Always show sign column, so that line numbers do not jump around when language server blends in warnings and errors when switching out of insert mode
vim.o.cursorline = true -- Highlight current line
vim.o.cursorcolumn = true -- Highlight current column
vim.o.whichwrap = 'b,s,h,l,<,>,[,]' -- Allow wrapping around lines
vim.o.undofile = true -- Enable persistent undo
vim.o.clipboard = 'unnamed,unnamedplus' -- Set the default register to the clipboard that is accessed by CTRL+C and CTRL+V, together with the line above it just takes the last one of the two that was used (I mostly use the latter) (Note the plus instead of caret, it is necessary here) Source for both: https://www.reddit.com/r/vim/comments/3ae4qf/psa_set_clipboardunnamed/
vim.api.nvim_create_autocmd('TextYankPost', { -- Highlight yanked text, useful if yanking outside of visual mode, source: https://github.com/VonHeikemen/nvim-starter/blob/00-minimal/init.lua
group = grp,
desc = 'Highlight on yank',
callback = function()
vim.highlight.on_yank({higroup = 'Visual', timeout = 300})
end,
})
-- Remember folded text:
-- vim.api.nvim_create_autocmd({"BufWinLeave"}, {
-- pattern = {"*.*"},
-- desc = "save view (folds), when closing file",
-- command = "mkview",
-- })
-- vim.api.nvim_create_autocmd({"BufWinEnter"}, {
-- pattern = {"*.*"},
-- desc = "load view (folds), when opening file",
-- command = "silent! loadview"
-- })
-- Set the titlestring, which is displayed e.g. in the window titles in I3:
vim.o.title = true
local augroup = vim.api.nvim_create_augroup("SetTitleString", { clear = true })
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*",
group = augroup,
callback = function()
vim.opt.titlestring = "%t%( %M%)%( (%{expand('%:~:h')})%)%a - Nvim"
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "typst",
callback = function()
vim.bo.tabstop = 4
vim.bo.shiftwidth = 4
vim.bo.softtabstop = 4
end,
desc = "Set indentation for Typst files"
})
vim.o.autochdir = true -- Change current bash working directory to current file directory on opening file (may not work as well as the ones above with the plugin, see https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file#Automatically_change_the_current_directory)
vim.o.lazyredraw = true -- Dont update screen during macro and script execution to save resources
vim.o.ttyfast = true -- Speed up scrolling in Vim
vim.o.scrolloff = 5 -- Keep 5 lines above and below cursor when scrolling
vim.o.sidescrolloff = 20 -- Keep 30 characters left and right of cursor when scrolling
vim.o.ignorecase = true -- Makes searches case insensitive
vim.o.smartcase = true -- Search case sensitive if it contains uppercase letter
-- The following lines for tabs are from https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces
vim.o.shiftwidth = 4 -- Set tab width to 4 spaces
vim.o.smarttab = true -- Enable smarttabs
vim.o.softtabstop = 4 -- Set width for tabs
vim.o.expandtab = true -- Use spaces instead of tabs
vim.o.autoindent = true -- Autoindent new lines
vim.o.tabstop = 4 -- Set display tab width to 4 spaces, otherwise arrows as tab symbols get elongated
vim.o.list = true -- Configure this for display of tabs and spaces characters, see comment on Source: https://vi.stackexchange.com/questions/31811/neovim-lua-config-how-to-append-to-listchars/37971#37971
vim.o.listchars = 'tab:――►,space:·' -- Show tabs and spaces
vim.o.wrap = true -- Disable line wrapping
vim.o.showbreak = '│' -- Show this character when a line is wrapped
vim.o.linebreak = true -- Break only at this character
vim.o.breakindent = true -- Break only at this character
vim.api.nvim_set_hl(0, 'NonText', {ctermbg='NONE', ctermfg='DarkGray'}) -- Disable background highlighting of linebreak/linewrap characters (Also affects other non-text characters)
vim.o.showmatch = true--TODO: Is this done later on again, making this redundant? -- Show matching brackets
vim.o.syntax = 'on' -- Syntax highlighting (Enables e. g. whitespace marker coloring)
vim.api.nvim_set_hl(0, 'Whitespace', {ctermbg='NONE', ctermfg='DarkGray'}) -- Disable background highlighting of tabs and spaces
vim.o.swapfile = false -- Disable creation of swap files (See https://stackoverflow.com/questions/821902/disabling-swap-files-creation-in-vim)
vim.o.splitright = true -- Create split windows below and to the right instead of above and left
vim.o.splitbelow = true -- Create split windows below and to the right instead of above and left
vim.o.fillchars = 'vert: ' -- Replace tile border with space
vim.api.nvim_set_hl(0, 'VertSplit', {ctermbg='Gray', ctermfg='Gray'})--TODO: THIS DOES NOT WORK PROPERLY -- Recolor tile border frame colors with background and foreground colors of current theme, so as to make it invisible
-- :hi VertSplit ctermfg=bg ctermbg=bg guifg=bg guibg=bg " Recolor tile border frame colors with background and foreground colors of current theme, so as to make it invisible
vim.o.fillchars = 'eob:¬' -- Replace end of buffer tildes with not symbol, can be replaced with space to hide them
-- TODO: This does not seem to change anything, at least not when going into next line from a comment: vim.o.formatoptions = vim.o.formatoptions:gsub("[cro]", "")-- Prevent comments from wrapping, and being continued after pressing o on a comment line (String manipulation removes options "c", "r" and "o")
-- TODO: Is this neccessary, or does lua make it fast enough? -- Turn off specific things for latex files, else vim gets slow (Source: https://stackoverflow.com/questions/8300982/vim-running-slow-with-latex-files):
-- au BufNewFile,BufRead *.tex
-- \ set nocursorline nocursorcolumn |
-- \ let g:loaded_matchparen=1 |
-- Start file explorer, unless a file or session is specified, eg. vim -S session_file.vim.
if next(vim.fn.argv()) == nil then
vim.cmd.normal('Explore')
end
-- Mappings -----------------------------
-- Aliases for mappings, see Source (by default, they are all nonrecursive):
function map(mode, shortcut, command)
vim.api.nvim_set_keymap(mode, shortcut, command, { noremap = true, silent = true })
end
function nmap(shortcut, command)
map('n', shortcut, command)
end
function imap(shortcut, command)
map('i', shortcut, command)
end
function vmap(shortcut, command)
map('v', shortcut, command)
end
function tmap(shortcut, command)
map('t', shortcut, command)
end
function xmap(shortcut, command) -- This is very niche, it applies a mapping to visual mode only, while vmap applies to visual AND select mode, see :h mapmode-x or :h map-modes
map('x', shortcut, command)
end
function smap(shortcut, command) -- This is very niche, it applies a mapping to select mode only, while vmap applies to visual AND select mode, see :h mapmode-s or :h map-modes
map('s', shortcut, command)
end
function omap(shortcut, command) -- This is very niche, it applies a mapping to select mode only, while vmap applies to visual AND select mode, see :h mapmode-s or :h map-modes
map('o', shortcut, command)
end
-- -------------------------- gallium recommendations
-- In qute, "a" and "e" are not bound, making remapping to gallium very easy, since "h" just stays where it is (shifted one to right from querty). So I just have to remap the movements, and their capitals, and i for inserting into field to k (see below)
-- hjkl -> haei
-- aei -> ljk : a zu l weil leicht erreichbar um in insert mode zu wechseln, e zu j weil e nicht so oft gebraucht wird, und j ist schwer zu erreichen, i zu k weil i für insert mode und leicht zu erreichen, und auch in vim wichtig für diw zum Beispiel
-- -------------------------- older colemak settings
-- Get all the default mappings from view-source:https://vim.rtorr.com/
-- === H does not need to be remapped, because h is an graphite homerow
-- === J
nmap('a', 'j')
vmap('a', 'j')
nmap('ga', 'gj') -- move to next line
vmap('ga', 'gj') -- move to next line
nmap('A', 'J') -- join lines
vmap('A', 'J') -- join lines
imap('<C-a>', '<C-j>') -- add linebreak at current position
nmap('k', 'a') -- Append
vmap('k', 'a') -- Append
nmap('gk', 'ga') -- Display hexal/octal of number under cursor
vmap('gk', 'ga') -- Display hexal/octal of number under cursor
nmap('K', 'A') -- Append at end of line
vmap('K', 'A') -- Append at end of line
-- imap('<C-K>', '<C-A>') -- This is not needed because by default C-A does nothing in vim
-- === K
-- vim.keymap.del('v', 'i') -- Remove waiting for text object after pressing i in visual mode -- TODO somehow like this i should be able to eliminate lag
nmap('e', 'k')
vmap('e', 'k') -- TODO: This works, but in visual mode i first waits for another character because of text objects, e.g. "i(", this leads to delay and accidental overpresses of i. For possible solution see https://www.reddit.com/r/vim/comments/be2sik/remap_textobject_commands/
nmap('ge', 'gk') -- move to previous line
vmap('ge', 'gk') -- move to previous line
nmap('E', 'K') -- open manpage for word under cursor
nmap('j', 'e')
vmap('j', 'e')
nmap('gj', 'ge')
vmap('gj', 'ge')
nmap('J', 'E')
-- === L
nmap('i', 'l')
vmap('i', 'l')
nmap('I', 'L') -- move to bottom of screen
vmap('I', 'L') -- move to bottom of screen
-- omap('i', 'l') -- For text object to the right -- UNCOMMENTING THIS BREAKS THINGS LIKE delete in braces, i.e. di(
nmap('l', 'i')
nmap('L', 'I') -- insert at beginning of line
vmap('l', 'i')
vmap('L', 'I') -- insert at beginning of line
nmap('<C-L>', '<Tab>') -- ATTENTION: This cannot be mapped, mapping Ctrl+i will break the tab key, since Ctrl+i is its terminal composed character sequence. See https://unix.stackexchange.com/questions/563469/conflict-ctrl-i-with-tab-in-normal-mode and https://vi.stackexchange.com/questions/25473/tab-does-not-work-with-vim-for-me -- go to newer position in jumplist -- See nvim api documentation, it says: NOTE: In the GUI and in a terminal supporting tui-modifyOtherKeys or tui-csiu, CTRL-I can be mapped separately from <Tab>, on the condition that both keys are mapped, otherwise the mapping applies
-- TODO: Eventuell ib, iB und it in visual mode
-- Map default behavior of neio keys to hjkl, in the way that: l does now what n did, h does now what e did, j does now what i did, k does now what o did:
-- nmap('l', 'n')
-- vmap('l', 'n')
-- nmap('L', 'N') -- search in other direction
-- vmap('L', 'N') -- search in other direction
-- imap('<C-l>', '<C-n>') -- insert (auto-complete) next match before the cursor during insert mode
-- nmap('h', 'e')
-- vmap('h', 'e')
-- nmap('H', 'E') -- back word end
-- vmap('H', 'E') -- back word end
-- nmap('<C-h>', '<C-e>') -- move screen down one line without moving cursor
-- vmap('<C-h>', '<C-e>') -- move screen down one line without moving cursor
-- nmap('j', 'i')
-- nmap('J', 'I') -- insert at beginning of line
-- vmap('j', 'i')
-- vmap('J', 'I') -- insert at beginning of line
-- nmap('<C-j>', '<C-i>') -- ATTENTION: This cannot be mapped, mapping Ctrl+i will break the tab key, since Ctrl+i is its terminal composed character sequence. See https://unix.stackexchange.com/questions/563469/conflict-ctrl-i-with-tab-in-normal-mode and https://vi.stackexchange.com/questions/25473/tab-does-not-work-with-vim-for-me -- go to newer position in jumplist
-- -- TODO: Eventuell ib, iB und it in visual mode
--
-- nmap('k', 'o')
-- vmap('k', 'o') -- move to other end of marked area
-- vmap('K', 'O') -- move to other end of block
-- nmap('<C-k>', '<C-o>') -- go to older position in jumplist
-- omap('k', 'i') -- For text object to the bottom
-- --nmap('K', 'O') this has been done above with kk and KK
-- TODO: Add colemak mappings for CTRL-W + hjkl for changing split windows, and also rearranging them
-- imap('', '<Tab>')
-- --------------------------
vim.g.mapleader = ' ' -- Map leader key to space
vim.g.maplocalleader = ' ' -- Make local leader key same as leader key, required by vimtex, see https://stackoverflow.com/questions/26837425/vim-how-to-use-the-control-key-as-my-local-leader
-- TODO: The following lines are disabled because they do not make sense on colemak
-- imap('jk', '<Esc>') -- Map jk to escape from insert mode to prevent reaching for escape - If needing to type literal jk, one has to wait one to two seconds between presses, see https://vim.fandom.com/wiki/Avoid_the_escape_key
-- vmap('jk', '<Esc>') -- Map jk to escape from visual mode
-- Map putting in visual mode so that text stays selected, but not if using capital P . Capital P is normally used to put text in visual mode, and not yank it to register, because small p yanks overwritten text to register
vmap('p', 'Pgv')
vmap('P', 'P')
-- xmap('p', 'c<c-r><c-r>0<esc>') -- Fix visual paste repeat, see https://vi.stackexchange.com/questions/34850/is-it-possible-to-properly-repeat-a-visual-replacement and https://www.reddit.com/r/vim/comments/qhd2pv/is_it_possible_to_properly_repeat_a_visual/
-- Map to visually select last pasted text:
nmap('gp', "`[v`]")
nmap('S', ':%s//gI<Left><Left><Left>')-- TODO: Maybe polish this with below call command? -- Map capital S to replace all, I tag needed to make case sensitive after o.ignorecase or so has been set
-- :autocmd FileType tex iabbrev fr \frac{ENUMERATOR}{DENOMINATOR}<Esc>?ENUMERATOR<CR>dwh:call InsertInput("Enumerator")<CR><Esc>?DENOMINATOR<CR>dwh:call InsertInput("Denominator")<CR>a<right>
-- TODO: This is legacy, since neovim 0.11 the bindings [<Space and ]<Space do this
-- nmap('kk', 'o<Esc>') -- Insert empty line below current line
-- nmap('KK', 'O<Esc>') -- Insert empty line above current line
-- Delete without yanking
nmap('d', '"_d')
nmap('dd', '"_dd')
nmap('D', '"_D')
vmap('d', '"_d')
vmap('D', '"_D')
-- Change without yanking
nmap('c', '"_c')
nmap('cc', '"_cc')
nmap('C', '"_C')
vmap('c', '"_c')
vmap('C', '"_C')
-- Delete single characters without yanking
nmap('x', '"_x')
nmap('X', '"_X')
vmap('x', '"_x')
vmap('X', '"_X')
-- Substitute without yanking (Capital s is already mapped to replace all)
nmap('s', '"_s')
vmap('s', '"_s')
-- Delete with yanking = Cut (Is equivalent to default behaviour of d and D)
nmap('<Leader>d', 'd')
nmap('<Leader>dd', 'dd')
nmap('<Leader>D', 'D')
vmap('<Leader>d', 'd')
vmap('<Leader>D', 'D')
-- Change with yanking = Cut (Is equivalent to default behaviour of c and C)
nmap('<Leader>c', 'c')
nmap('<Leader>cc', 'cc')
nmap('<Leader>C', 'C')
vmap('<Leader>c', 'c')
vmap('<Leader>C', 'C')
-- Delete single characters with yanking = Cut (Is equivalent to default behaviour of x and X)
nmap('<Leader>x', 'x')
nmap('<Leader>X', 'X')
vmap('<Leader>x', 'x')
vmap('<Leader>X', 'X')
-- Substitute with yanking via leader (Is equivalent to default behaviour of s)
nmap('<Leader>s', 's')
vmap('<Leader>s', 's')
-- Make change whole line ("cc") start at first non-blank character, so that it does not delete the indentation:
nmap('cc', '_"_C') -- need "_ so yank also gets blackholed
-- TODO: Maybe make CC use the old behavior of cc, so that it deletes the indentation? But would mean delay when using single C
-- -- Map for easier window tile navigation
-- nmap('<C-e>', '<C-W><C-J>')
-- nmap('ႭჃႳ', '<C-W><C-K>') -- see alacritty config for the meaning of this symbol, mapped to ctrl+i binding, see https://medium.com/@jogarcia/alacritty-with-tmux-escape-sequence-for-custom-binding-47df5e401c51
-- nmap('<C-o>', '<C-W><C-L>')
-- nmap('<C-n>', '<C-W><C-H>')
-- nmap('<C-E>', '<C-W>J')
-- nmap('ႴႭჃႳ', '<C-W>K') -- see alacritty config for the meaning of this symbol, mapped to ctrl+i binding, see https://medium.com/@jogarcia/alacritty-with-tmux-escape-sequence-for-custom-binding-47df5e401c51
-- nmap('<C-O>', '<C-W>L')
-- nmap('<C-N>', '<C-W>H')
-- -- Map for easier window tile resizing
-- nmap('<C-Up>', '<C-W>+')
-- nmap('<C-Down>', '<C-W>-')
-- nmap('<C-Left>', '<C-W><')
-- nmap('<C-Right>', '<C-W>>')
-- nmap('<C-M>', '<C-W>=')
-- Map to switch jumping to marks accent aigu and apostrophe, because accent aigu does not work when dead keys are enabled, and jumping to line and column is more important:
nmap("'", '`')
nmap("`", "'")
-- Prevent cursor from shifting to left after enter-leaving the insert mode: -- TODO: Disabled experimentally
-- vim.g.CursorColumnI = 0 -- the cursor column position in INSERT
-- vim.cmd('autocmd InsertEnter * let CursorColumnI = col(".")')-- TODO: Do all these in proper lua
-- vim.cmd('autocmd CursorMovedI * let CursorColumnI = col(".")')
-- vim.cmd('autocmd InsertLeave * if col(".") != CursorColumnI | call cursor(0, col(".")+1) | endif')
-- Map File explorer to CTRL+n TODO: This all has been disabled in favor of yazi.nvim plugin
-- nmap('<C-p>', ':Explore! <CR>') -- CTRL+p seems to be the convention for doing this, see https://youtu.be/DIWr3FkQnnc?feature=shared&t=164
-- nmap('<Leader><C-p>', ':FuzzyOpen <CR>') -- Open fuzzy finder for file names
-- -- Set netrw settings:
-- vim.g.netrw_list_hide = '^\\./$,^\\.\\./$'
-- vim.g.netrw_hide = 1
-- -- vim.g.netrw_keepdir = 0 -- TODO: Does conflict with autochdir, see https://vi.stackexchange.com/questions/34557/how-can-i-use-let-gnetrw-keepdir-0-for-folders-and-autochdir-for-files -- keep working dir same as browsing dir while using netrw, see https://inlehmansterms.net/2014/09/04/sane-vim-working-directories/
-- -- Fix netrw bug where when jumping back to it via ctrl+o it will not actually open netrw, but a different buffer (either unnamed buffer or buffer from before), see https://www.reddit.com/r/neovim/comments/14mftou/jumplist_with_netrw/ and https://github.com/neovim/neovim/issues/24721
-- -- For this solution see https://www.reddit.com/r/neovim/comments/14mftou/jumplist_with_netrw/
-- vim.g.netrw_fastbrowse = 2
-- vim.g.netrw_keepj = ""
-- Command to open buffer explorer and promt for number of desired buffer (enter number and press enter) (source see https://vim.fandom.com/wiki/Easier_buffer_switching#Switching_by_number)
nmap('<Leader>b', ':buffers<CR>:buffer<Space>') -- To go to previous buffer use CTRL+6 or CTRL+^ by default
-- Command to correct last/next error to first recommended spelling:
nmap('<Leader>z', '[s1z=gi<ESC>l')
nmap('<Leader>Z', ']s1z=gi<ESC>l')
-- LSP mappings:
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) -- source https://blog.pabuisson.com/2022/08/neovim-modern-features-treesitter-and-lsp/
-- Default mappings provided since neovim 0.11 (see https://gpanders.com/blog/whats-new-in-neovim-0-11/):
-- vim.lsp.buf.rename() is mapped to grn
-- vim.lsp.buf.references() is mapped to grr
-- vim.lsp.buf.implementation() is mapped to gri
-- vim.lsp.buf.document_symbol() is mapped to gO
-- Map capital U to redo instead of ctrl+r and restore state of last changed line to leader+capital U instead of capital U:
nmap('U', '<C-R>')
nmap('<Leader>U', 'U')
-- -- Mapping to display register list and paste content of register that is selected by "number/letter" + "enter" at the bottom prompt in the split window that shows the registers: (Source: https://vi.stackexchange.com/questions/8467/how-can-i-easily-list-the-content-of-the-registers-before-pasting/8468#8468) -- TODO: This does not work yet
-- nmap('<Leader>p', ':reg <bar> exec \'normal! "\'.input(\'>\').\'p\'<CR>') -- TODO: This does not seem to work yet, numbered register contents are way too old
-- Mapping to put register with space in front, as often would be convenient:
nmap('<Leader>p', 'a <ESC>p')
-- Mapping to put the contents of the register without the leading newline: -- TODO
nmap('<Leader>P', 'pbJ')
-- Mapping to split line at cursor (inverse to capital J) (and also remove trailing space if there is one in the remaining upper line):
nmap('<Leader>A', 'i<CR><ESC>k:s/ $//<CR>$')
-- Mappings for terminal mode, see https://gist.github.com/mahemoff/8967b5de067cffc67cec174cb3a9f49d
nmap('<Leader>t', ':terminal<CR>')
-- Always enter insert mode when entering terminal buffer (important e. g. when returning to gitui window from edit):
vim.api.nvim_create_autocmd({ "TermOpen", "BufEnter" }, { -- Source see https://vi.stackexchange.com/questions/3670/how-to-enter-insert-mode-when-entering-neovim-terminal-pane/3765#3765
pattern = "term://*",
callback = function()
-- Set terminal-specific options
vim.opt_local.spell = false
vim.opt_local.number = false
vim.opt_local.relativenumber = false
-- Terminal mode escape key mapping
vim.api.nvim_buf_set_keymap(0, 't', '<Esc>', '<C-\\><C-N>:bd!<CR>', { noremap = true, silent = true })
-- Start terminal in insert mode
vim.cmd("startinsert")
end
})
-- Mapping to open float with list of lsp errors on current line:
nmap('go', ':lua vim.diagnostic.open_float()<CR>') -- the usual keymap is "gl", which becomes "go" in colemak
vim.diagnostic.config({
float = {
border = "rounded", -- Rounded borders for the floating window
source = "always", -- Show the source of the diagnostic
header = "", -- Remove the "Diagnostics" header
prefix = "‣ ", -- Add a bullet point before each message
}
})
-- vim.api.nvim_set_hl(0, "NormalFloat", {bg="#FFFFFF"}) -- TODO Bkg is not usual color
-- Mapping to force gf to go edit file even if it does not exist for use in note taking/wiki creation to quickly make new notes:
nmap('gf', ':e <cfile><CR>')
-- Function to browse forward links in note:
function notes_browse_links()
local bufname = vim.api.nvim_buf_get_name(0)
vim.cmd(':e Browse links')
vim.bo.buftype = 'nofile' -- So there wont be buffer change warning
vim.bo.bufhidden = 'hide' -- So it wont clog up the buffer list when switching through, cant be unloaded so Ctrl-i and ctrl-o work
local grephandle = io.popen( 'rg -os "\\./\\w*" ' .. bufname .. ' | sort -u' ) -- sort -u sorts alphabetically and removes duplicates
local grepoutput = grephandle:read("*a")
grephandle:close()
local grepoutputarray = {}
for line in grepoutput:gmatch '[^\n]+' do
table.insert(grepoutputarray, line:gsub('./',''))
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, grepoutputarray)
end
vim.keymap.set('n', '<Leader>nL', notes_browse_links, { noremap = true, silent = true })
-- Function to browse backward links in note:
function notes_browse_backlinks()
local filename = vim.fn.expand('%:t')
vim.cmd(':e Browse backlinks')
vim.bo.buftype = 'nofile' -- So there wont be buffer change warning
vim.bo.bufhidden = 'hide' -- So it wont clog up the buffer list when switching through, cant be unloaded so Ctrl-i and ctrl-o work
local grephandle = io.popen( 'rg -Fls "./' .. filename .. '" * | sort -u' )
local grepoutput = grephandle:read("*a")
grephandle:close()
local grepoutputarray = {}
for line in grepoutput:gmatch '[^\n]+' do
table.insert(grepoutputarray, line)
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, grepoutputarray)
end
vim.keymap.set('n', '<Leader>nB', notes_browse_backlinks, { noremap = true, silent = true })
-- Mapping leader + nl to browse forward links in note that are not explicitly links (with ./), but are just plain words:
function notes_browse_words()
local bufname = vim.api.nvim_buf_get_name(0)
vim.cmd(':e Browse words')
vim.bo.buftype = 'nofile' -- So there wont be buffer change warning
vim.bo.bufhidden = 'hide' -- So it wont clog up the buffer list when switching through, cant be unloaded so Ctrl-i and ctrl-o work
local rghandle = io.popen( 'rg -os "\\w*" ' .. bufname .. ' | sort -u' )
local rgoutput = rghandle:read("*a")
rghandle:close()
local lshandle = io.popen( 'ls -1' )
local lsoutput = lshandle:read("*a")
lshandle:close()
local outputarray = {}
-- Check that the word exists as file:
for rgline in rgoutput:gmatch '[^\n]+' do
for lsline, i in lsoutput:gmatch '[^\n]+' do -- use different regex here because gmatch does not work with caret characters when also taking index variable
if rgline == lsline then
table.insert(outputarray, rgline)
-- TODO: Remove first i lines from lsoutput, so that it is not searched through again, because of alphabetic ordering
break
end
end
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, outputarray)
end
vim.keymap.set('n', '<Leader>nl', notes_browse_words, { noremap = true, silent = true })
-- Function to browse backward words in note:
function notes_browse_backwords()
local filename = vim.fn.expand('%:t')
vim.cmd(':e Browse backwords')
vim.bo.buftype = 'nofile' -- So there wont be buffer change warning
vim.bo.bufhidden = 'hide' -- So it wont clog up the buffer list when switching through, cant be unloaded so Ctrl-i and ctrl-o work
local grephandle = io.popen( 'rg -Fls "' .. filename .. '" * | sort -u' )
local grepoutput = grephandle:read("*a")
grephandle:close()
local grepoutputarray = {}
for line in grepoutput:gmatch '[^\n]+' do
table.insert(grepoutputarray, line)
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, grepoutputarray)
end
vim.keymap.set('n', '<Leader>nb', notes_browse_backwords, { noremap = true, silent = true })
-- Mapping to prevent deselecting visually selected text when indenting or unindenting it:
vmap('<', '<gv')
vmap('>', '>gv')
-- Mapping to search for visually selected text:
vmap('//', 'y/\\V<C-R>=escape(@",\'/\\\')<CR><CR>') -- See https://vim.fandom.com/wiki/Search_for_visually_selected_text#Simple
-- Save and quit file from insert and normal mode:
imap('<C-s>', '<Esc>:lua if vim.bo.modified then vim.cmd("w") end if #vim.fn.getbufinfo({buflisted = 1}) > 1 then vim.cmd("bd") else vim.cmd("q") end<CR>')
nmap('<C-s>', ':lua if vim.bo.modified then vim.cmd("w") end if #vim.fn.getbufinfo({buflisted = 1}) > 1 then vim.cmd("bd") else vim.cmd("q") end<CR>')
-- TODO: The following has been replaced by m4xshen/autoclose.nvim, but has been reactivated, because autoclose seems to cause unnecessary lag
-- Brackets and Braces completion (Source: https://vim.fandom.com/wiki/Automatically_append_closing_characters) TODO: THIS CAUSES MUCH LAG WHEN TYPING IN FRONT OF CLOSING BRACKET or similar
-- Curly Brackets:
imap('{', '{}<Left>')
imap('{<CR>', '{<CR>}<Esc>O')
imap('{{', '{')
imap('{}', '{}')
imap('}', '<C-R>=strpart(getline(\'.\'), col(\'.\')-1, 1) == \"}\" ? \"\\<lt>Right>\" : \"}\"<CR>')-- Skip placement of closing brackets if already present, used backward compatible vim version which does not use <expr> matching but register instead, because otherwise it would not work with lua, see https://vim.fandom.com/wiki/Automatically_append_closing_characters#Backwards-compatible_closing_brace_skip
-- Parentheses:
imap('(', '()<Left>')
imap('(<CR>', '(<CR>)<Esc>O')
imap('((', '(')
imap('()', '()')
imap(')', '<C-R>=strpart(getline(\'.\'), col(\'.\')-1, 1) == \")\" ? \"\\<lt>Right>\" : \")\"<CR>')-- Skip placement of closing brackets if already present
-- Square Brackets:
imap('[', '[]<Left>')
imap('[<CR>', '[<CR>]<Esc>O')
imap('[[', '[')
imap('[]', '[]')
imap(']', '<C-R>=strpart(getline(\'.\'), col(\'.\')-1, 1) == \"]\" ? \"\\<lt>Right>\" : \"]\"<CR>')-- Skip placement of closing brackets if already present
-- Double Quotes (Same character comletions are a little different in the last line, see source):
imap('\"', '\"\"<Left>')
imap('\"<CR>', '\"<CR>\"<Esc>O')
imap('\"\"', '\"')
vim.api.nvim_set_keymap('i', '"', 'strpart(getline("."), col(".")-1, 1) == "\\"" ? "\\<Right>" : "\\"\\"\\<Left>"', { expr = true, noremap = true, silent = true }) -- Skip placement of closing quotes if already present (Notice the expr option in contrast to the default imap function)
-- Single Quotes:
imap("\'", "\'\'<Left>")
imap("\'<CR>", "\'<CR>\'<Esc>O")
imap("\'\'", "\'")
vim.api.nvim_set_keymap('i', "'", 'strpart(getline("."), col(".")-1, 1) == "\'" ? "\\<Right>" : "\'\'\\<Left>"', { expr = true, noremap = true, silent = true }) -- Skip placement of closing quotes if already present
-- Backtick:
imap("`", "``<Left>")
imap("`<CR>", "`<CR>`<Esc>O")
imap("``", "`")
vim.api.nvim_set_keymap('i', "`", 'strpart(getline("."), col(".")-1, 1) == "`" ? "\\<Right>" : "``\\<Left>"', { expr = true, noremap = true, silent = true }) -- Skip placement of closing quotes if already present
-- Dollar Sign (only in tex and bib files, annoying otherwise):
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'tex', 'bib' },
callback = function()
imap('$', '$$<Left>')
imap('$<CR>', '$<CR>$<Esc>O')
imap('$$', '$$')
vim.api.nvim_set_keymap('i', '$', 'strpart(getline("."), col(".")-1, 1) == "\\$" ? "\\<Right>" : "\\$\\$\\<Left>"', { expr = true, noremap = true, silent = true }) -- Skip placement of closing quotes if already present
-- Speed optimizations against laggy cursor in long documents (see and :h tex-slow)
vim.opt_local.cursorline = false -- Otherwise cursor can lag when moving/deleting characters
vim.opt_local.cursorcolumn = false -- Otherwise cursor can lag when moving/deleting characters
vim.opt_local.relativenumber = false -- Otherwise cursor can lag when moving/deleting characters
vim.cmd("NoMatchParen") -- Otherwise cursor can lag when moving/deleting characters
vim.cmd("syn sync maxlines=300")
end,
group = templates
})
-- Execute python code
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'python' },
callback = function()
imap('<F5>', '<ESC>:!python3 %<CR>') -- Execute python code
nmap('<F5>', ':!python3 %<CR>') -- Execute python code
-- vim.bo.tw = 79 -- Set textwidth to 79 for python, as recommended in PEP8 -- TODO: This has been turned off because it does not break strings intelligently, resulting in frustrating formatting
end,
group = compile_execute
})
-- Compile and execute Rust code with cargo run
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'rust' },
callback = function()
imap('<F5>', '<ESC>:!cargo run<CR>') -- Execute python code
nmap('<F5>', ':!cargo run<CR>') -- Execute python code
end,
group = compile_execute
})
-- Compile groff document
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'groff' },
callback = function()
imap('<F5>', '<Esc>:!ff %<CR>') -- Compile groff document
nmap('<F5>', ':!ff %<CR>') -- Compile groff document
end,
group = templates
})
-- Compile and execute Fortran code with gfortran:
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'fortran' },
callback = function()
imap('<F5>', '<ESC>:!gfortran % -o %:r && ./%:r<CR>') -- Compile and execute Fortran code
nmap('<F5>', ':!gfortran % -o %:r && ./%:r<CR>') -- Compile and execute Fortran code
end,
group = compile_execute
})
-- Compile and execute C code with gcc:
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'c' },
callback = function()
-- Development build (F5)
vim.keymap.set("n", "<F5>", function()
vim.bo.makeprg = "gcc -std=c99 -ggdb3 -O0 -fsanitize=address % -o %:r" -- ggdb3 is for additional debug info, O0 is for keeping all variables visible by doing no optimization (-Og would still do this, even if it says in the manual that it is better for debugging), -fsanitize=address is a tracer (?) against memory leaks
vim.cmd("make | if !empty(getqflist()) | copen | endif") -- make, and show output, then open quickfix list but if it is non-empty (I believe qfl gets populated by "make")
end, { buffer = true })
vim.keymap.set("i", "<F5>", function()
vim.cmd("stopinsert")
vim.cmd("write")
vim.bo.makeprg = "gcc -std=c99 -ggdb3 -O0 -fsanitize=address % -o %:r"
vim.cmd("make | if !empty(getqflist()) | copen | endif")
end, { buffer = true })
-- Production build (Shift-F5 => F17)
vim.keymap.set("n", "<F17>", function()
vim.bo.makeprg = "gcc -std=c99 -Wall -Werror -O3 -s -DNDEBUG % -o %:r" -- specify version, all warnings as errors, use level 3 optimization, strip debug symbols, turn off asserts
vim.cmd("make | if !empty(getqflist()) | copen | endif")
end, { buffer = true })
vim.keymap.set("i", "<F17>", function()
vim.cmd("stopinsert")
vim.cmd("write")
vim.bo.makeprg = "gcc -std=c99 -Wall -Werror -O3 -s -DNDEBUG % -o %:r" -- specify version, all warnings as errors, use level 3 optimization, strip debug symbols, turn off asserts
vim.cmd("make | if !empty(getqflist()) | copen | endif")
end, { buffer = true })
-- -- When building for release use "gcc -std=c99 -O3 -s -DNDEBUG", specify version, use level 3 optimization, strip debug symbols, turn off asserts
-- imap('<F5>', '<ESC>:!gcc -std=c99 -ggdb3 -Og -fsanitize=address -fdiagnostics-color=always % -o %:r && ./%:r<CR>') -- Compile and execute C code for debugging and development
-- nmap('<F5>', ':!gcc -std=c99 -ggdb3 -Og -fsanitize=address -fdiagnostics-color=always % -o %:r && ./%:r<CR>') -- Compile and execute C code for debugging and development
-- imap('<F17>', '<ESC>:!gcc -std=c99 -Wall -Werror -O3 -s -DNDEBUG -fsanitize=address -fdiagnostics-color=always % -o %:r && ./%:r<CR>') -- Compile and execute C code for production (Press Shift-F5 for this)
-- nmap('<F17>', ':!gcc -std=c99 -Wall -Werror -O3 -s -DNDEBUG -fsanitize=address -fdiagnostics-color=always % -o %:r && ./%:r<CR>') -- Compile and execute C code for production (Press Shift-F5 for this)
end,
group = compile_execute
})
-- compile and execute C++ code with g++:
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'cpp' },
callback = function()
imap('<F5>', '<ESC>:!g++ % -o %:r && ./%:r<CR>') -- Compile and execute C++ code
nmap('<F5>', ':!g++ % -o %:r && ./%:r<CR>') -- Compile and execute C++ code
end,
group = compile_execute
})
-- Make each character in list below work as text object for latex selection, see https://vi.stackexchange.com/questions/12146/refer-to-text-between-arbitrary-delimiting-characters
local chars = { '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%', '$', '=', "<Space>" }
for _, char in ipairs(chars) do
vim.api.nvim_set_keymap('x', 'i' .. char, ':<C-u>normal! T' .. char .. 'vt' .. char .. '<CR>', { silent = true })
vim.api.nvim_set_keymap('o', 'i' .. char, ':normal vi' .. char .. '<CR>', { silent = true })
vim.api.nvim_set_keymap('x', 'a' .. char, ':<C-u>normal! F' .. char .. 'vf' .. char .. '<CR>', { silent = true })
vim.api.nvim_set_keymap('o', 'a' .. char, ':normal va' .. char .. '<CR>', { silent = true })
end
-- Commands -----------------------------
vim.cmd('command! WipeRegs for i in range(34,122) | silent! call setreg(nr2char(i), []) | endfor | :wsh!')-- TODO: This must be done in proper lua, vim.cmd is a workaround -- Wipe all registers (Source: https://stackoverflow.com/questions/19430200/how-to-clear-vim-registers-effectively/41003241#41003241)
-- Plugins -----------------------------
-- Bootstrap lazy.nvim plugin manager:
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.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)
-- Set plugins and options for lazy.nvim:
local plugins = {
'lewis6991/gitsigns.nvim',
-- 'zbirenbaum/copilot.lua',
'jhawthorn/fzy',
'cloudhead/neovim-fuzzy',
-- 'tpope/vim-repeat',
'neovim/nvim-lspconfig', -- TODO: This is legacy since neovim 0.11+ does it natively, see https://gpanders.com/blog/whats-new-in-neovim-0-11/
'lervag/vimtex', -- TODO: On the vimtex github it says not to lazyload vimtex, as it will cause inverse search to fail
{ 'chomosuke/typst-preview.nvim',
ft = 'typst', -- or lazy = false
version = '1.*',
opts = {}, -- lazy.nvim will implicitly calls `setup {}`
},
{ 'iamcco/markdown-preview.nvim',
cmd = { 'MarkdownPreviewToggle', 'MarkdownPreview', 'MarkdownPreviewStop' },
ft = { 'markdown' },
build = function() vim.fn['mkdp#util#install']() end,
},
-- 'anott03/nvim-lspinstall', -- TODO: Can perhaps use anott03/nvim-lspinstall to automatically boostrap needed language servers
-- 'nvimdev/lspsaga.nvim', -- TODO: Is recommended on https://www.notonlycode.org/neovim-lua-config/ , but not clear what it does
{ 'L3MON4D3/LuaSnip', -- For inspiration and tips see https://cj.rs/blog/ultisnips-to-luasnip/ and https://evesdropper.dev/files/luasnip/ultisnips-to-luasnip/
dependencies = { "rafamadriz/friendly-snippets" }, -- For this to not slow down loading, require("luasnip.loaders.from_vscode").lazy_load() must be activated, see below. Do work, but are not good. See also plugin luasnip-latex-snippets.nvim
},
--'evesdropper/luasnip-latex-snippets.nvim', -- Do work, but are not good. See also plugin friendly-snippets.
'saadparwaiz1/cmp_luasnip',
'hrsh7th/nvim-cmp', -- Completions, but needs setup to work for each specific language
'hrsh7th/cmp-nvim-lsp', -- For this and the following three plugins see recommended config on https://github.com/hrsh7th/nvim-cmp -- TODO: This is legacy since neovim 0.11+ does it natively, see https://gpanders.com/blog/whats-new-in-neovim-0-11/
'nvim-treesitter/nvim-treesitter',
'nvim-treesitter/nvim-treesitter-textobjects',
'nvim-treesitter/nvim-treesitter-refactor', -- Use refactor functionality to implement jump to definition etc, see https://github.com/nvim-treesitter/nvim-treesitter-refactor?tab=readme-ov-file#navigation
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path',
'petertriho/cmp-git',
'nvim-lua/plenary.nvim', -- dependency of cmp-git
'hrsh7th/cmp-omni', -- For Latex support, see https://github.com/lervag/vimtex/issues/2215
'hrsh7th/cmp-cmdline',
-- { url = "https://git.sr.ht/~whynothugo/lsp_lines.nvim" }, -- For wrapping lsp inline diagnostics in new lines
-- 'vim-airline/vim-airline', -- Airline makes sshfs files lag to unusable degrees, see similar powerline issue: https://github.com/powerline/powerline/issues/549
-- 'vim-airline/vim-airline-themes',
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
-- { 'zbirenbaum/copilot-cmp', -- See https://www.reddit.com/r/neovim/comments/twe45i/copilotlua_copilotcmp_pure_lua_plugins_for_github/ , for this installation thing see https://github.com/zbirenbaum/copilot-cmp
-- config = function()
-- require('copilot_cmp').setup()
-- end },
{ "kylechui/nvim-surround",
version = "*", -- Use for stability; omit to use `main` branch for the latest features
event = "VeryLazy",
config = function()
require("nvim-surround").setup({
-- TO surround word with latex funcxtion use ysiwc and get prompted for command, to surround visual selection in environment press c , for the source see https://github.com/kylechui/nvim-surround/discussions/53#discussioncomment-3142459
surrounds = {
["c"] = {
add = function()
local cmd = require("nvim-surround.config").get_input "Command: "
return { { "\\" .. cmd .. "{" }, { "}" } }
end,
},
["e"] = {
add = function()
local env = require("nvim-surround.config").get_input "Environment: "
return { { "\\begin{" .. env .. "}", "" }, { "\\end{" .. env .. "}" } }
end,
},
},
})
end
},
-- "m4xshen/autoclose.nvim", -- This seems to cause to much lag, see manual implementation below
--"PhilGrunewald/vim-py-kid", -- TODO: This was an alternative to a jupyter notebook, but it does not work well
"gentoo/gentoo-syntax",
{
"mikavilpas/yazi.nvim",
event = "VeryLazy",
dependencies = {
-- check the installation instructions at
-- https://github.com/folke/snacks.nvim
"folke/snacks.nvim"
},
keys = {
-- 👇 in this section, choose your own keymappings!
{
"<leader><C-p>",
mode = { "n", "v" },
"<cmd>Yazi<cr>",
desc = "Open yazi at the current file",
},
{
-- Open in the current working directory
"<leader>cw",
"<cmd>Yazi cwd<cr>",
desc = "Open the file manager in nvim's working directory",
},
{
"<C-p>",
"<cmd>Yazi toggle<cr>",
desc = "Resume the last yazi session",
},
},
---@type YaziConfig | {}
opts = {
-- if you want to open yazi instead of netrw, see below for more info
open_for_directories = true,
keymaps = {
show_help = "<f1>",
},
},
-- 👇 if you use `open_for_directories=true`, this is recommended
init = function()
-- More details: https://github.com/mikavilpas/yazi.nvim/issues/802
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
end,
},
}
local opts = {}
require("lazy").setup({ plugins, opts }) -- Start lazy.nvim TODO: Lazyloading does need to be enabled explicitly, but some plugins may need to be excluded from lazyloading, see http://www.lazyvim.org/configuration/lazy.nvim and e.g. https://github.com/lervag/vimtex/issues/2996#issuecomment-2359489726
-- require('copilot').setup({
-- suggestion = { enabled = false },
-- panel = { enabled = false },
-- filetypes = {
-- yaml = true,
-- markdown = true,
-- help = true,
-- svn = true,
-- cvs = false,
-- gitcommit = true,
-- gitrebase = true,
-- ["."] = true,
-- },
-- })
require('gitsigns').setup({ -- Setup gitsigns plugin, see Source: https://github.com/lewis6991/gitsigns.nvim
preview_config = {
-- Options passed to nvim_open_win
border = 'rounded',
style = 'minimal',
relative = 'cursor',
row = 0,
col = 1
},
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local function map(mode, l, r, opts)
opts = opts or {}
opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts)
end
-- Navigate between hunks with ]c and [c
map('n', ']c', function()
if vim.wo.diff then return ']c' end
vim.schedule(function() gs.next_hunk() end)
return '<Ignore>'
end, {expr=true})
map('n', '[c', function()
if vim.wo.diff then return '[c' end
vim.schedule(function() gs.prev_hunk() end)
return '<Ignore>'
end, {expr=true})
-- Diffthis mapping:
map('n', '<leader>hs', gs.stage_hunk)
map('n', '<leader>hr', gs.reset_hunk)
map('v', '<leader>hs', function() gs.stage_hunk {vim.fn.line('.'), vim.fn.line('v')} end)
map('v', '<leader>hr', function() gs.reset_hunk {vim.fn.line('.'), vim.fn.line('v')} end)
map('n', '<leader>hu', gs.undo_stage_hunk)
map('n', '<leader>hp', gs.preview_hunk)
map('n', '<leader>hd', gs.diffthis) -- TODO: This throws errors
map('n', '<leader>hD', function() gs.diffthis('~') end)
map({'o', 'x'}, 'ah', ':<C-U>Gitsigns select_hunk<CR>') -- TODO: here default ih mapping might collide with colemak navkeys, using ah instead of ih, check if this works
end
})
-- require'lspconfig'.pyright.setup{} -- Setup pyright language server for python - IF THIS FAILS, MAY NEED TO RUN CONSOLE COMMAND 'npm i -g pyright', see Source: https://github.com/neovim/nvim-lspconfig -- TODO: In the github it says: "nvim-lspconfig does not set keybindings or enable completion by default" - so look at "suggested configuration" paragraph -- TODO: Install html, shell and latex language servers/lsps # TODO: Add suggested keybindings, especially for "jump to definition"
-- TODO: For basedpyright install see https://github.com/LazyVim/LazyVim/discussions/3350
require'lspconfig'.bashls.setup{} -- Setup bash language server, for config of filetypes see https://smarttech101.com/nvim-lsp-configure-language-servers-shortcuts-highlights/#configure_each_server_separately -- TODO: This is legacy since neovim 0.11+ does it natively, see https://gpanders.com/blog/whats-new-in-neovim-0-11/
require("luasnip.loaders.from_vscode").lazy_load() -- This is needed in case of luasnips, otherwise vim may load very slowly, see https://github.com/rafamadriz/friendly-snippets#with-lazynvim
--
-- Texlab latex language server setup: -- TODO: This is legacy since neovim 0.11+ does it natively, see https://gpanders.com/blog/whats-new-in-neovim-0-11/
require'lspconfig'.texlab.setup{
settings = {
texlab = {
build = {
-- useFileList = true, -- This enables bibliography and images in subfolders defined in seperate packages etc. to be recognized by texlab (https://github.com/latex-lsp/texlab/issues/1145), but may drastically reduce startup time if .fls file is too big (https://github.com/latex-lsp/texlab/pull/1160)
-- TODO the issue with the undefined references is fixed by adding \bibliography{name.bib}{} in the beginning of the document. This is what the personal custom package does in dobibliography, but
},
experimental = {
followPackageLinks = true, -- for including definitions from custom userdefined packages
mathEnvironments = {'align', 'equation', 'align*', 'equation*'}, -- Otherwise these environments are not recognized in part
enumEnvironments = {'enumerate', 'itemize'}, -- Otherwise these environments are not recognized in part
-- Recognize custom include figures for quick completion:
labelDefinitionCommands = {'incfig'},
labelDefinitionPrefixes = {{'incfig', 'fig:'}},
}
}
}
}
-- TODO: This is legacy since neovim 0.11+ does it natively, see https://gpanders.com/blog/whats-new-in-neovim-0-11/
require("lspconfig")["tinymist"].setup {
settings = {
formatterMode = "typstyle",
exportPdf = "onSave",
semanticTokens = "enable",
}
}
-- See e.g. https://github.com/latex-lsp/texlab/wiki/Configuration#texlabexperimentalfollowpackagelinks
--vim.g.texlab.experimental.followPackageLinks = true -- for including definitions from custom userdefined packages
--vim.g.texlab.experimental.mathEnvironments = {'align', 'equation', 'align*', 'equation*'} -- Otherwise these environments are not recognized in part
--vim.g.texlab.experimental.enumEnvironments = {'enumerate', 'itemize'} -- Otherwise these environments are not recognized in part
-- texlab.experimental.citationCommands = {}, -- Just for reference, there are also some more options like this one and the one above
-- Options for plugins
-- VIMTEX
vim.g.vimtex_view_method = 'zathura' -- Use zathura as pdf viewer
-- Exit vim if quickfix window is last window on screen (Source https://vim.fandom.com/wiki/Automatically_quit_Vim_if_quickfix_window_is_the_last):
vim.g.vimtex_complete_bib_simple = 1 -- Enable sorting after accuracy of match in citation completion, see https://github.com/lervag/vimtex/issues/1265#issuecomment-443894124
vim.g.vimtex_indent_lists = {} -- disable indentation of lists, and thus items in itemize, temporary fix to weird default vimtex indentation, see https://github.com/lervag/vimtex/issues/2599
vim.g.vimtex_quickfix_open_on_warning = 0
vim.g.vimtex_quickfix_ignore_filters = { 'Overfull', 'Underfull'}
--vim.g.vimtex_quickfix_autoclose_after_keystrokes = 1 -- TODO: This closes the quickfix window when work on the document is resumed, but close on successful compilation wound be preferable, possibly with binding <Leader>ll (compilation shortcut) to compilation+closing quickfix window, but that would also need to be done for saving
vim.api.nvim_create_autocmd("BufEnter", {
callback = function()
-- if the window is quickfix go on
if vim.bo.buftype == "quickfix" then
-- if this window is last on screen quit without warning
if vim.fn.winbufnr(2) == -1 then
vim.cmd('quit')
end
end
end,
group = grp
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "tex",
callback = function()
vim.keymap.set("i", "<CR>", function()
return vim.fn.getline("."):match("^%s*\\item%s+.+$")
and "<esc>o\\item <esc>a"
or "<CR>"
end, { expr = true, buffer = true })
-- -------- Provide functionality to only compile on save:
-- Initialize buffer local flag vimtex_compile to false:
vim.b.vimtex_compile = false
-- Overwrite leader+ll keymap to toggle vimtex_compile flag true and if true, compile document as single shot:
vim.keymap.set("n", "<Leader>ll", function()
vim.b.vimtex_compile = not vim.b.vimtex_compile
if vim.b.vimtex_compile then
vim.cmd("VimtexCompileSS")
end
end, { buffer = true })
-- Overwrite save command to do a single shot compile if vimtex_compile flag is true: -- TODO: instead use a autocmd BufWritePost for filetype tex, so that w does not get overwritten for e.g. bib files
function compile_tex_ifflag()
if vim.b.vimtex_compile then
vim.cmd("VimtexCompileSS") -- Trigger VimtexCompileSS for LaTeX compilation
end
end
vim.cmd([[
command! -buffer -nargs=* WRITECOMPILETEX write <args> | lua compile_tex_ifflag()
]])
-- Hack to circumvent lowercase user defined command prohibition, source:
vim.cmd([[
fun! SetupCommandAlias(from, to)
exec 'cnoreabbrev <expr> '.a:from
\ .' ((getcmdtype() is# ":" && getcmdline() is# "'.a:from.'")'
\ .'? ("'.a:to.'") : ("'.a:from.'"))'
endfun
call SetupCommandAlias("w","WRITECOMPILETEX")
]])
-- TODO: Maybe setup command alias in LUA instead, see https://stackoverflow.com/questions/3878692/how-to-create-an-alias-for-a-command-in-vim/69951171#69951171
end,
})
-- VIMTEX-LIVE-PREVIEW
-- vim.g.livepreview_previewer = 'zathura' -- Use zathura as pdf viewer-- TODO:It seems to work without this line, so maybe it is not needed, but this is a little suspicious, would only make sense if zathura is the only compatible reader it can find
-- TODO: Let Vimtex syntax highliting recognize some greek characters as normal characters, so that they can be used in math mode, with e. g. the following commands (Source: https://vi.stackexchange.com/questions/19040/add-keywords-to-a-highlight-group/19043#19043): " TODO Probably gets overwritten by vimtex plugin loading i somethign afterwards, even though earlier in the file
-- ":syntax keyword LatexGreekLetters α β γ δ ε ζ η θ ι κ λ μ ν ξ π ρ σ τ υ φ χ ψ ω Γ Δ Θ Λ Ν Ρ Σ Υ Φ Ψ Ω
-- ":highlight def link LatexGreekLetters Special
-- TODO: Adjust group in the line above, for groups see http://www.drchip.org/astronaut/vim/syntax/tex.vim.gz
-- MARKDOWN-PREVIEW
vim.g.mkdp_auto_close = 0 -- Do not close preview window when changing buffer (Will close anyway when closing vim)
vim.g.mkdp_auto_start = 1 -- Start preview automatically when opening markdown file
vim.g.mkdp_port = '3415' -- Set port for preview, otherwise it would be chosen at random, which could potentially conflict
vim.g.mkdp_theme = 'dark' -- Set dark theme for markdown preview
-- LUA-SNIPPETS, see https://www.reddit.com/r/neovim/comments/tbtiy9/choice_nodes_in_luasnip/
ls = require("luasnip")
-- see alacritty config for the meaning of the character, or https://stackoverflow.com/questions/3336188/gvim-tab-key-doesnt-work:
-- vim.keymap.set({"i"}, "", function() ls.expand() end, {silent = true}) -- TODO: This seems to be unused
-- vim.keymap.set({"i", "s"}, "<C-o>", function() ls.jump( 1) end, {silent = true}) -- TODO: This seems to be unused
-- vim.keymap.set({"i", "s"}, "<C-e>", function() ls.jump(-1) end, {silent = true}) -- TODO: This seems to be unused
-- vim.keymap.set({"i", "s"}, "<C-E>", function() -- TODO: This seems to be unused
-- if ls.choice_active() then
-- ls.change_choice(1)
-- end
-- end, {silent = true})
-- Set up nvim-cmp: -- Hint: To limit width of suggestion and documentation window see https://github.com/hrsh7th/nvim-cmp/discussions/609
local cmp = require'cmp'
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
-- The mapping of C-i cannot work with the special character bound to ctrl i in alacritty, since nvim-cmp only checks for specific alphanumeric characters and certain ascii ones, see https://github.com/hrsh7th/nvim-cmp/pull/2073 and for a solution see code below, and also https://github.com/hrsh7th/nvim-cmp/issues/1849
["<C-e>"] = cmp.mapping.select_prev_item(),
["<C-a>"] = cmp.mapping.select_next_item(),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-n>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = false }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items, and to true to have to scroll through list to select something.
['<C-o>'] = cmp.mapping.confirm({ select = false }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items, and to true to have to scroll through list to select something.
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' }, -- Primary source for coding completions
{ name = 'omni', group_index = 2 }, -- Use only if LSP lacks certain completions (e.g., VimTeX)
{ name = 'path', group_index = 2 }, -- Filepath completions
{ name = 'luasnip', group_index = 2 }, -- Useful if you use snippets
--{ name = 'copilot', group_index = 3 }, -- Lower priority, or trigger manually
}, {
{ name = 'buffer' },
})
})
-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {