-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.vim
570 lines (481 loc) · 17.5 KB
/
init.vim
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
" updated for my Arch machine
" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" Using a dark background within the editing area and syntax highlighting
set background=dark
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
if has("autocmd")
filetype plugin indent on
endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
" MY CUSTOM CONFIG
" ================
" everything shown will be utf8
set encoding=utf-8
" everything written will be utf8
set fileencoding=utf-8
" show line numbers
set number relativenumber
" set backup, undo, and swap files location
" set directory=$HOME/Desktop/programming/_misc/vim_swap_files//
" set backupdir=$HOME/Desktop/programming/_misc/vim_swap_files//
set undodir=$HOME/Desktop/programming/_misc/vim_swap_files//
" actually i hate swap files now, too much hassle
set noswapfile
" make splits default to below instead of above
set splitbelow
" opens terminal in curr window
" assumes splitbelow=true
" command Term normal :term<CR><C-W>k:q!<CR><C-W>j
" EDIT - nvm, i never use embedded temrinals or vim windows anymore
"
" command! Term normal :term<CR><C-W>k:q!<CR>
" make vertical window split behave like horiz
" assumes vsplit goes right
" EDIT - nvm, i never use vim windows anymore
"
" nnoremap <C-W>v <C-W>v<C-W>l:enew<CR>
"" nnoremap <C-W><C-V> <C-W>v<C-W>l:enew<CR>
" save on every Esc
" although this can cause buggy behavior: https://stackoverflow.com/q/11940801
" inoremap <Esc> <Esc>:w<CR>
" nnoremap <Esc> <Esc>:w<CR>
" nnoremap <Esc>[ <Esc>[
" autocmd TermResponse * nnoremap <Esc> <Esc>:w<CR>
autocmd! InsertLeave * silent! update
" keep selection after indents
vnoremap < <gv
vnoremap > >gv
" initiate a search without affecting cursor position
" 1. silently fill search register
" 2. execute an empty %s search bc it shows statusbar output
" (eg '3 results on 2 lines')
" 3. move cursor to begin the search
" " nnoremap / :let @/=''<Space><Bar><Space>%s///gn<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
" nnoremap / :let @/=''<Left>
" clear a search quickly to make highlights go away
"nnoremap ? :let @/=''<CR>
nnoremap <ESC> :let @/=''<CR>
" implied case sensitivity
set ignorecase
set smartcase
" auto make folds based on indentation
set foldmethod=indent
" but don't start files already folded
" set foldlevelstart=10
set nofoldenable
" and first folding shouldn't fold everything
set foldlevel=10
" set spacebar as special leader key
let mapleader = " "
" convenience for new empty buffer, like a scratchpad
nnoremap <leader>n :enew<CR>
" scroll with any vertical navigation
" set scrolloff=999
" tab thru buffers, personal fav buffer navigation method
nnoremap <Tab> :bnext<CR>
nnoremap <S-Tab> :bprev<CR>
" naive f(ind) next occurrence motion
" nnoremap f :let @/=''<Space><Bar><Space>n<Bar>:let@/=''<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
" i dont like highlighting matching parens
let loaded_matchparen = 1
" select-all shortcut
" nnoremap <C-A> ggvG
" shortcut for scrolling down/up
nnoremap <C-J> 9<C-E>
nnoremap <C-K> 9<C-Y>
" html filetype indentation
autocmd! FileType html setlocal shiftwidth=2 tabstop=2 expandtab
" convience to close html tags
autocmd! FileType html inoremap <buffer> </ </<C-X><C-O>
" i would for xml too, but it's not working...
" autocmd! FileType xml inoremap <buffer> </ </<C-X><C-O>
" javascript filetype indentation
autocmd! FileType javascript setlocal shiftwidth=2 tabstop=2 expandtab
" xml filetype idnentation, at least for adndroid development
autocmd! FileType xml setlocal shiftwidth=2 tabstop=2 expandtab
" elm filetype indentation, converting tabs to spaces
" TODO: try setting up formatprg for autoformatting
autocmd! FileType elm setlocal shiftwidth=2 tabstop=2 expandtab
" autocmd! BufWrite *.elm ElmFormat " loses cursor
command! ElmFormat normal mm:%!elm-format --stdin --yes<CR>`m
autocmd! FileType elm nnoremap <leader>w :ElmFormat<CR>
" ocaml autoformatting
autocmd! FileType ocaml setlocal shiftwidth=2 tabstop=2 expandtab
command! OcamlFormat normal :%!ocamlformat %<CR>
autocmd! FileType ocaml nnoremap <leader>w :OcamlFormat<CR>
" prettify html in current buffer with <leader>w in normal mode
" EDIT - nvm, tidy compains too much and prints errors and doesnt like meta
" seciton and blah blah blah
" and EDIT - npx prettier doesnt do what i want either - i should just write
" my own
" command! HTMLFormat normal mm:%!npx prettier --stdin-filepath samplenamejustforextension.html<CR>`m
" autocmd! FileType html nnoremap <leader>w :HTMLFormat<CR>
" rust autoformatting
command! RustFormat normal mm:%!rustfmt --edition 2021<CR>`m
autocmd! FileType rust nnoremap <leader>w :RustFormat<CR>
" html autoformatting
command! HtmlFormat normal mm:%!superhtml fmt --stdin<CR>`m
autocmd! FileType html nnoremap <leader>w :HtmlFormat<CR>
" make .svelte like .html
au BufRead,BufNewFile *.svelte setfiletype html
" make xml... xml
au BufRead,BufNewFile *.xml setfiletype xml
" .json tab spacing
autocmd FileType json setlocal shiftwidth=2 tabstop=2 expandtab
" to get visual block mode using wsl on windows terminal
nnoremap <C-Q> <C-V>
" open url in browser (assuming url was just yanked to default register)
" command! Web normal :!~/personal_scripts/browse.sh '<C-R>"'<CR>
" test with: https://wonger.dev/clark/readme#:~:text=keep%20everything%20in%20the%20command%2Dline
" EDIT - nvm, `gx` over links will open automatically with non-windows vim 7.4+,
" at least when the cursor is before the anchor # fragment
"
" command! Web normal :!cmd.exe /C min '<C-R>"'<CR>
" nnoremap <leader>u :Web<CR>
" vnoremap <leader>u y:Web<CR>
" highlight search matches
set hlsearch
" presentation helper, highlight cursor line with <S-H>
func! HLCurrLine()
let [_, lineno, _, _] = getpos(".")
" eg. '\%>0l\%<2l' matches first line
let pattern = '\%>' . (lineno-1) . 'l\%<' . (lineno+1) . 'l'
if exists("g:mirrored_cur_match_id")
silent! call matchdelete(g:mirrored_cur_match_id)
endif
if !exists("g:last_line") || (g:last_line != lineno)
silent! let g:mirrored_cur_match_id = matchadd('Search', pattern)
endif
let g:last_line = lineno
endfunc
nnoremap <S-H> :call HLCurrLine()<CR>
" turn off line numbers; useful for presenting
command! Nonum normal :set norelativenumber | set nonumber<CR>
" make default directional navigation work with line wraps
nnoremap k gk
nnoremap j gj
" HACKING TOGETHER A BOX DRAWING SHORTCUT
" draw a box at perimeter of visual block selection with <S-B>
func! DrawBoxAroundSelection()
" assumes that first line len is max line len
normal! gv"vy
let lines = split(getreg('v'), '\n')
let numlines = len(lines)
let numcols = strdisplaywidth(lines[0])
let line0 = '┌' . repeat('─', numcols-2) . '┐'
let linen = '└' . repeat('─', numcols-2) . '┘'
let updated_lines = [line0]
for line in lines[1:-2]
let boxed_line = '│' . strcharpart(line, 1, numcols-2) . '│'
call add(updated_lines, boxed_line)
endfor
call add(updated_lines, linen)
silent call setreg('v', updated_lines, "b" . numcols)
silent normal! gv"vP
endfunc
vnoremap <S-B> :<C-U>call DrawBoxAroundSelection()<CR>
" remove gray status bar that appeared in nvim
" also remove the quieter statusline called ruler as well
set laststatus=0
set noruler
" increment letters like numbers with <C-a>
set nrformats+=alpha
" fix syntax highlighting breaking
autocmd BufEnter * syntax sync fromstart
nnoremap <leader>l <Esc>:syntax sync fromstart<CR>
" copy selection to windows clipboard (not utf-8 tho) (works in wsl too)
" EDIT - not on windows now
"
" vnoremap <S-C> :w !cmd.exe /C clip.exe<CR><CR>
" connect nvim to xclip keyboard
" :h clipboard
" it works differently (better) than normal vim
set clipboard+=unnamedplus
let g:clipboard = {
\ 'name': 'xclip-clipboard-everywhere',
\ 'copy': {
\ '+': 'xclip -i -selection clipboard',
\ '*': 'xclip -i -selection clipboard',
\ },
\ 'paste': {
\ '+': 'xclip -o -selection clipboard',
\ '*': 'xclip -o -selection clipboard',
\ },
\ 'cache_enabled': 0,
\ }
" or connect nvim to windows system clipboard command
" let g:clipboard = {
" \ 'name': 'win32yank-wsl',
" \ 'copy': {
" \ '+': 'win32yank.exe -i --crlf',
" \ '*': 'win32yank.exe -i --crlf',
" \ },
" \ 'paste': {
" \ '+': 'win32yank.exe -o --lf',
" \ '*': 'win32yank.exe -o --lf',
" \ },
" \ 'cache_enabled': 0,
" \ }
" convenience for default registers == system clipboard
" vnoremap <leader>y "+y
" nnoremap <leader>y "+y
"
" vnoremap <leader>p "+p
" nnoremap <leader>P "+P
" nnoremap <leader>p "+p
" alternative window controls since i've already bound ctrl-w for terminal/tmux
func! MoveToWindowBelow()
let prev_window_id = winnr()
wincmd j
let curr_window_id = winnr()
if (curr_window_id == prev_window_id)
" wincmd j had no effect, so bubble up to tmux
silent !tmux select-pane -D
" get rid of annoying vim confirmation message
normal <CR>
endif
endfunc
func! MoveToWindowAbove()
let prev_window_id = winnr()
wincmd k
let curr_window_id = winnr()
if (curr_window_id == prev_window_id)
" wincmd j had no effect, so bubble up to tmux
silent !tmux select-pane -U
" get rid of annoying vim confirmation message
normal <CR>
endif
endfunc
" nnoremap ê :call MoveToWindowBelow()<CR>
nnoremap <leader>j :call MoveToWindowBelow()<CR>
nnoremap <leader>k :call MoveToWindowAbove()<CR>
" nnoremap <leader>j :winc j<CR>
" nnoremap <leader>k :winc k<CR>
" use terminal color palette
" apaprently using 16 terminal colors is already the default??
" and apparently i shouldnt have to set t_Co if my terminal is
" correctly configured
" set t_Co=16
"
" EDIT - apparently truecolor is enabled by defeault, probs because ghostty
" sets $COLORTERM=truecolor
"
" to disable truecolor, and conseuqently enable default terminal theme colors,
" set notermguicolors
" PLUGINS
" =======
"
" call plug#begin('~/.vim/plugged')
"
" Plug 'andys8/vim-elm-syntax'
"
" call plug#end()
" HACKING TOGETHER AN ANSI ART EDITOR
" ===================================
"
" func! MyHighlights()
" highlight YellowBlock ctermfg=9 ctermbg=9
" endfunc
"
" func! AnsiLength(str)
" " get length of string after stripping ansi escape sequences.
" " this length is equivalent to desired col of rendered output,
" " ie 'actual column'.
" " note: maybe a regex would be faster than a system call
" " note: ansifilter output length seems to include the escape sequence as a character, instead of desired grouping with the following character
" silent return strlen( system('ansifilter', a:str) )
" endfunc
"
" " note: for performance, consider preprocessing by making a csv of
" " source columns that correspond to each output column.
" " would have to update it every time line(s) in source buffer is changed
" func! CalcRenderedAnsiLocation()
" " return lineno and col of rendered ansi art buffer,
" " given cursor position in source buffer
" " eg:
" "
" " 'abc[1;2;3;mdef[4;5;6;m[7;8;9;mghi'
" " ^
" " 'abcdefghi'
" " ^
" "
" " source col 28 -> rendered col 7
" " 13 -> 4
" " 4 -> 4
" " 1 -> 1
"
" let [_, lineno, col, _] = getpos(".")
" let source_line = getbufline(bufnr('%'), lineno)[0]
"
" " En = next escape char position/col in source line
" " Ep = prev ''
" " note: does not account for other escape codes, eg \e \033 \x1b
" " consider converting escape codes in advance
" let En_col = stridx(source_line, '', col)
" let En_col = (En_col == -1 ? strlen(source_line) : En_col)
" let Ep_col = max([ strridx(source_line[0:col], '') , 0])
"
" " position/col where esc seq is located in rendered output
" let En_rend_col = AnsiLength(source_line[0:En_col])
" let Ep_rend_col = AnsiLength(source_line[0:Ep_col])
"
" " determining actual rendered column by comparing two methods
" let rend_col_far = En_rend_col - (En_col - col)
" let rend_col_near = Ep_rend_col
" let rend_col = max([rend_col_far, rend_col_near]) - 1
" return [lineno, rend_col]
" endfunc
"
" " highlight position in current window
" func! HighlightLocation(linenum, colnum)
" " eg. '\%>0l\%<2l' matches first line
" let pattern_linematch = '\%>' . (a:linenum-1) . 'l\%<' . (a:linenum+1) . 'l'
" " eg. '\%5c.' matches char at 5th col
" let pattern_colmatch = '\%' . a:colnum . 'c.'
" let pattern = pattern_linematch . pattern_colmatch
"
" " remove prev highlight and set current highlight
" if exists("g:mirrored_cur_match_id")
" call matchdelete(g:mirrored_cur_match_id)
" endif
" let g:mirrored_cur_match_id = matchadd('YellowBlock', pattern)
" endfunc
"
" " calculate desired position
" " highlight it in terminal window
" " assumes only/last window is terminal output render (and curr window is source file being edited) (ie winnr('$') == 2)
" func! HighlightCorrespondingAnsi()
" let [linenum, dest_col] = CalcRenderedAnsiLocation()
" let term_win_id = winnr('$')
" 2 windo call HighlightLocation(linenum, dest_col)
" silent execute 'wincmd w'
" endfunc
"
" " let g:cursor_highlight_on = 0
" " func! ToggleHighlight()
" " if g:cursor_highlight_on
" " matchadd('YellowBlock', pattern)
" " endfunc
"
" " Convenience controls for navigating between block chars
" " even better goal: nav to next character that's not in an escape sequence
" func! RemapCardinals()
" 2 windo set nohlsearch
" let @/ = '[▄|▀|█]'
" " nnoremap <C-J> jn
" " nnoremap <C-K> kN
" " nnoremap <C-H> N
" " nnoremap <C-L> n
" endfunc
"
" " for livereload: refreshing terminal buffer
" " tells other window (the one with term buffer) to load rendered ansi
" func! ReloadAnsi()
" let filename = expand('%')
" exec '2 windo term ++curwin cat ' . filename
" endfunc
"
" " usage: :AnsiCursor<CR> when viewing a source file with ansi escapes
" " shows a corresponding rendered display in split window
" " missing:
" " - livereload on change
" " consider :term ++curwin cat ... or :term ++hidden to not open a new window
" "
" " tips from developing:
" " - clearmatches() to get rid of orphaned match highlights
" " - use :2 windo ... to do commands on seemingly untouchable term window
" augroup CursorMirroring
" autocmd! CursorMoved *.ansi if winnr('$') == 2 | call HighlightCorrespondingAnsi()
" autocmd! InsertLeave *.ansi call ReloadAnsi()
" " if buftype=terminal set nonumber norelativenumber
" command! AnsiCursor normal :term cat %<CR>:call MyHighlights()<CR>:unlet! g:mirrored_cur_match_id<CR>:2 windo set nonumber norelativenumber<CR>
" " :call RemapCardinals()<CR>
" augroup END
"
" =========
" HACKING TOGETHER A DIRECTORY NAVIGATOR
" command! Cd normal y$:%!l -1 <C-R>"<CR>ggO..<CR>:cd <C-R>"<CR>
" HACKING A SIMPLENOTE UI WITH QUICKFIX LIST
"
" call setqflist([], ' ', {
" \ 'lines' : getqflist(),
" \ 'efm' : '%f',
" \ 'quickfixtextfunc': 'QfOldFiles',
" \ 'title': 'Search results: ' . getqflist({'title': 1}).title
" })
"
" call setqflist([], ' ', {'items': getqflist(), 'quickfixtextfunc': 'QfCustomGrepResult'}) | copen | <C-W> <S-K>
"
" func QfCustomGrepResult(info)
" " print name and grep match text, omitting default line and col num
" let items = getqflist({'id' : a:info.id, 'items' : 1}).items
" let l = []
" let seen = []
" for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
" let item = items[idx]
" let fname = bufname(item.bufnr)
" " if index(l:seen, l:fname) >= 0
" " continue
" " else
" call add(seen, fname)
" " consider adding spaces to make the result appear on
" " second line
" let displayed = printf('%s | %s', fname, item.text)
" call add(l, displayed)
" " endif
" endfor
" return l
" endfunc
"
" autocmd QuickFixCmdPost * call setqflist([], ' ', {'items': getqflist(), 'quickfixtextfunc': 'QfCustomGrepResult'}) | copen | wincmd K
"
" nnoremap <expr> j &buftype ==# 'quickfix' ? ':cnext<CR><C-W>k' : 'j'
" nnoremap <expr> k &buftype ==# 'quickfix' ? ':cprev<CR><C-W>k' : 'k'
" MESSY CROSS-SESSION REGISTER SYNCING
" ---
" " alt-hjkl make windows terminal switch panes
" nnoremap ì :!wt -w 0 mf right<CR><CR>
" nnoremap è :set lz<CR>:wv <Bar> call UpdateRegisters()<CR>:!wt -w 0 mf left<CR><CR>:set nolz<CR><CR>
" nnoremap ë :!wt -w 0 mf up<CR><CR>
" nnoremap ê :wv <Bar> call UpdateRegisters()<CR>:!wt -w 0 mf down<CR><CR>
"
" " flag for each session to opt in to synced registers
" " let syncedSessions = "VIM"
" let @r="VIM,VIM1,VIM2"
"
" " update viminfo with this session's registers,
" " and tell other sessions to load that newly updated viminfo
" " nnoremap í :wv<CR>:!vim --server-name VIM --remote-send "<lt>Esc>:rv<lt>CR>"<CR><CR>
"
" function UpdateRegisters()
" let servers = split(@r, ',')
" " let servers = split(g:syncedSessions, ',')
" " let servers = split(system('vim --serverlist'), '\n')
" for s in servers
" let a = system('vim --servername ' . s . ' --remote-send "<Esc>:rv<CR>"')
" endfor
" endfunction
" used in ubuntu's default config/dotfiles forced upon me from wsl
" source $VIMRUNTIME/defaults.vim
" resolve utf-8 / cursor issue (??) with WSL
" https://superuser.com/a/553610
set t_RV=
" set ambw=double
" https://superuser.com/a/1525060
set t_u7=