-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimrc
803 lines (660 loc) · 19.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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
" default
runtime! debian.vim
if has("syntax")
syntax enable
endif
if filereadable("/etc/vim/vimrc.local")
silent! source /etc/vim/vimrc.local
endif
if has("autocmd")
au bufreadpost * if line("'\"") > 1 && line("'\"") <= line("$")
\| exe "normal! g'\"" | endif
endif
if &term =~ '^screen'
execute "set <s-up>=\e[1;2A"
execute "set <s-down>=\e[1;2B"
execute "set <s-right>=\e[1;2C"
execute "set <s-left>=\e[1;2D"
endif
call pathogen#infect("bundle/{}", "~/.dotfiles/vim/scripts/{}")
call pathogen#helptags()
filetype plugin indent on
" settings
set fileformats=unix
set showcmd
set autowrite
set ttyfast lazyredraw
set nu rnu
set nowrap
set splitbelow splitright
set incsearch
" set completeopt+=longest
set timeoutlen=280
set colorcolumn=80
set textwidth=79
set scrolloff=25
set noshowmatch
set wildmenu wildmode=list:longest,list,full
set wildignore+=*.o,*.hi,*.pyc
set backspace=indent,eol,start
set nf+=alpha
set laststatus=2
set t_Co=256
set pumheight=10
set complete+=k
set tabpagemax=100
set list lcs=tab:\·\
" colorscheme
set background=dark
silent! colorscheme solarized
" backup to ~/.tmp; get rid of .swp files
set backup
set backupdir=~/.vim-tmp,~/.tmp,/var/tmp,/tmp
set backupskip=/tmp/*,/private/tmp/*
set directory=~/.vim-tmp,~/.tmp,/var/tmp,/tmp
set writebackup
" indentation
set autoindent
set noexpandtab tabstop=4 shiftwidth=4
" code folding settings
set foldignore=
set foldmethod=indent
set foldnestmax=10
set nofoldenable foldlevel=1
" italic comments
highlight Comment cterm=underline
set t_ZH=[3m
set t_ZR=[23m
" UltiSnips
let g:UltiSnipsExpandTrigger = "<c-j>"
let g:UltiSnipsJumpForwardTrigger = "<c-j>"
let g:UltiSnipsJumpBackwardTrigger = "<c-k>"
let g:UltiSnipsSnippetDirectories = ["ultisnips"]
" NERDCommenter
let NERDSpaceDelims = 1
let g:NERDCustomDelimiters = {
\ "c": { "left": "//", "right": "",
\ "leftAlt": "/*","rightAlt": "*/" },
\ "pgsql": {"left": "--"},
\ "hack_asm": {"left": "//"},
\ "hack_vm": {"left": "//"}
\}
" NERDTree key-maps
let NERDTreeMapOpenSplit="s"
let NERDTreeMapOpenVSplit="v"
let NERDTreeMapActivateNode="h"
let NERDTreeMapToggleHidden="<c-h>"
let NERDTreeMapJumpFirstChild="<leader>k"
let NERDTreeMapJumpLastChild="<leader>j"
let NERDTreeWinSize=30
let NERDTreeWinSize=28
let NERDTreeIgnore = ["__pycache__", '\.pyc$']
" emmet
imap <c-e> <c-y>,
let g:user_emmet_install_global = 0
autocmd FileType html,css EmmetInstall
" ctrlp
let g:ctrlp_prompt_mappings = {
\ 'AcceptSelection("h")': ['<m-j>'],
\ 'AcceptSelection("v")': ['<m-k>'],
\ 'AcceptSelection("t")': ['<m-l>'],
\}
" smart pasting
let &t_SI .= "\<esc>[?2004h"
let &t_EI .= "\<esc>[?2004l"
" tmux/vim pane navigation
let previous_title = substitute
\(system("tmux display-message -p '#{pane_title}'"), '\n', '', '')
let &t_ti = "\<esc>]2;vim\<esc>\\" . &t_ti
let &t_te = "\<esc>]2;". previous_title . "\<esc>\\" . &t_te
" Synclude
let g:synclude_matches_file = "~/.dotfiles/vim/matches.syn"
" TapSurround
let g:surround_close_char = {
\"<" : ">",
\"{" : "}",
\"(" : ")",
\'"' : '"',
\"'" : "'",
\"[" : "]",
\"*" : "*",
\"$" : "$",
\"`" : "`"
\}
" highlighting/syntax
hi cursorlinenr ctermfg=red ctermbg=0
hi folded cterm=none,bold ctermbg=2 ctermfg=black
hi wildmenu cterm=none ctermfg=232 ctermbg=2
hi specialkey cterm=none ctermfg=darkgrey ctermbg=none
hi nontext ctermfg=red
hi vertsplit ctermfg=black ctermbg=2
hi statuslinenc cterm=none ctermfg=black ctermbg=2
hi NonText ctermbg=15
hi Search ctermbg=1 ctermfg=8
hi SpellBad cterm=bold ctermfg=196
hi statusline cterm=none ctermbg=235
hi statuslinenc ctermfg=none ctermbg=236
hi MatchParen cterm=bold ctermfg=45 ctermbg=none
hi _extraWhitespace ctermbg=88
" autocomplete menu
hi pmenu cterm=none ctermbg=2 ctermfg=233
hi pmenusel ctermbg=233 ctermfg=red
hi pmenusbar cterm=none ctermbg=black
hi pmenuthumb cterm=none ctermbg=red
" tab-bar
hi TabLineFill cterm=none ctermbg=15
hi TabLineSel cterm=none ctermfg=231 ctermbg=32
hi TabLine cterm=none ctermfg=7 ctermbg=15
" statusline
hi User1 ctermfg=231 ctermbg=31
hi User2 ctermfg=160 ctermbg=31
hi User3 ctermfg=22 ctermbg=118
hi User4 ctermfg=231 ctermbg=1
hi User5 cterm=none ctermbg=235
hi User6 ctermfg=231 ctermbg=31
hi User7 cterm=bold ctermfg=234 ctermbg=253
hi CurrentWordTwins cterm=bold ctermfg=8
" autocommands
func! LocalVimrc()
" Searches up the open file's directory tree for a file named
" ".vimrc.local"; if one exists, sources it.
if exists("b:local_vimrc_loaded")
return
endif
let b:local_vimrc_loaded = 1
let dir_to_use = expand("%:p:")
if len(dir_to_use) == 0
let dir_to_use = getcwd()
endif
let cmd = "$HOME/.dotfiles/vim/scripts/local_vimrc.zsh " . dir_to_use
let local_vimrc_path = system(cmd)
if len(local_vimrc_path) != 0
" exec ":source " . local_vimrc_path . "\<cr>"
silent! exec "source " . local_vimrc_path
endif
endfunc
match _extraWhitespace /\s\+$/
augroup miscellaneous
au!
au WinEnter,BufRead,BufNewFile * silent! call StatusLine()
au WinEnter * call NERDTreeQuit()
au WinLeave * silent! call StatusLineNC()
au VimResized * wincmd =
au InsertEnter * hi _extraWhitespace ctermbg=8
au InsertEnter,WinLeave * set nornu
au InsertEnter * set timeoutlen=140
au InsertLeave * set timeoutlen=280
au InsertLeave * hi _extraWhitespace ctermbg=88
au InsertLeave,WinEnter * silent! exe &nu?"set rnu":""
au FileType * exe "setlocal dict+=~/.vim/dict/" . &filetype . ".txt"
au FileType cpp set filetype=c.cpp
au FileType sql set filetype=pgsql.sql
au FileType node set filetype=node.javascript
au FileType arduino set filetype=processing
au bufnewfile * silent! call s:LoadTemplate()
au BufRead {.,}pylintrc set ft=dosini
au BufRead,BufNewFile *.json set filetype=javascript.json
au BufReadPre,BufNewFile *.md let g:markdown_fenced_languages = [
\"c", "python", "javascript", "sh"
\]
au BufRead,BufNewFile *.tmp exe "set ft=template." .
\split(expand("%:t:r"), "_")[0]
au BufRead *.supp set filetype=supp
au BufRead *.val set filetype=valgrind
au BufRead *.html.j2 set filetype=django
au BufRead gitconfig set filetype=gitconfig
au BufRead psqlrc set filetype=pgsql
au BufRead .psqlrc set filetype=pgsql
au BufRead,BufEnter,BufNewFile *.plot,*.gnuplot set filetype=gnuplot
au BufReadPost ~/.vimrc exe "normal! zM"
au BufWrite,BufRead,BufEnter * :let &titlestring='xterm: ' . expand('%:t')
au BufEnter,Filetype * call LocalVimrc()
augroup END
" commands
com! ToggleUniversalFold :set foldmethod=indent <bar> exe "norm! " .
\(&foldlevel != 0?"zM":"zR")
com! -nargs=1 Ftpackage so ~/.dotfiles/vim/ftpackage/<args>.vim
com! Haste :echo system("command haste " . shellescape(expand("%:p")))
com! Dpaste :call Dpaste()
" key mappings
let mapleader = " "
" global
map c <plug>NERDCommenterToggle
" normal
norem <leader>f :NERDTreeToggle<cr>
nnorem <leader>ev :tabf $MYVIMRC<cr>
nnorem <leader>ef :call OpenFtpluginFile()<cr>
nnorem <leader>eu :call OpenUltiSnipsFile()<cr>
nnorem <leader>et :call OpenTemplateFile()<cr>
no <leader>ss :call SynStack()<cr>
nnorem <leader>w <esc>:w<cr>
nnorem <leader>q <esc>:q<cr>
nnorem <leader>wq <esc>:wq<cr>
nnorem <leader>fq <esc>:q!<cr>
nnorem <leader>wa <esc>:wa<cr>
nnorem <c-e> <c-w>
" faster navigation
nnorem H b
nnorem L w
nnorem J 4j
nnorem K 4k
nnorem <leader>l $
nnorem <leader>h ^
nnorem <leader>j G
nnorem <leader>k gg
nnorem <leader>n :set rnu!<cr>
nnorem + <c-a>
nnorem _ <c-x>
nnorem = =<cr>
nnorem <c-f> zA
nnorem zf za
nnorem <silent> za :ToggleUniversalFold<cr>
nnorem <leader>t :tabnext<cr>
nnorem <leader>st :tabprev<cr>
nnorem <leader>r :wincmd r<cr>
nnorem sv :source ~/.vimrc<cr>
nnorem ss :silent! sp
nnorem vv :silent! vsp
nnorem ;vsp :echo "Nope."
nnorem ;sp :echo "Nope."
nnorem <c-b> <c-v>
nnorem <leader>rt :call HardRetab("soft")<cr>
nnorem tt :silent! tabe
nnorem <tab> >>
nnorem <s-tab> <<
nnorem <up> <esc>:call ResizeUp()<cr>
nnorem <down> <esc>:call ResizeDown()<cr>
nnorem <left> <esc>:call ResizeLeft()<cr>
nnorem <right> <esc>:call ResizeRight()<cr>
nmap <s-up> <up><up><up>
nmap <s-down> <down><down><down>
nmap <s-left> <left><left><left>
nmap <s-right> <right><right><right>
norem <c-m> <c-]>
norem <c-n> <c-T>
nnoremap <m-m> :vsplit<cr><c-]>
nnoremap <m-n> :split<cr><c-]>
nnoremap s /
nnoremap S ?
nnoremap <leader>c :let @+=expand("<cword>")<cr>
nmap g <Plug>(easymotion-prefix)
nnoremap m gg
nnorem ; :
nnorem : <nop>
nnorem \ @
nnorem @ <nop>
nnorem <leader>p :let @+ = expand("%:p")<cr>
nnorem <leader>e <esc>:call HighlightCurrentWord()<cr>
" tmux/vim pane navigation
if exists('$TMUX')
nnorem <silent> <c-h> :call TmuxOrSplitSwitch('h', 'L')<cr>
nnorem <silent> <c-j> :call TmuxOrSplitSwitch('j', 'D')<cr>
nnorem <silent> <c-k> :call TmuxOrSplitSwitch('k', 'U')<cr>
nnorem <silent> <c-l> :call TmuxOrSplitSwitch('l', 'R')<cr>
else
map <c-h> <c-w>h
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
endif
" operator-pending
" faster navigation
onorem H b
onorem L w
onorem J 4j
onorem K 4k
onorem <leader>l $
onorem <leader>h ^
onorem <leader>j G
onorem <leader>k gg
" insert
inorem <special><expr> <esc>[200~ SmartPaste()
im <c-z> <plug>NERDCommenterInsert
" Scroll up/down auto-complete menu with j/k
inorem <expr> J ((pumvisible())?("\<c-n>\<c-n>\<c-n>"):("J"))
inorem <expr> K ((pumvisible())?("\<c-p>\<c-p>\<c-p>"):("K"))
inorem jk <esc>
inorem <c-p> <c-x><c-f>
inorem <c-n> <esc>:call SkipPastSymbol()<cr>a
inorem <tab> <c-r>=Tab_Or_Complete()<cr>
inorem <s-tab> <c-p>
inorem <bs> <c-r>=SmartBackspace(col("."), virtcol("."))<cr>
inorem <up> <esc>:call ResizeUp()<cr>i
inorem <down> <esc>:call ResizeDown()<cr>i
inorem <left> <esc>:call ResizeLeft()<cr>i
inorem <right> <esc>:call ResizeRight()<cr>i
inorem " ""<left>
inorem "" "
inorem ' ''<left>
inorem '' '
inorem ` ``<left>
inorem `` `
inorem <silent> <c-h> <esc>:TmuxNavigateLeft<cr>
inorem <silent> <c-j> <esc>:TmuxNavigateDown<cr>
inorem <silent> <c-k> <esc>:TmuxNavigateUp<cr>
inorem <silent> <c-l> <esc>:TmuxNavigateRight<cr>
inorem <silent> <c-\> <esc>:TmuxNavigatePrevious<cr>
inorem ( ()<left>
inorem (( ()
inorem [ []<left>
inorem [[ []
inorem { {<cr>}<esc>O
inorem {{ {}<left>
inorem <c-z> x<esc>:call EscapeAbbreviation()<cr>a
inorem <c-o> <esc>o
inorem <c-e> <c-w>
" visual
" Faster navigation
vnorem <leader>l $
vnorem <leader>h ^
vnorem <leader>j G
vnorem <leader>k gg
vnorem H b
vnorem L w
vnorem J 4j
vnorem K 4k
vnorem <leader>c "+y
vnoremap <tab> :<bs><bs><bs><bs><bs>call VisualIndent()<cr>
vnoremap <s-tab> :<bs><bs><bs><bs><bs>call VisualDeindent()<cr>
vnorem // y/<c-r>"<cr>
vnorem ?? y?<c-r>"<cr>
vnorem s :sort<cr>
nm f gf
nm F gF
" functions
func! NERDTreeQuit()
" If any NERDTree buffers are open, close them.
redir => buffersoutput
silent buffers
redir END
let pattern = '^\s*\(\d\+\)\(.....\) "\(.*\)"\s\+line \(\d\+\)$'
let windowfound = 0
for bline in split(buffersoutput, "\n")
let m = matchlist(bline, pattern)
if (len(m) > 0)
if (m[2] =~ '..a..')
let windowfound = 1
endif
endif
endfor
if (!windowfound)
quitall
endif
endfunc
func! OpenFtpluginFile()
for filetype in split(&ft, '\V.')
exe printf(
\"norm! :tabe $HOME/.dotfiles/vim/ftplugin/%s.vim\<cr>",
\l:filetype)
endfor
endfunc
func! OpenTemplateFile()
for filetype in split(&ft, '\V.')
let filepath = glob(
\printf("$HOME/.dotfiles/vim/templates/%s_%s.tmp", &ft,
\expand("%:e")))
if empty(l:filepath)
let filepath = glob(
\printf("$HOME/.dotfiles/vim/templates/%s.tmp", &ft))
endif
exe printf("norm! :tabe %s\<cr>", l:filepath)
endfor
endfunc
func! OpenUltiSnipsFile()
for filetype in split(&ft, '\V.')
exe printf(
\"norm! :tabe $HOME/.dotfiles/vim/ultisnips/" .
\"%s.snippets\<cr>", l:filetype)
endfor
endfunc
func! VisualIndent()
" Indent a block of text in visual mode, then restore the visual
" selection shifted right by the indentation width.
let start_line = line("'<")
let end_line = line("'>")
for line in range(start_line, end_line)
silent! exec "normal! " . line . "gg>>"
endfor
exec "normal! l" . start_line . "gg\<c-v>" . end_line . "gg"
endfunc
func! VisualDeindent()
" Deindent a block of text in visual mode, then restore the visual
" selection shifted left by the indentation width.
let start_line = line("'<")
let end_line = line("'>")
if col("'<") > 1
for line in range(start_line, end_line)
silent! exec "normal! " . line . "gg<<"
endfor
exec "normal! h"
endif
exec "normal! " . start_line . "gg\<c-v>" . end_line . "gg"
endfunc
func! Tab_Or_Complete()
" If the preceding letter is a keyword-character, trigger
" autocompletion; otherwise, insert the output of `SmartTab()`.
func! SmartTab(colPos)
" Insert either a soft or hard tab, depending on the contents of the
" line preceding the cursor position.
"
" Args:
" colPos : (int) Output of `col(".")`.
"
" Return:
" (str) If any preceding characters are only hard-tabs, insert a
" hard tab otherwise, insert a soft tab.
let currLn = getline(".")
if a:colPos == 1 || currLn[:a:colPos - 2] =~ "^[\t]*$"
return "\<tab>"
else
return repeat(" ", &tabstop - (virtcol(".") - 1) % &tabstop)
endif
endfunc
let colPos = col('.')
if colPos > 1 && strpart(getline('.'), colPos - 2, 3) =~ '^\k'
return "\<c-n>"
else
return SmartTab(colPos)
endif
endfunc
func! SmartBackspace(colPos, virtColPos)
" If the cursor position is preceded by multiple spaces, delete all
" spaces until the last tab-stop column.
"
" Args:
" colPos : (int) Output of `col(".")`.
" virtColPos : (int) Output of `virtcol(".")`.
let distFromStart = (a:virtColPos - 1) % &tabstop
if distFromStart == 0
let distFromStart = &tabstop
endif
let virtColPos = a:virtColPos - distFromStart
let startRealIndent = a:colPos - distFromStart
if startRealIndent >= 1 &&
\ getline('.')[startRealIndent - 1: a:colPos - 2] =~ "^[ ]*$"
return repeat("\<bs>", (a:colPos - startRealIndent))
else
return "\<bs>"
endif
endfunc
func! SmartPaste()
" Toggles `paste` when pasting from the system clipboard.
set pastetoggle=<esc>[201~
set paste
return ""
endfunc
func! TmuxOrSplitSwitch(wincmd, tmuxdir)
" Facilitates seamless navigation across tmux/vim splits with a single
" set of keymaps.
let previous_winnr = winnr()
silent! execute "wincmd " . a:wincmd
if previous_winnr == winnr()
call system("tmux select-pane -" . a:tmuxdir)
redraw!
endif
endfunc
func! HardRetab(tab_type)
" Retab whitespace at the beginning of all lines.
"
" Args:
" tab_type : (str) If "soft", convert all soft-tabs to hard-tabs.
" If "hard", convert all hard-tabs into soft-tabs.
let start_cur_pos = getcurpos()
let soft_tab = repeat(" ", &tabstop)
if a:tab_type == "soft"
let soft_tab_regex = '\(^\(' . soft_tab . '\)*\)\@<=' .
\ soft_tab
silent! exec "normal! :%s/" . soft_tab_regex . "/\t/g\<cr>"
elseif a:tab_type == "hard"
let hard_tab_regex = "\\(^[\t]*\\)\\@<=\t"
silent! exec "normal! :%s/" . hard_tab_regex . "/" . soft_tab .
\ "/g\<cr>"
endif
call setpos(".", start_cur_pos)
endfunc
func! EscapeAbbreviation()
" Enter a space and suppress any abbreviation expansion.
exe printf("norm! x%s ", (col(".") == len(getline("."))?"a":"i"))
endfunc
func! GetScriptNumber(script_name)
" Return the <SNR> of a script.
"
" Args:
" script_name : (str) The name of a sourced script.
"
" Return:
" (int) The <SNR> of the script; if the script isn't found, -1.
redir => scriptnames
silent! scriptnames
redir END
for script in split(l:scriptnames, "\n")
if l:script =~ a:script_name
return str2nr(split(l:script, ":")[0])
endif
endfor
return -1
endfunc
func! s:LoadTemplate()
" Load a template for a new file.
"
" Read a template for a new file, if one exists, and perform flag
" substitution. Accounts for dotted filetypes (prioritizing templates
" by order of filetype).
func! s:ReadFiletypeTemplate(filetype)
" If a template for the current file exists, read it into the
" buffer.
"
" Template filenames can take the following format, in order of
" descending priority.
"
" 1. "%s_%s" % (filetype, file-extension)
" 2. "%s" % (filetype)
"
" Args:
" filetype (str): The filetype to load a template for.
"
" Returns:
" int: 1 if a file was found; 0 otherwise.
let templatesDir = "~/.dotfiles/vim/templates"
let templatePath = glob(printf(
\"%s/%s_%s.tmp", l:templatesDir, a:filetype,
\expand("%:e")))
if empty(l:templatePath)
let templatePath = glob(printf(
\"%s/%s.tmp", l:templatesDir, a:filetype))
if empty(l:templatePath)
return 0
endif
endif
exe printf("read %s", l:templatePath)
0d
return 1
endfunc
func! s:SubstituteTemplateFlags()
" Perform flag substitution after a template file has been loaded.
let template_flags = {
\"__FILEBASE__" : expand("%:r")
\}
for flag in keys(l:template_flags)
exe printf(
\"%%s/%s/%s/g", l:flag,
\substitute(l:template_flags[l:flag], "/", '\\/', "g"))
endfor
exe "norm! /__START__\<cr>9x"
startinsert!
endfunc
for filetype in split(&ft, '\V.')
if s:ReadFiletypeTemplate(l:filetype)
call s:SubstituteTemplateFlags()
return
endif
endfor
endfunc
func! SynStack()
" Print the names of the syntax groups for the word under the cursor.
if exists("*synstack")
echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endif
endfunc
func! StatusLine()
" Statusline generator for the currently selected window split.
func! GitBranchName()
" Return the open file's Git work-tree's branch name.
"
" Return:
" (str) If the open file is inside a Git tree, return its current
" branch; otherwise, "".
let gitCommand = "cd " . expand("%:p:h") . " &&
\ git rev-parse --is-inside-work-tree > /dev/null 2>&1 &&
\ (git symbolic-ref --short HEAD 2> /dev/null ||
\ git rev-parse HEAD | cut -b-10)"
let branchName = system(gitCommand)
if len(branchName) > 0
return " " . branchName[:len(branchName) - 2] . " "
else
return ""
endif
endfunc
setl statusline=%1*\ %f\ " filename
setl statusline+=%4*%{&ff!='unix'?'-------NOTUNIX-------':''}
setl stl+=%2*%{&readonly?'\ ':''} " readonly
if !exists("b:gitBranchName")
let b:gitBranchName = GitBranchName()
endif
setl stl+=%3*%{b:gitBranchName} " branch name
" modified (note unicode space)
setl stl+=%4*%{&modified?' +\ ':''}
setl stl+=%5*%= " right justify
setl stl+=%6*\ %{strlen(&ft)?&ft:'none'}\ " filetype
setl stl+=%7*\ %p%%\ " percent of file
endfunc
func! StatusLineNC()
" Statusline generator for idle window splits.
setl statusline=%1*\ %f\ " filename
setl stl+=%4*%{&modified?'\ +\ ':''} " modified
setl stl+=%5*%= " right justify
endfunc
func! SkipPastSymbol()
" Move the cursor past the next `symbolRegex` without leaving insert-mode.
let symbolRegex = '[)\]}''"`*$]'
let currLn = getline(".")
if currLn[getcurpos()[2]:] =~ symbolRegex
exe "norm! /" . symbolRegex . "\<cr>"
endif
endfunc
let g:highlighted_words = []
let g:next_highlight_number = 0
func! HighlightCurrentWord()
let cword = expand("<cword>")
let syn_group_name = "_highlight_tmp_" . cword
let words_ind = index(g:highlighted_words, cword)
if words_ind == -1
exe "syn keyword " . syn_group_name . " " . cword . " containedin=ALL"
exe "hi " . syn_group_name . " ctermfg=" . g:next_highlight_number
let g:next_highlight_number = (g:next_highlight_number + 1) % 40
call add(g:highlighted_words, cword)
else
exe "syntax clear " . syn_group_name
call remove(g:highlighted_words, words_ind)
endif
endfunc