-
Notifications
You must be signed in to change notification settings - Fork 5
/
.vimrc
779 lines (655 loc) Β· 23.2 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
768
769
770
771
772
773
774
775
776
777
778
779
set encoding=utf-8
scriptencoding utf-8
" Moshe's vimrc. Custom made for my needs :)
" . . . .
" |\ /| | / \ o
" | \/ | .-. .--.|--. .-. /___\. ._.--. .
" | |( )`--.| |(.-' / \\ / | | |
" ' ' `-' `--'' `-`--' ' ``' ' `--' `-
" Basic configurations {{{
set nocompatible
" if !exists('g:syntax_on') | syntax enable | endif
if has('vim_starting')
if has('syntax') && !exists('g:syntax_on')
syntax enable
endif
endif
if executable('/bin/zsh')
set shell=/bin/zsh\ -l
elseif executable('/bin/bash')
set shell=/bin/bash
else
set shell=/bin/sh
endif
" set tags=./tags,tags;$HOME
colorscheme darkblue
" set shellcmdflag=-ic
set number " Show current line number
set relativenumber " Show relative line numbers
set linebreak " Avoid wrapping a line in the middle of a word.
set wrap
if has('nvim')
set cursorcolumn
set cursorline " Add highlight behind current line
endif
" hi cursorline cterm=none term=none
" highlight CursorLine guibg=#303000 ctermbg=234
set hlsearch " highlight reg. ex. in @/ register
set incsearch " Search as characters are typed
if exists('&inccommand')
set inccommand=split " Incremental search and replace with small split window
endif
set ignorecase " Search case insensitive...
set smartcase " ignore case if search pattern is all lowercase,
" case-sensitive otherwise
set autoread " Re-read file if it was changed from the outside
set scrolloff=7 " When about to scroll page, see 7 lines below cursor
set cmdheight=2 " Height of the command bar
set hidden " Hide buffer if abandoned
set showmatch " When closing a bracket (like {}), show the enclosing
set splitbelow " Horizontaly plitted windows open below
set splitright " Vertically plitted windows open below
" bracket for a brief second
set nostartofline " Stop certain movements from always going to the first
" character of a line.
set confirm " Prompt confirmation if exiting unsaved file
set lazyredraw " redraw only when we need to.
set noswapfile
set nobackup
set nowritebackup
set wildmenu " Displays a menu on autocomplete
set wildmode=longest:full,full " on first <Tab> it will complete to the
" longest common string and will invoke wildmenu
set title " Changes the iterm title
set showcmd
set guifont=:h
set mouse=a
set undofile " Enables saving undo history to a file
set colorcolumn=80 " Mark where are 80 characters to start breaking line
set textwidth=80
" set guicursor=i:blinkwait700-blinkon400-blinkoff250
hi ColorColumn ctermbg=238 guibg=lightgrey
set fileencodings=utf-8,cp1251
set visualbell " Use visual bell instead of beeping
set formatoptions-=t " auto break long lines"
if has('nvim')
set shortmess+=c " don't give |ins-completion-menu| messages.
set shortmess-=l " Print "lines" instead of "L"
set shortmess-=q " Print the name of the macro when recording
set shortmess-=S " Print the search count message
endif
" Ignore node_modules
set wildignore+=**/node_modules/**
set wildignore+=.hg,.git,.svn,*.DS_Store,*.pyc
" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
" delays and poor user experience.
set updatetime=300
if v:version > 703 || v:version == 703 && has('patch541')
set formatoptions+=j " Delete comment character when joining commented lines
endif
filetype plugin on
filetype plugin indent on
if has('nvim')
set list
set listchars=tab:βΒ·,trail:Β·,precedes:ο¨,extends:ο©,eol:β²,
" set lcscope=tab:βΒ·,trail:Β·,precedes:ο¨,extends:ο©
set fillchars=vert:\|,fold:Β·
set noemoji
endif
set path+=** " When searching, search also subdirectories
" Set python path
if executable('/usr/local/bin/python3')
let g:python3_host_prog='/usr/local/bin/python3'
elseif executable('/usr/bin/python3')
let g:python3_host_prog='/usr/bin/python3'
endif
" Auto load file changes when focus or buffer is entered
augroup ReloadFile
autocmd!
au FocusGained,BufEnter * :checktime
augroup END
if &history < 1000
set history=1000
endif
" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved. {{{
if has('nvim-0.5.0') || has('patch-8.1.1564')
" Recently vim can merge signcolumn and number column into one
set signcolumn=auto
elseif exists('&signcolumn')
set signcolumn=yes
endif
" }}}
" set verbose=1
if has('termguicolors')
" let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
" let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
" }}}
" Indentation {{{
" Indentation settings for using 4 spaces instead of tabs.
" Do not change 'tabstop' from its default value of 8 with this setup.
filetype indent on
if exists('+breakindent')
set breakindent " Maintain indent on wrapping lines
endif
set autoindent " always set autoindenting on
set copyindent " copy the previous indentation on autoindenting
set smartindent " Number of spaces to use for each step of (auto)indent.
set shiftwidth=4 " Number of spaces for each indent
set softtabstop=4
set tabstop=4
set smarttab " insert tabs on the start of a line according to
" shiftwidth, not tabstop
set expandtab " Tab changes to spaces. Format with :retab
function! s:ChangeIndentNum() abort
call inputsave()
let the_num = str2nr(input('Enter new indent: '))
call inputrestore()
exe 'set shiftwidth=' . the_num
exe 'set softtabstop=' . the_num
exe 'set tabstop=' . the_num
endfunction
nnoremap cii :<C-u>call <SID>ChangeIndentNum()<CR>
" }}}
" Statusline {{{
set statusline=%.50F\ -\ FileType:\ %y
set statusline+=%= " Switch to the right side
set statusline+=%l " Current line
set statusline+=/ " Separator
set statusline+=%L\ " Total lines
set showtabline=2
" }}}
" Mappings {{{
" Map leader to space
let mapleader=' '
let maplocalleader = "\\"
" Map 0 to first non-blank character
nnoremap 0 ^
" Move to the end of the line
nnoremap L $zL
vnoremap L $
nnoremap H 0zH
vnoremap H 0
"indent/unindent visual mode selection with tab/shift+tab
vmap <tab> >gv
vmap <s-tab> <gv
" Sudo write
command! W w :term sudo tee % > /dev/null
" Copy number of lines and paste below
nnoremap <leader>cp :<c-u>exe 'normal! y' . (v:count == 0 ? 1 : v:count) . 'j' . (v:count == 0 ? 1 : v:count) . 'jo<C-v><Esc>p'<cr>
" Windows mappings
nnoremap <Leader><Leader> <C-^>
nnoremap <tab> <c-w>w
nnoremap <c-w><c-c> <c-w>c
nnoremap <leader>n :bn<cr>
nnoremap <c-w>v :vnew<cr>
nnoremap <c-w>s :new<cr>
nnoremap <c-w>e :enew<cr>
" Delete current buffer
nnoremap <silent> <leader>bd :bp <bar> bd #<cr>
" Close current buffer
nnoremap <silent> <leader>bc :close<cr>
" Split navigations mappings
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
" Run macro
nnoremap Q @q
" Paste in insert mode
inoremap <c-v> <c-r>"
" Quickfix
nnoremap ]q :cnext<cr>zz
nnoremap [q :cprev<cr>zz
nnoremap ]l :lnext<cr>zz
nnoremap [l :lprev<cr>zz
" This creates a new line of '=' signs the same length of the line
nnoremap <leader>= yypVr=
" Map dp and dg with leader for diffput and diffget
nmap <leader>dp :diffput<cr>
nmap <leader>dg :diffget<cr>
nmap <leader>du :windo diffoff <bar> windo diffupdate<cr>
nmap <leader>dn :windo diffthis<cr>
nmap <leader>df :windo diffoff<cr>
" Map enter to no highlight
nnoremap <silent> <CR> :nohlsearch<CR><CR>
" Set mouse=v mapping
nnoremap <leader>ma :set mouse=a<cr>
nnoremap <leader>mv :set mouse=v<cr>
" Search mappings
nnoremap <silent> * :execute "normal! *N"<cr>
nnoremap <silent> # :execute "normal! #n"<cr>
nnoremap <expr> n 'Nn'[v:searchforward]
xnoremap <expr> n 'Nn'[v:searchforward]
onoremap <expr> n 'Nn'[v:searchforward]
nnoremap <expr> N 'nN'[v:searchforward]
xnoremap <expr> N 'nN'[v:searchforward]
onoremap <expr> N 'nN'[v:searchforward]
" Search visually selected text with // or * or #
vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>
function! s:StarSearch(cmdtype) abort
let old_reg=getreg('"')
let old_regtype=getregtype('"')
norm! gvy
let @/ = '\V' . substitute(escape(@", a:cmdtype . '\.*$^~['), '\_s\+', '\\_s\\+', 'g')
norm! gVzv
call setreg('"', old_reg, old_regtype)
endfunction
vnoremap * :<C-u>call <SID>StarSearch('/')<CR>/<C-R>=@/<CR><CR>
vnoremap # :<C-u>call <SID>StarSearch('?')<CR>?<C-R>=@/<CR><CR>
" Map - to move a line down and _ a line up
nnoremap - "ldd$"lp
nnoremap _ "ldd2k"lp
" Base64 dencode
vnoremap <silent><leader>46 c<c-r>=substitute(system('base64 --decode', @"), '\n$', '', 'g')<cr><esc>
vnoremap <silent><leader>64 c<c-r>=substitute(system('base64', @"), '\n$', '', 'g')<cr><esc>
" Map U to toggle word to uppercase/lowercase in insert and normal and
" visual
nnoremap U viw~
vnoremap U ~
" Vimrc edit mappings
nnoremap <silent> <leader>ev :execute("vsplit ".'~/.vimrc')<cr>
nnoremap <silent> <leader>ep :execute("vsplit ".'~/.vimrcplugins')<cr>
" highlight last inserted text
nnoremap gV `[v`]
" Exit insert mode
inoremap jk <esc>
nnoremap <leader>qq :qall<cr>
" Copy entire file to clipboard
nnoremap Y :%y+<cr>
" Search and Replace
nnoremap <Leader>r :.,$s?<C-r><C-w>?<C-r><C-w>?gc<Left><Left><Left>
vnoremap <leader>r "hy:.,$s?<C-r>h?<C-r>h?gc<left><left><left>
vnoremap <leader>dab "hyqeq:v?\V<c-r>h?d E<cr>:let @"=@e<cr>:noh<cr>
vnoremap <leader>daa "hyqeq:g?\V<c-r>h?d E<cr>:let @"=@e<cr>:noh<cr>
vnoremap <leader>yab "hymmqeq:v?\V<c-r>h?yank E<cr>:let @"=@e<cr>`m:noh<cr>
vnoremap <leader>yaa "hymmqeq:g?\V<c-r>h?yank E<cr>:let @"=@e<cr>`m:noh<cr>
vnoremap <leader>p "_dP
" Change every " -" with " \<cr> -" to break long lines of bash
nnoremap <silent> <leader>\ :.s/ -/ \\\r -/g<cr>:noh<cr>
" Every parameter in its own line
function SplitParamLines() abort
let f_line_num = line('.')
let indent_length = indent(f_line_num)
exe "normal! 0f(a\<cr>\<esc>"
exe ".s/\s*,/,\r" . repeat(' ', indent_length + &shiftwidth - 1) . '/g'
nohlsearch
exe "normal! 0t)a\<cr>\<esc>"
endfunction
nnoremap <silent> <leader>( :call SplitParamLines()<cr>
" Change \n to new lines
nmap <silent> <leader><cr> :silent! %s?\\n?\r?g<bar>silent! %s?\\t?\t?g<bar>silent! %s?\\r?\r?g<cr>:noh<cr>
" move vertically by visual line (don't skip wrapped lines)
nnoremap j gj
nnoremap k gk
" Change working directory based on open file
nnoremap <leader>cd :cd %:p:h<CR>:pwd<CR>
" Move visually selected block
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
" Convert all tabs to spaces
nnoremap <leader>ct<space> :retab<cr>
" Copy file path to clipboard
nnoremap <silent> <leader>cfp :let @+ = expand('%')<cr>:echo "Copied file path " . expand('%')<cr>
"" }}}
" Netrw (directory browsing) out-of-the-box plugin {{{
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 25
let g:netrw_keepdir = 1
" augroup ProjectDrawer
" autocmd!
" autocmd VimEnter * :if !exists("NERDTree") | Vexplore | endif
" augroup END
map <silent> <C-o> :Lexplore<CR>
" }}}
" Enable folding {{{
set foldenable
set foldmethod=syntax
set foldlevel=999
set foldlevelstart=10
" Enable folding with the leader-f/a
nnoremap <leader>f za
nnoremap <leader>caf zM
nnoremap <leader>oaf zR
" Open level folds
nnoremap <leader>olf zazczA
" }}}
" Abbreviations {{{
inoreabbrev teh the
inoreabbrev seperate separate
inoreabbrev dont don't
inoreabbrev rbm # TODO: remove before merging
inoreabbrev cbm # TODO: change before merging
inoreabbrev ubm # TODO: uncomment before merging
inoreabbrev funciton function
inoreabbrev functiton function
inoreabbrev fucntion function
inoreabbrev funtion function
inoreabbrev erturn return
inoreabbrev retunr return
inoreabbrev reutrn return
inoreabbrev reutn return
inoreabbrev queyr query
inoreabbrev htis this
inoreabbrev foreahc foreach
inoreabbrev forech foreach
" }}}
" Conceals {{{
let g:conceal_rules = [
\ ['!=', 'β '],
\ ['<=', 'β€'],
\ ['>=', 'β₯'],
\ ['=>', 'β'],
\ ['==', 'β‘'],
\ ['===', 'β‘β‘'],
\ ['\<function\>', 'Ζ'],
\ ]
" Conceal is not needed when we have FiraCode with ligatures
" for [value, display] in g:conceal_rules
" execute "call matchadd('Conceal', '".value."', 10, -1, {'conceal': '".display."'})"
" endfor
set conceallevel=1
" call matchadd('Conceal', 'package', 10, 99, {'conceal': 'p'})
"}}}
" Diff with last save function {{{
function! s:DiffWithSaved()
let filetype=&ft
diffthis
vnew | r # | normal! 1Gdd
exe 'setlocal bt=nofile bh=wipe nobl noswf ro foldlevel=999 ft=' . filetype
diffthis
nnoremap <buffer> q :bd!<cr>
augroup ShutDownDiffOnLeave
autocmd! * <buffer>
autocmd BufDelete,BufUnload,BufWipeout <buffer> wincmd p | diffoff |
\wincmd p
augroup END
wincmd p
endfunction
com! DiffSaved call s:DiffWithSaved()
nnoremap <leader>ds :DiffSaved<cr>
" }}}
" Special filetypes {{{
augroup special_filetype
au!
autocmd FileType json syntax match Comment +\/\/.\+$+
autocmd BufNewFile,BufRead aliases.sh setf zsh
autocmd BufNewFile,BufRead .eslintrc setf json
" autocmd BufNewFile,BufRead *.hcl setf terraform
autocmd BufRead,BufNewFile */templates/*.yaml,*/templates/*.tpl,*.gotmpl,helmfile.yaml set ft=helm
autocmd FileType javascript set filetype=javascriptreact | set iskeyword+=-
augroup end
augroup custom_nginx
autocmd!
autocmd FileType nginx setlocal iskeyword+=$
autocmd FileType nginx let b:coc_additional_keywords = ['$']
augroup end
let g:sh_fold_enabled = 4
com! FormatJSON exe '%!python -m json.tool'
function FormatEqual() abort
let save_cursor = getcurpos()
normal! gg=G
silent! exe '%s#)\zs\ze{# #g'
call setpos('.', save_cursor)
echom 'Formatted with equalprg'
endfunction
" }}}
" Run current buffer {{{
nnoremap <silent> <F5> :call ExecuteFile()<CR>
" Will attempt to execute the current file based on the `&filetype`
" You need to manually map the filetypes you use most commonly to the
" correct shell command.
function! ExecuteFile()
let l:filetype_to_command = {
\ 'javascript': 'node',
\ 'python': 'python3',
\ 'html': 'open',
\ 'sh': 'bash'
\ }
call inputsave()
let sure = input('Are you sure you want to run the current file? (y/n): ')
call inputrestore()
if sure !=# 'y'
return ''
endif
echo ''
let l:cmd = get(l:filetype_to_command, &filetype, 'bash')
:%y
new | 0put
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
exe '%!'.l:cmd
normal! ggO
call setline(1, 'Output of ' . l:cmd . ' command:')
normal! yypVr=o
endfunction
" }}}
" Grep {{{
" This is only availale in the quickfix window, owing to the filetype
" restriction on the autocmd (see below).
function! <SID>OpenQuickfix(new_split_cmd)
" 1. the current line is the result idx as we are in the quickfix
let l:qf_idx = line('.')
" 2. jump to the previous window
wincmd p
" 3. switch to a new split (the new_split_cmd will be 'vnew' or 'split')
execute a:new_split_cmd
" 4. open the 'current' item of the quickfix list in the newly created buffer
" (the current means, the one focused before switching to the new buffer)
execute l:qf_idx . 'cc'
endfunction
augroup grep_augroup
autocmd!
autocmd QuickFixCmdPost [^l]* copen
autocmd QuickFixCmdPost l* lopen
autocmd FileType qf nnoremap <buffer> <C-v> :call <SID>OpenQuickfix("vnew")<CR>
autocmd FileType qf nnoremap <buffer> <C-x> :call <SID>OpenQuickfix("split")<CR>
augroup END
" Set grepprg as RipGrep or ag (the_silver_searcher), fallback to grep
if executable('rg')
let &grepprg="rg --vimgrep --no-heading --smart-case --hidden --follow -g '!{" . &wildignore . "}' $*"
set grepformat=%f:%l:%c:%m,%f:%l:%m
elseif executable('ag')
let &grepprg='ag --vimgrep --smart-case --hidden --follow --ignore "!{' . &wildignore . '}" $*'
set grepformat=%f:%l:%c:%m
else
let &grepprg='grep -n -r --exclude=' . shellescape(&wildignore) . ' . -- $*'
endif
function s:RipGrepCWORD(bang, visualmode, ...) abort
let search_word = a:1
if a:visualmode
" Get the line and column of the visual selection marks
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
" Get all the lines represented by this range
let lines = getline(lnum1, lnum2)
" The last line might need to be cut if the visual selection didn't end on the last column
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
" The first line might need to be trimmed if the visual selection didn't start on the first column
let lines[0] = lines[0][col1 - 1:]
" Get the desired text
let search_word = join(lines, "\n")
endif
if search_word ==? ''
let search_word = expand('<cword>')
endif
echom 'Searching for ' . search_word
" Silent removes the "press enter to continue" prompt, and bang (!) is for
" not jumping to the first result
let grepcmd = 'silent grep' . a:bang .' -- ' . shellescape(search_word)
execute grepcmd
endfunction
command! -bang -range -nargs=? RipGrepCWORD call <SID>RipGrepCWORD("<bang>", v:false, <q-args>)
command! -bang -range -nargs=? RipGrepCWORDVisual call <SID>RipGrepCWORD("<bang>", v:true, <q-args>)
nmap <c-f> :RipGrepCWORD!<Space>
vmap <c-f> :RipGrepCWORDVisual!<cr>
" }}}
" " Highlight word under cursor {{{
" let g:word_highlight#disabled_ft = [
" \'qf',
" \'fugitive',
" \'nerdtree',
" \'gundo',
" \'diff',
" \'fzf',
" \'floaterm',
" \'vim-plug'
" \]
" let g:word_highlight#disabled_buftypes = ['terminal', 'quickfix', 'help']
" let g:word_highlight#linked_group = 'NormalFloat'
" let g:word_highlight#current_word_match_id = 666
" function! s:HighlightWordUnderCursor()
" let l:current_word_match_group_name = 'MatchWord'
" let l:default_linked_group = 'CursorLine'
" let l:linked_group = get(g:, 'word_highlight#linked_group', l:default_linked_group)
" let l:extra_highlights = 'cterm=undercurl gui=undercurl'
" " Clear matches
" let matches_list = getmatches()
" let current_word_match_id = get(g:, 'word_highlight#current_word_match_id', -1)
" for match in matches_list
" let match_id = get(match, 'id', '-1')
" if match_id == g:word_highlight#current_word_match_id
" call matchdelete(g:word_highlight#current_word_match_id)
" end
" endfor
" " Don't match non-words, diff buffer, disabled_buftypes and disabled_ft
" if matchstr(getline('.'), '\%' . col('.') . 'c.') !~# '\k' | return 0 | endif
" if &diff | return 0 | endif
" if index(get(g:, 'word_highlight#disabled_buftypes', []), &buftype) >= 0 | return 0 | endif
" if index(get(g:,'word_highlight#disabled_ft', []), &filetype) >= 0 | return 0 | endif
" if !hlexists(l:linked_group)
" let l:linked_group = default_linked_group
" endif
" " Add highlight group
" " BG
" let cterm_bg_color = synIDattr(synIDtrans(hlID(l:linked_group)), 'bg', 'cterm')
" let cterm_bg_color = cterm_bg_color != '' ? cterm_bg_color : '240'
" let gui_bg_color = synIDattr(synIDtrans(hlID(l:linked_group)), 'bg', 'gui')
" let gui_bg_color = gui_bg_color != '' ? gui_bg_color : '#6d737d'
" " FG
" let cterm_fg_color = synIDattr(synIDtrans(hlID(l:linked_group)), 'fg', 'cterm')
" let cterm_fg_color = cterm_fg_color != '' ? cterm_fg_color : 'white'
" let gui_fg_color = synIDattr(synIDtrans(hlID(l:linked_group)), 'fg', 'gui')
" let gui_fg_color = gui_fg_color != '' ? gui_fg_color : 'white'
" let hi_text = 'guibg=' . gui_bg_color .
" \ ' ctermbg=' . cterm_bg_color .
" \ ' guifg=' . gui_fg_color .
" \ ' ctermfg=' . cterm_fg_color
" exec 'hi ' . l:current_word_match_group_name . ' ' . l:extra_highlights . ' ' . hi_text
" " Add matches
" let cword_clean = substitute(expand('<cword>'), '/', '\/', 'g')
" call matchadd(l:current_word_match_group_name, '\V\<' . cword_clean . '\>', -5, g:word_highlight#current_word_match_id)
" endfunction
" augroup MatchWord
" autocmd!
" autocmd! CursorHold,CursorHoldI * call <SID>HighlightWordUnderCursor()
" augroup END
" " }}}
" Terminal configurations {{{
if exists(':terminal')
if !exists('g:terminal_ansi_colors')
let g:terminal_ansi_colors = [
\'#21222C',
\'#FF5555',
\'#69FF94',
\'#FFFFA5',
\'#D6ACFF',
\'#FF92DF',
\'#A4FFFF',
\'#FFFFFF',
\'#636363',
\'#F1FA8C',
\'#BD93F9',
\'#FF79C6',
\'#8BE9FD',
\'#F8F8F2',
\'#6272A4',
\'#FF6E6E'
\]
endif
" Function to set terminal colors
fun! s:setTerminalColors()
if exists('g:terminal_ansi_colors')
for i in range(len(g:terminal_ansi_colors))
exe 'let g:terminal_color_' . i . ' = g:terminal_ansi_colors[' . i . ']'
endfor
unlet! g:terminal_ansi_colors
endif
endfunction
augroup TerminalAugroup
autocmd!
" Start terminal in insert mode
autocmd BufEnter * if &buftype == 'terminal' | :startinsert | endif
" Call terminal colors function only after colorscheme changed
autocmd Colorscheme * call <sid>setTerminalColors()
augroup END
tnoremap <Esc> <C-\><C-n>
" To force using 256 colors
set t_Co=256
endif
" }}}
" {{{ Visual Calculator
function s:VisualCalculator() abort
let save_pos = getpos('.')
" Get visual selection
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
let first_expr = join(lines, "\n")
" Get arithmetic operation from user input
call inputsave()
let operation = input('Enter operation: ')
call inputrestore()
" Calculate final result
let fin_result = eval(str2nr(first_expr) . operation)
" Replace
exe 's/\%V' . first_expr . '/' . fin_result . '/'
call setpos('.', save_pos)
endfunction
command! -range VisualCalculator call <SID>VisualCalculator()
vmap <c-r> :VisualCalculator<cr>
" }}}
" Last position on document {{{
if has('autocmd')
augroup redhat
autocmd!
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ endif
augroup END
endif
" }}}
" YamlToJson JsonToYaml {{{
function! YamlToJson() abort
% !python3 -c 'import yaml, json, sys; print(json.dumps(yaml.safe_load(sys.stdin)));'
" set filetype=json
" FormatJSON
endfunction
function! JsonToYaml() abort
% !python -c 'import yaml, json, sys; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)'
" set filetype=yaml
endfunction
com! JsonToYaml call JsonToYaml()
com! YamlToJson call YamlToJson()
" }}}
" Decrypt encrypt ansible secret {{{
function DencryptAnsibleSecretFile(...) abort
let action = 'de'
if get(a:, 1, v:false)
let action = 'en'
endif
silent! exe '!ansible-vault ' . action . 'crypt --vault-password-file ~/ansible_secret %'
endfunction
com! EncryptAnsible call DencryptAnsibleSecretFile(1)
com! DecryptAnsible call DencryptAnsibleSecretFile()
" }}}