-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
executable file
·768 lines (631 loc) · 22.5 KB
/
.vimrc
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
" vi: fdm=marker
" General settings {{{1
set showmode
set hidden
set nocompatible
set ruler
set autoindent
set backspace=indent,eol,start
set smartindent
set expandtab
set tabstop=2
set softtabstop=2
set shiftwidth=2
set incsearch
set hlsearch
set background=dark
set nomore
set wildmode=longest:list
set mouse=a
set number
set title
set titlestring=%f%m
set ignorecase
set smartcase
set infercase
set tabpagemax=50
set updatetime=100
set showcmd
set virtualedit=block
set shortmess-=S
syntax enable
colorscheme desert
highlight Search ctermbg=Yellow
filetype plugin on
" Colors for vimdiff {{{1
highlight DiffAdd ctermfg=Green ctermbg=NONE
highlight DiffChange ctermfg=None ctermbg=Black
highlight DiffDelete ctermfg=Red ctermbg=NONE
highlight DiffText ctermfg=Yellow ctermbg=Black
" Cursor line {{{1
set cursorline
set cursorlineopt=both
highlight CursorLine ctermbg=237 cterm=none
highlight CursorLineNr ctermbg=237 cterm=bold ctermfg=yellow
" Status line {{{1
set laststatus=2
autocmd VimEnter * highlight User1 ctermfg=15 ctermbg=242
autocmd VimEnter * highlight User2 ctermfg=0 ctermbg=248
autocmd VimEnter * highlight User3 ctermfg=11 ctermbg=242
autocmd VimEnter * highlight User4 ctermfg=9 ctermbg=242
set statusline=%2*\ %n " Buffer number
set statusline+=\ %1*\ %t " File name
set statusline+=\ %M " Modified flag
set statusline+=%R " Read only flag
set statusline+=%H " Help buffer flag
set statusline+=%W " Preview window flag
set statusline+=\ %3*%{NearestMethodOrFunction()}
set statusline+=%= " Separation between left and right alignment
set statusline+=\ %F " Full path of file in the buffer
set statusline+=\ %4*\ %y " File type
set statusline+=\ %1*\ %5l: " Row
set statusline+=%-4c " Column
set statusline+=%2*%4P " Percentage
set statusline+=\
autocmd BufWinEnter,BufReadPost,BufWritePost quickfix setlocal statusline=%2*\ %Y\ %1*%{exists('w:quickfix_title')?\ '\ '.w:quickfix_title\ :\ ''}%=\%4*%q\ %1*%(%5l/%-4L%)\ %2*%4P\
" Follow the leader {{{1
let mapleader = " "
" Swap files and backups {{{1
set directory:~/.vim/swapfiles//
set backupdir:~/.vim/backup//
" White space rendering {{{1
set listchars=tab:▸\ ,trail:·,nbsp:_
"}}}
" PLUGINS
" Bootstrap plugins {{{1
execute pathogen#infect()
" Force snippet version 1 {{{1
let g:snipMate = { 'snippet_version' : 1 }
" Syntastic {{{1
let g:syntastic_javascript_checkers = ["eslint"]
let g:syntastic_javascript_eslint_exec = "eslint_d"
let g:syntastic_javascript_eslint_args = ['--fix']
let g:syntastic_javascriptreact_checkers = ["javascript/eslint"]
let g:syntastic_javascriptreact_eslint_exec = "eslint_d"
let g:syntastic_javascriptreact_eslint_args = ["--fix"]
let g:syntastic_cpp_compiler_options = " -std=c++11 -stdlib=libc++ -Wall"
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_stl_format = " ⛔️ :%F (%t)"
autocmd VimEnter *.js autocmd BufWritePost *.js checktime
autocmd CursorHold *.js checktime
autocmd VimEnter *.jsx autocmd BufWritePost *.jsx checktime
autocmd CursorHold *.jsx checktime
autocmd BufWritePre *.js call execute('LspCodeActionSync source.fixAll.ts')
autocmd BufWritePre *.ts call execute('LspCodeActionSync source.fixAll.ts')
nnoremap <silent> <Leader>f :checktime<CR>
set autoread
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
let syntastic_mode_map = { 'passive_filetypes': ['html'] }
nnoremap <silent> <Leader>e :SyntasticCheck<CR> :Errors<CR> :lopen<CR> :let w:quickfix_title = "Syntastic check"<CR> :lfirst<CR>
" Jump to next/previous - or first - error {{{1
nmap [e :call Lnext()<CR>
nmap ]e :call Lprev()<CR>
function Lnext()
try
lnext
catch
lfirst
endtry
endfunction
function Lprev()
try
lprevious
catch
lfirst
endtry
endfunction
" Comments {{{1
autocmd FileType javascript setlocal commentstring=//\ %s
autocmd FileType javascriptreact setlocal commentstring={/*\ %s\ */}
autocmd FileType conf setlocal commentstring=#\ %s
autocmd FileType gitconfig setlocal commentstring=#\ %s
autocmd FileType sh setlocal commentstring=#\ %s
autocmd FileType vim setlocal commentstring=\"\ %s
autocmd FileType php setlocal commentstring=#\ %s
autocmd FileType yaml setlocal commentstring=#\ %s
autocmd FileType jinja setlocal commentstring={#\ %s\ #}
autocmd FileType scss setlocal commentstring=//\ %s
" FZF {{{1
set runtimepath+=/opt/homebrew/opt/fzf
let $FZF_DEFAULT_COMMAND='fd --hidden --follow --exclude .git; echo .env'
let $FZF_DEFAULT_OPTS='--bind ctrl-a:select-all,ctrl-d:deselect-all'
nnoremap <silent> <Leader>t :Files!<CR>
nnoremap <silent> <Leader>. :Files! %:h<CR>
nnoremap <silent> <Leader>b :Buffers!<CR>
nnoremap <silent> <Leader>g :GFiles!?<CR>
nnoremap <silent> <Leader>h :HistoryFiles!<CR>
nnoremap <silent> <Leader><Leader> :b#<CR>
imap <c-x><c-l> <plug>(fzf-complete-line)
command! -bang -nargs=? -complete=dir Files
\ call fzf#vim#files(<q-args>, fzf#vim#with_preview({'options': '--cycle'}, 'right:50%', '?'), <bang>0)
command! -bang -nargs=? -complete=dir Buffers
\ call fzf#vim#buffers(<q-args>, fzf#vim#with_preview({'options': '--cycle'}, 'right:50%', '?'), <bang>0)
command! -bang -nargs=* HistoryFiles
\ call fzf#vim#history(fzf#vim#with_preview({'options': '--cycle'}, 'right:50%', '?'), <bang>0)
command! -bang -nargs=* Ag
\ call fzf#vim#ag(<q-args>, fzf#vim#with_preview({'options': '--delimiter : --nth 1 --tac --cycle'}, 'right:50%', '?'), <bang>1)
command! -bang -nargs=* Agi
\ call fzf#vim#ag(<q-args>, fzf#vim#with_preview({'options': '--delimiter : --nth 4 --tac --cycle'}, 'right:50%', '?'), <bang>1)
" LSP {{{1
nnoremap <silent> K :LspHover<CR>
autocmd FileType vim nnoremap <silent> <buffer> K :help <CR>
nnoremap <silent> gd :LspDefinition<CR>
nnoremap <silent> gD :LspPeekDefinition<CR>
nnoremap <silent> <Leader>R :LspRename<CR>
nnoremap <silent> <Leader>E :LspDocumentDiagnostics<CR> :lopen<CR> :let w:quickfix_title = "LSP Diagnostics"<CR> :lfirst<CR>
nnoremap <silent> <Leader>W :LspHover<CR>
nnoremap <silent> <leader>A :LspCodeAction<CR>
xnoremap <silent> <leader>A :LspCodeAction<CR>
" Settings: https://github.com/prabirshrestha/vim-lsp/blob/master/doc/vim-lsp.txt
let g:lsp_document_code_action_signs_enabled = 0
let g:lsp_diagnostics_float_cursor = 1
let g:lsp_diagnostics_float_delay = 150
let g:lsp_diagnostics_float_insert_mode_enabled = 0
let g:lsp_diagnostics_enabled = 1
let g:lsp_diagnostics_highlights_delay = 0
highlight lspReference ctermbg=242 ctermfg=yellow
let g:lsp_diagnostics_signs_delay = 0
let g:lsp_hover_conceal = 1
let g:markdown_syntax_conceal = 1
let g:markdown_fenced_languages = ["javascript"]
let g:lsp_diagnostics_virtual_text_enabled = 0
set conceallevel=2
let g:lsp_settings = {
\ "typescript-language-server": {
\ "workspace_config": {
\ "diagnostics": {
\ "ignoredCodes": [80001, 7016]
\ }
\ }
\ }
\ }
" Logging
let g:lsp_log_verbose = 1
let g:lsp_log_file = expand('~/vim-lsp.log')
" vim-markdown {{{1
let g:vim_markdown_folding_disabled = 1
" markdown-preview {{{1
nnoremap <silent> <Leader>mp :MarkdownPreview<CR>
" Completion: Cycling {{{1
inoremap <expr> <C-j> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <C-k> pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <CR> pumvisible() ? asyncomplete#close_popup() : "\<CR>"
" Completion: force refresh {{{1
imap <C-Space> <Plug>(asyncomplete_force_refresh)
" vimspector {{{1
nmap <Leader>dd <Plug>VimspectorLaunch
nmap <Leader>dc <Plug>VimspectorContinue
nmap <Leader>dl <Plug>VimspectorStepInto
nmap <Leader>dj <Plug>VimspectorStepOver
nmap <Leader>dk <Plug>VimspectorStepOut
nmap <Leader>dr <Plug>VimspectorRestart
nnoremap <silent> <Leader>de :call vimspector#Reset()<CR>
nmap <Leader>db <Plug>VimspectorToggleBreakpoint :echo "Toggle breakpoint"<CR>
nmap <Leader>db <Plug>VimspectorToggleBreakpoint :echo "Toggle breakpoint"<CR>
nmap <Leader>dq <Plug>VimspectorToggleConditionalBreakpoint<CR>
nnoremap <silent> <Leader>dB :call vimspector#ListBreakpoints()<CR>
nnoremap <silent> <Leader>dC :call vimspector#ClearBreakpoints()<CR>
nmap <silent> <Leader>di <Plug>VimspectorBalloonEval
source ~/git/dotfiles/vimspector-bindings.vim
let g:vimspector_sign_priority = {
\ 'vimspectorBP': 13,
\ 'vimspectorBPCond': 12,
\ 'vimspectorBPLog': 12,
\ 'vimspectorBPDisabled': 11,
\ 'vimspectorPC': 999,
\ }
" git-gutter {{{1
highlight! link SignColumn LineNr
highlight GitGutterAdd guifg=#00ff00 ctermfg=2
highlight GitGutterChange guifg=#ffff00 ctermfg=3
highlight GitGutterDelete guifg=#ff0000 ctermfg=1
nnoremap <silent> <Leader>gq :GitGutterQuickFix<CR>:copen<CR>
let g:gitgutter_sign_allow_clobber = 0
let g:gitgutter_sign_priority = 9
" Terraform {{{1
let g:terraform_fmt_on_save = 1
" Ranger {{{1
let g:ranger_command_override = 'ranger --cmd "set show_hidden=true"'
let g:ranger_map_keys = 0 " Do not map default keys
let g:ranger_replace_netrw = 1 " Use Ranger when opening directories
command! -bang Bclose :bd " Required to use as netrw replacement
nnoremap <silent> <Leader>r :Ranger<CR>
" Vista – outline {{{1
autocmd VimEnter * call vista#RunForNearestMethodOrFunction()
autocmd FileType vista,vista_kind nnoremap <buffer> <silent> / :<c-u>call vista#finder#fzf#Run()<CR>
let g:vista#renderer#enable_icon = 1
let g:vista_keep_fzf_colors = 1
nnoremap <silent> <Leader>/ :call vista#finder#fzf#Run()<CR>
nnoremap <silent> <Leader>o :Vista!!<CR>
function! NearestMethodOrFunction() abort
let name = get(b:, "vista_nearest_method_or_function", "")
if name != ""
return " ƒ " .. name
endif
return ""
endfunction
" Quick-Scope {{{1
let g:qs_highlight_on_keys = ['f', 'F', 't', 'T']
" GitHub Copilot {{{1
imap <silent><script><expr> <S-Tab> copilot#Accept()
imap <C-L> <Plug>(copilot-accept-word)
let g:copilot_no_tab_map = v:true
let g:copilot_node_command = "~/.nvm/versions/node/v20.17.0/bin/node"
let g:copilot_workspace_folders = ["~/git/"]
vnoremap <silent><S-Tab> :Copilot panel<CR>
"}}}
" MISCELLANEOUS
" Git jump {{{1
command! -bar -nargs=+ -complete=custom,Complete_git_jump GitJump cexpr system("git jump --stdout " . expand(<q-args>)) | :copen | :let w:quickfix_title = "[git jump " .. expand(<q-args>) .. "]"
abbreviate gj GitJump
function Complete_git_jump(ArgLead, CmdLine, CursorPos)
let words = split(a:CmdLine, " ")
if len(words) == 2 && len(a:ArgLead) > 0
return system("compgen -W 'diff merge grep ws' -- " .. a:ArgLead)
endif
return ""
endfunction
" Previous jump list entry {{{1
nnoremap <silent> <S-Tab> <C-o>
" Folding {{{1
set foldcolumn=3
let foldinglevelstart=99
set fillchars+=fold:\ ,foldsep:│,foldclose:>,foldopen:v
set foldtext=Folding()
function Folding()
let line = getline(v:foldstart)
if line =~# "\\v\\s(Scenario|describe|context|it|Given|When|Then|And|But)[.(]"
let line = substitute(line, "([\"'`]", ": ", "")
let line = substitute(line, "[\"'`],.*", "", "")
return line
endif
let line = substitute(line, "{$", "{…}", "")
let line = substitute(line, "[$", "[…]", "")
let line = substitute(line, "{[{]{[0-9]$", "", "") " Weird hack because three { folds here, otherwise…
return line
endfunction
nnoremap <silent> <Leader>ff :call FoldFunctions()<CR>
nnoremap <silent> <Leader>fs :call FoldScenarios()<CR>
nnoremap <silent> <Leader>fc :call FoldClass()<CR>
function FoldFunctions()
execute "normal zE"
g/\<function\>/ :normal $zf%za
endfunction
function FoldScenarios()
execute "normal zE"
g/\v\s(Scenario|describe)[.(]/ :normal zf%
endfunction
function FoldClass()
execute "normal zE"
g/\v^\s+(async )?[a-z0-9#_]+\([^)]*\) \{/ :normal $zf%
silent g/ static.*{$/ :normal $zf%
:nohlsearch
endfunction
" Toggle relative row numbers {{{1
nnoremap <C-n> :set relativenumber!<CR>
vnoremap <C-n> <ESC> :set relativenumber!<CR> :norm gv<CR>
" Remember English keyboard layout {{{1
nnoremap Ö :echo "⚠️ Keyboard layout ⚠️"<CR>
" Reset filetype (hack to reactivate syntax highlighting) {{{1
command Ftreset :let &ft=&ft
autocmd VimResized * :Ftreset
" Toggle booleans {{{1
nnoremap <silent> <Leader>a :call ToggleBoolean()<CR>
command Only :norm wi.onlyO// eslint-disable-next-line no-only-tests/no-only-tests
function ToggleBoolean()
let l:toggleHash = {
\ "no": "yes",
\ "yes": "no",
\ "on": "off",
\ "off": "on",
\ "true": "false",
\ "false": "true",
\}
let cursor_pos = getpos(".")
let under_cursor = expand("<cword>")
let cursor_char = nr2char(strgetchar(getline("."), col(".") - 1))
" Set 'curswant' to make vertical movement after toggling work
call add(cursor_pos, col("."))
if !has_key(l:toggleHash, under_cursor)
echo "Cannot toggle '" .. under_cursor .. "'"
return
endif
" Jump to first character in matching toggle string
if cursor_char !~? "[a-z]"
let jump_to = nr2char(strgetchar(under_cursor, 0))
keepjumps execute ":normal f" .. jump_to
endif
" Toggle!
let substitution = l:toggleHash[under_cursor]
keepjumps execute ":normal ciw" .. substitution
" Jump back to previous cursor position
call setpos(".", cursor_pos)
endfunction
" Surround {{{1
nnoremap gs( lbi(<ESC>ea)<ESC>
nnoremap gs) lbi(<ESC>ea)<ESC>
nnoremap gs{ lbi{<ESC>ea}<ESC>
nnoremap gs} lbi{<ESC>ea}<ESC>
nnoremap gs[ lbi[<ESC>ea]<ESC>
nnoremap gs] lbi[<ESC>ea]<ESC>
nnoremap gs" lbi"<ESC>ea"<ESC>
nnoremap gs' lbi'<ESC>ea'<ESC>
nnoremap gs/ lbi/<ESC>ea/<ESC>
nnoremap gs_ lbi_<ESC>ea_<ESC>
nnoremap gs* lbi*<ESC>ea*<ESC>
nnoremap gs` lbi`<ESC>ea`<ESC>
vnoremap gs( :s/\%V.*\%V./(&)/<CR>`< :noh<CR>
vnoremap gs) :s/\%V.*\%V./(&)/<CR>`< :noh<CR>
vnoremap gs{ :s/\%V.*\%V./{&}/<CR>`< :noh<CR>
vnoremap gs} :s/\%V.*\%V./{&}/<CR>`< :noh<CR>
vnoremap gs[ :s/\%V.*\%V./[&]/<CR>`< :noh<CR>
vnoremap gs] :s/\%V.*\%V./[&]/<CR>`< :noh<CR>
vnoremap gs" :s/\%V.*\%V./"&"/<CR>`< :noh<CR>
vnoremap gs' :s/\%V.*\%V./'&'/<CR>`< :noh<CR>
vnoremap gs/ :s/\%V.*\%V.//&//<CR>`< :noh<CR>
vnoremap gs_ :s/\%V.*\%V./_&_/<CR>`< :noh<CR>
vnoremap gs* :s/\%V.*\%V./*&*/<CR>`< :noh<CR>
vnoremap gs` :s/\%V.*\%V./`&`/<CR>`< :noh<CR>
" Un-surround {{{1
nnoremap gsd( lF(xf)x
nnoremap gsd) hF(xf)x
nnoremap gsd{ lF{xf}x
nnoremap gsd} hF{xf}x
nnoremap gsd[ lF[xf]x
nnoremap gsd] hF[xf]x
nnoremap gsd" hf"xF"x
nnoremap gsd' hf'xF'x
nnoremap gsd/ hf/xF/x
nnoremap gsd_ hf_xF_x
nnoremap gsd* hf*xF*x
nnoremap gsd` hf`xF`x
vnoremap gsd :s/\%V.\(.*\)\%V./\1/<CR>`< :noh<CR>
" camelCase <-> SNAKE_CASE {{{1
nnoremap <leader>CC :normal viw<CR> :s/\%V./\l&/g<CR> `< :s/\%V_\(.\)/\u\1/g<CR>`< :noh<CR>
nnoremap <leader>CS :normal viw<CR> :s/\%V[A-Z]/_&/g<CR> `< :s//\U&/g<CR> `< :noh<CR>
" Split JavaScript object to multiple lines {{{1
command SplitJS s/[{,]/&\r/g | execute "normal V$F}%<ESC>" | '<,'>s/}/\r}/g | noh
" Open all TODOs in the quickfix window {{{1
set grepprg=ag\ --nogroup\ --nocolor
command! TODO silent! grep TODO | cw | :let w:quickfix_title = "TODO list" | redraw!
" Format JS/JSON with jq {{{1
command -range JSON '<,'>!tr -d '\n' | xargs -0 printf "'\%s'" | xargs jq -n
" Highlight unwanted spacing {{{1
highlight ExtraWhitespace ctermbg=red guibg=red
autocmd BufEnter * match ExtraWhitespace /\s\+$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace //
" Matching helper {{{1
function Match(number, visual_mode)
" Visual mode? (`mode()` is always 'n', so this is ugly, but works...)
if (a:visual_mode)
let [ line_number, start_column ] = getpos("'<")[1:2]
let end_column = getpos("'>")[2]
let line = getline(line_number)
let highlight_string = line[start_column - 1:end_column - 1]
else
" Default to word under cursor
let highlight_string = expand("<cword>")
endif
execute ":" .. a:number .. "match User" .. (a:number ? a:number : "1") .. " /" .. highlight_string .. "/"
echomsg "Highlighting \"" .. highlight_string .. "\" (" .. (a:number ? a:number : "1") .. ")"
endfunction
noremap <Leader>mm :call Match("", v:false)<CR>
noremap <Leader>m1 :call Match("", v:false)<CR>
noremap <Leader>m2 :call Match(2, v:false)<CR>
noremap <Leader>m3 :call Match(3, v:false)<CR>
xnoremap <Leader>mm :call Match("", v:true)<CR>
xnoremap <Leader>m1 :call Match("", v:true)<CR>
xnoremap <Leader>m2 :call Match(2, v:true)<CR>
xnoremap <Leader>m3 :call Match(3, v:true)<CR>
command Matchoff :match | :2match | :3match
" Help "Open file under cursor" understand file names without suffix {{{1
set suffixesadd=.js,.json,.hbs,.ts
" Add common paths to help locating files {{{1
let project = system("pwd | cut -d \/ -f1-5 | tr '\n' '/'")
let &path=".,".project."app/views/,".project."views/"
" Fuzzy find relative path from current file to another, and print {{{1
nnoremap <silent> <Leader>i :silent :execute "!grealpath --relative-to " . shellescape(expand("%:h")) ." $(fd '.jsx?$' \| fzf) \| sed -E -e 's,^([^.]),./\\1,' \| xargs printf \| pbcopy"<CR> :normal "*P<CR> :redraw!<CR>
" Fuzzy find config node from `exp-config {{{1
nnoremap <silent> <Leader>I :silent :execute "!node ~/scripts/exp-config.js " . getcwd() . " \| fzf \| xargs printf \| pbcopy"<CR> :normal "*P<CR> :redraw!<CR>
" Open tig log and blame, respectively {{{1
nnoremap <silent> <Leader>L :silent !tig %<CR>:redraw!<CR>
nnoremap <silent> <Leader>l :silent :execute "!tig blame " . shellescape(expand("%")) . " +" . line(".") <CR>:redraw!<CR>
" Delete buffer. Quit if no more open buffers {{{1
nnoremap <silent> <Leader>q :call CloseOrQuit()<CR>
function CloseOrQuit()
let current_buffer = bufnr()
try
if empty(bufname(current_buffer))
:q
else
:bd
endif
catch
echohl WarningMsg | echo "Unable to close, unsaved buffer? Try :cq" | echohl none
endtry
endfunction
nnoremap <silent> <Leader>w :w<CR>
" Insert blank line below cursor {{{1
nnoremap <Leader> o
" Open a terminal {{{1
nnoremap <silent> <Leader>TT :terminal ++close<CR>
nnoremap <silent> <Leader>Tn :terminal ++close node<CR>
" Search for the visually selected string {{{1
function VisualSearch(search_direction)
let v_register = @v
normal! gv"vy
let @/ = '\V' . substitute(escape(@v, '/\'), '\n', '\\n', 'g')
call search(@/, a:search_direction == "?" ? "b" : "")
let @v = v_register
endfunction
" - v:searchforward is reset when exiting a function, so it needs to be
" manually set when searching backwards
" - The cursor is moved to column 0, so we search forward to get it in the
" first column of the match (hence the "n" and "N", respectively)
" This also sets v:hlsearch to 1 (which is also reset when exiting functions)
vnoremap * :call VisualSearch("/")<CR>\|n
vnoremap # :call VisualSearch("?")<CR>\|:let v:searchforward = 0<CR>\|N
" F-KEYS BINDINGS {{{1
" Prev/next tab
noremap <F1> gT
noremap <F2> gt
" Toggle line-wrapping
noremap <silent><F3> :set wrap!<CR>:echo (&wrap == 0 ? "No wrap" : "Wrapping")<CR>
" Toggle case-sensitivity
noremap <silent><F4> :call ToggleCase()<CR>
function ToggleCase()
let ignore_case = &ignorecase
let smart_case = &smartcase
set noignorecase " Needed for the regex match to work
if (getreg("/") =~ '[A-Z]')
set ignorecase
if (smart_case == 0)
set smartcase
else
set nosmartcase
endif
else
set smartcase
if (ignore_case == 0)
set ignorecase
else
set noignorecase " Redundant, kept for clarity
endif
endif
endfunction
" Toggle conceal level
noremap <silent><F5> :call ToggleConcealLevel()<CR>
function ToggleConcealLevel()
if (&conceallevel == 2)
set conceallevel=0
else
set conceallevel=2
endif
echo "Conceal level: " . &conceallevel
endfunction
" Toggle "list mode"
noremap <silent><F6> :set list!<CR>
" Toggle spelling
noremap <F7> :set spell!<CR>
" Turn of highlighting
noremap <F8> :noh<CR>
" Prev/next diff
nmap <F9> [c
nmap <F10> ]c
" Make k/j traverse wrapped lines
noremap j gj
noremap k gk
" Speedy quick/location list handling {{{1
nnoremap <silent><leader>j :call NextListItem()<CR>
function NextListItem()
try
if IsQuickFixOpen()
cnext
else
lnext
endif
norm zz
catch
echom "No more items (Bot)"
endtry
endfunction
nnoremap <silent><leader>k :call PrevListItem()<CR>
function PrevListItem()
try
if IsQuickFixOpen()
cprev
else
lprev
endif
norm zz
catch
echom "No more items (Top)"
endtry
endfunction
nnoremap <silent><leader>c :call CloseList()<CR>
function CloseList()
if IsQuickFixOpen()
cclose
else
lclose
endif
endfunction
function IsQuickFixOpen()
return len(filter(getwininfo(), 'v:val.quickfix && !v:val.loclist')) != 0
endfunction
" Make ctrl-x/ctrl-a work with selection {{{1
vnoremap <C-X> :g/./exe "norm \<C-X>"<CR>gv
vnoremap <C-A> :g/./exe "norm \<C-A>"<CR>gv
" Parenthesis matching {{{1
highlight MatchParen cterm=none ctermbg=red ctermfg=black
" Store folds and other view related stuff {{{1
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
" Function to remove automagically created views {{{1
function! DeleteFileView()
let path = fnamemodify(bufname("%"), ":p")
let path = substitute(path, "=", "==", "g")
if !empty($HOME)
let path = substitute(path, "^".$HOME, "\~", "")
endif
let path = substitute(path, "/", "=+", "g") . "="
let path = &viewdir . "/" . path
call delete(path)
echo "Deleted" path
endfunction
command Delview call DeleteFileView()
command Noq :noautocmd cq
" Spelling {{{1
autocmd FileType markdown setlocal spell
autocmd FileType gitcommit setlocal spell
autocmd FileType vim setlocal spell
autocmd FileType javascript setlocal spell
set spelllang=en,sv
set spellsuggest=fast,20
set spelloptions=camel
" Session handling {{{1
command Save mksession! ~/.session.vim
command Load source ~/.session.vim
" Marko syntax highlighting {{{1
autocmd BufNewFile,BufRead *.marko set filetype=html
" Fat fingers syndrome {{{1
command W w
command Q q
command Wq wq
command WQ wq
command Qa qa
command QA qa
command Wa wa
command WA wa
command Cq cq
" Quickly reset filetype when lost due to swap file collision {{{1
command Ftjs set ft=javascript
" Sort in dictionary order, ignoring case {{{1
xnoremap <leader>s :!sed 's/{ /{/' \| sort -df<CR>
" Copy/paste helper {{{1
xnoremap <leader>y "*y
nnoremap <leader>y "*y
nnoremap <leader>yy "*yy
xnoremap <leader>p "*p
nnoremap <leader>p "*p
" Pretty colors {{{1
highlight User1 ctermbg=black ctermfg=white
highlight User2 ctermbg=gray ctermfg=black
highlight User3 ctermbg=black ctermfg=yellow
highlight User4 ctermbg=black ctermfg=red
highlight Pmenu ctermbg=darkgray ctermfg=white
highlight PmenuSel ctermbg=Gray
" No idea... {{{1
if version >= 700
set nofsync
endif
set viminfo='25,\"50,n~/.viminfo