comparison dot_config/nvim/init.vim @ 273:e6c22e1383c6

Add jinja syntax
author zegervdv <zegervdv@me.com>
date Mon, 27 Jul 2020 17:43:15 +0200
parents
children b77b75c88ce6
comparison
equal deleted inserted replaced
272:2789ed06da9d 273:e6c22e1383c6
1 " vim:fdm=marker:ts=2:sw=2
2
3 let s:darwin = has('mac')
4 let s:windows = has('win32')
5
6 " Include Vim-Plug {{{
7 if !s:windows
8 if has('nvim')
9 if empty(glob('~/.config/nvim/autoload/plug.vim'))
10 silent !mkdir -p ~/.config/nvim/autoload
11 silent !curl -fLo ~/.config/nvim/autoload/plug.vim
12 \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
13 autocmd VimEnter * PlugInstall
14 endif
15 else
16 if empty(glob('~/.vim/autoload/plug.vim'))
17 silent !mkdir -p ~/.vim/autoload
18 silent !curl -fLo ~/.vim/autoload/plug.vim
19 \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
20 autocmd VimEnter * PlugInstall
21 endif
22 endif
23 endif
24
25 " }}}
26 " Vim-Plug {{{
27 let g:plug_window='topleft new'
28
29 if has('nvim')
30 call plug#begin('~/.config/nvim/plugged')
31 else
32 call plug#begin('~/.vim/plugged')
33 endif
34
35 " General Plugins
36 if !has("nvim")
37 Plug 'tpope/vim-sensible'
38 endif
39
40 if s:darwin
41 Plug 'vim-scripts/gitignore'
42 endif
43
44 Plug 'sjl/splice.vim'
45
46 Plug 'tpope/vim-repeat'
47 Plug 'tpope/vim-rsi'
48 Plug 'einfachtoll/didyoumean'
49
50 " Session Management
51 Plug 'tpope/vim-obsession'
52
53 " Spelling
54 Plug 'tpope/vim-abolish'
55
56 " Settings
57 Plug 'sgur/vim-editorconfig'
58
59 " Brackets
60 Plug 'tpope/vim-surround'
61 Plug 'raimondi/delimitMate'
62 Plug 'tommcdo/vim-exchange'
63
64 " Formatting
65 Plug 'junegunn/vim-easy-align', { 'on' : [] }
66 augroup load_easy_align
67 autocmd!
68 autocmd CursorHold,CursorHoldI * call plug#load('vim-easy-align') | autocmd! load_easy_align
69 augroup END
70
71 " Comments
72 Plug 'tpope/vim-commentary'
73
74 " Moving in files
75 Plug 'wellle/targets.vim', { 'on': [] }
76 augroup load_targets
77 autocmd!
78 autocmd InsertEnter * call plug#load('targets.vim') | autocmd! load_targets
79 augroup END
80 Plug 'mhinz/vim-grepper', { 'on' : 'Grepper' }
81 Plug 'wellle/context.vim'
82
83 " Indentation
84 Plug 'nathanaelkane/vim-indent-guides'
85
86 " Command line
87 Plug 'tpope/vim-eunuch', { 'on' : ['Remove', 'Unlink', 'Move', 'Rename', 'Mkdir', 'Chmod', 'Find', 'Locate', 'SudoEdit', 'SudoWrite']}
88
89 " Undoing
90 Plug 'sjl/gundo.vim', { 'on': 'GundoToggle' }
91
92 " Pasting
93 " Plug 'junegunn/vim-peekaboo'
94
95 " Tabs
96 Plug 'gcmt/taboo.vim'
97
98 " Other
99 Plug 'yuttie/comfortable-motion.vim'
100 Plug 'tpope/vim-apathy'
101
102 " Tmux
103 Plug 'christoomey/vim-tmux-navigator'
104 Plug 'tmux-plugins/vim-tmux-focus-events'
105 " Plug 'roxma/vim-tmux-clipboard'
106
107 " Background make
108 " Plug 'skywind3000/asyncrun.vim'
109 Plug 'tpope/vim-dispatch'
110
111 " Completing and snippets
112 Plug 'sirver/ultisnips'
113 Plug 'honza/vim-snippets'
114 if has('nvim')
115 " Plug 'neoclide/coc.nvim', { 'branch': 'release' }
116 " Plug 'neoclide/coc-sources'
117 Plug 'neovim/nvim-lsp'
118 Plug 'haorenW1025/completion-nvim'
119 Plug 'haorenW1025/diagnostic-nvim'
120 endif
121
122 " Copying
123 Plug 'ShikChen/osc52.vim'
124
125 " Vim file navigation
126 " Plug 'tpope/vim-vinegar'
127 Plug 'justinmk/vim-dirvish'
128 Plug 'tpope/vim-projectionist'
129
130 " Theme
131 Plug 'NLKNguyen/papercolor-theme'
132 Plug 'joshdick/onedark.vim'
133
134 "Tcl
135 Plug 'vim-scripts/tcl.vim--smithfield-indent', { 'for': 'tcl'}
136
137 Plug 'zegervdv/ale', { 'branch': 'add_verible' }
138 Plug 'sbdchd/neoformat'
139
140 " Debugging
141 Plug 'sakhnik/nvim-gdb', { 'do': ':!./install.sh \| UpdateRemotePlugins' }
142
143 Plug 'Glench/Vim-Jinja2-Syntax'
144
145 if s:darwin
146 " Ruby
147 Plug 'tpope/vim-rails', { 'for': 'ruby' }
148 Plug 'vim-ruby/vim-ruby', { 'for': 'ruby' }
149 Plug 'tpope/vim-bundler', { 'for': 'ruby' }
150 Plug 'tpope/vim-rake', { 'for': 'ruby' }
151 Plug 'slim-template/vim-slim', { 'for': 'ruby' }
152 Plug 'duwanis/tomdoc.vim', { 'for': 'ruby' }
153 Plug 'keith/rspec.vim', { 'for': 'rspec' }
154
155 " Markdown
156 Plug 'tpope/vim-markdown', { 'for': 'markdown' }
157
158 " Jade
159 Plug 'digitaltoad/vim-jade', { 'for': 'jade' }
160
161 " Latex
162 Plug 'lervag/vimtex', { 'for': 'tex' }
163 endif
164
165 " Coffeescript
166 Plug 'kchmck/vim-coffee-script', { 'for': 'coffee' }
167
168 " C
169 Plug 'NLKNguyen/c-syntax.vim', { 'for': 'c' }
170
171 " Python
172 Plug 'Vimjas/vim-python-pep8-indent', { 'for': 'python' }
173 Plug 'vim-python/python-syntax', { 'for': 'python' }
174
175 " Git
176 Plug 'tpope/vim-git'
177 call plug#end()
178 " }}}
179
180 " Activate built in plugins {{{
181 if !has('nvim')
182 if has('packages')
183 packadd! matchit
184 packadd! shellmenu
185 endif
186 endif
187 source $VIMRUNTIME/ftplugin/man.vim
188
189 if has('packages')
190 silent! packadd! log_file
191 silent! packadd! browsify
192 silent! packadd! snippets
193 silent! packadd! chipscoper
194 endif
195
196 if has("nvim")
197 let g:python3_host_prog=expand('$HOME/.config/virtualenvs/python3/bin/python')
198 let g:python_host_prog=expand('$HOME/.config/virtualenvs/python2/bin/python')
199 endif
200 " }}}
201
202 " General Settings and options {{{
203
204 " Backspace over everything, like normal
205 set backspace=2
206
207 " Buffers
208 set autoread " Read external changes
209 set hidden " Change between buffers without saving
210 set autowriteall " Write changes when losing focus
211
212 " Visuals
213 set number
214 set relativenumber
215 set ruler
216 syntax on
217 set title
218 set scrolloff=4 " Stay 4 lines from top/bottom
219 set showcmd
220
221 " Theme and style
222 " Override needs to be defined before calling colorscheme
223 function! IndentGuidesColor() abort
224 highlight IndentGuidesOdd ctermbg=254
225 highlight IndentGuidesEven ctermbg=254
226 endfunction
227
228 augroup IndentColors
229 autocmd!
230 autocmd ColorScheme * call IndentGuidesColor()
231 augroup END
232
233 " function! ColorOverrides() abort
234 " highlight Special ctermfg=31
235 " endfunction
236
237 " augroup ColorOverrides
238 " autocmd!
239 " autocmd ColorScheme * call ColorOverrides()
240 " augroup END
241
242
243 if !has("nvim")
244 set t_Co=256
245 end
246 let g:PaperColor_Theme_Options = {
247 \ 'theme': {
248 \ 'default': {
249 \ 'allow_bold': 0
250 \ }
251 \ }
252 \}
253 let g:onedark_color_overrides = {
254 \ "yellow": {"cterm": "3", "gui": "#ffc24b"}
255 \}
256 set background=dark
257 colorscheme onedark
258 if s:darwin
259 set background=dark
260 colorscheme tomorrow-night
261 set guifont=inconsolata:h13
262 elseif s:windows
263 set background=light
264 colorscheme PaperColor
265 set guifont=consolas:h10
266 endif
267
268 if has('nvim')
269 set notermguicolors
270 endif
271
272 set showmatch " Highlight matching brackets
273
274 set wrap " Wrap lines
275 set wrapmargin=2 " Stay 2 chars from side
276 set textwidth=79
277 set colorcolumn=81
278 set linebreak " Smarter wrapping
279 if v:version > 703
280 set breakindent " Indent wrapped lines to same level
281 endif
282
283 set fixendofline
284
285 set expandtab " Expand tabs to spaces
286 if s:darwin
287 set tabstop=2 shiftwidth=2 " Tab is 2 spaces
288 else
289 set tabstop=3 shiftwidth=3
290 endif
291
292 set shiftround " Round shift indents to nearest value
293
294 " Searching
295 set magic " Use magic regexes
296 set hlsearch " Highlight all matches
297 set incsearch " Show matches while typing
298 set ignorecase " Ignore case when searching
299 set smartcase " Be case sensitive if at least one uppercase char is used
300 set gdefault " Default substitute all matches on a line
301 if has('nvim')
302 set inccommand=nosplit
303 endif
304
305 set autoindent " Automatically indent
306 set cindent " Indent based on C syntax
307 set cinwords+=foreach
308
309 set updatetime=300
310
311 if s:darwin
312 set vb
313 endif
314 set noerrorbells " Don't beep
315
316 set guioptions-=r
317 set guioptions-=l
318 set guioptions-=R
319 set guioptions-=L
320 set guioptions-=a
321 set guioptions-=m
322 set guioptions-=T
323 set guioptions+=c
324
325 set history=100 " Set history 100
326 set wildmenu " Command completion
327 set wildmode=longest:full,full " Complete to fullest match
328 set ttyfast " Use a fast terminal
329 set lazyredraw " No need to redraw constantly
330 set shortmess+=c
331
332 set ttimeoutlen=-1 " Set the timeout to a minimum
333 set diffopt+=iwhite " Ignore spaces in diffs"
334
335 set tags=.git/tags,tags,/project/asic_fpga/tools/vim/share/vim/vimfiles/tags
336
337 " Have find look through all folders
338 set path+=**
339 " Add PRJROOT and subdirs to path if it is set
340 if exists('$PRJROOT')
341 set path+=$PRJROOT/**
342 endif
343
344 set formatoptions=
345 set formatoptions+=c " Format comments
346 set formatoptions+=r " Continue comments by default
347 set formatoptions+=o " Make comment when using o or O from comment line
348 set formatoptions+=q " Format comments with gq
349 set formatoptions+=n " Recognize numbered lists
350 set formatoptions+=2 " Use indent from 2nd line of a paragraph
351 " set formatoptions+=l " Don't break lines that are already long
352 set formatoptions+=1 " Break before 1-letter words
353
354 set signcolumn=yes
355
356 " Enable cursorline
357 set cursorline
358 augroup cline
359 au!
360 autocmd WinEnter * setlocal cursorline
361 autocmd WinLeave * setlocal nocursorline
362 augroup END
363
364 if !s:windows
365 set list
366 set listchars=tab:»\ ,trail:·,extends:>,precedes:<,nbsp:+
367 endif
368
369 set sessionoptions-=options
370
371 " Scan files for completion
372 set complete=.,w,b,u,k,kspell,t,i,d
373 " set completeopt=menuone,longest,preview,noinsert,noselect
374 set completeopt=noinsert,menuone,noselect
375
376 set splitright
377 set virtualedit=block
378
379 if v:version > 703
380 set conceallevel=0
381 if !has('nvim')
382 set cryptmethod=blowfish2
383 endif
384 endif
385
386 filetype plugin indent on
387
388 set pastetoggle=<F2> " Toggle to paste mode
389
390 if v:version >= 703
391 set undofile
392 endif
393 set undolevels=1000
394 set backup
395 set noswapfile
396 if has('nvim')
397 " set viminfo+=n~/.config/nvim/.viminfo
398 set backupdir=~/.config/nvim/tmp/backup/,.
399 set directory=~/.config/nvim/tmp/swap//,.
400 else
401 set viminfo+=n~/.vim/.viminfo
402 set backupdir=~/.vim/tmp/backup/,.
403 set directory=~/.vim/tmp/swap//,.
404 endif
405 if !isdirectory(expand(&backupdir))
406 call mkdir(expand(&backupdir), "p")
407 endif
408 if !isdirectory(expand(&directory))
409 call mkdir(expand(&directory), "p")
410 endif
411
412 if v:version >= 703
413 if has('nvim')
414 set undodir=~/.config/nvim/tmp/undo//,.
415 else
416 set undodir=~/.vim/tmp/undo//,.
417 endif
418 if !isdirectory(expand(&undodir))
419 call mkdir(expand(&undodir), "p")
420 endif
421 endif
422
423
424 set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*.o,*.bin,*.elf,*.hex,*.eps,.git/**,*.dup,.hg/**,*.orig,*.*~
425
426 " No selecting via mouse (stops visual selection when scrolling)
427 set mouse=nic
428
429 " }}}
430 " Status line {{{
431 hi StatusLine ctermfg=0 ctermbg=3 cterm=NONE
432 hi StatusLineNC ctermfg=8 ctermbg=7 cterm=NONE
433 hi User1 ctermfg=0 ctermbg=7
434 hi User2 ctermfg=NONE ctermbg=NONE
435 hi User3 ctermfg=0 ctermbg=3
436 hi User4 ctermfg=0 ctermbg=5
437 function! Status()
438 let l:statusline=''
439 let l:statusline.=' %t'
440 let l:statusline.=' %1*'
441 let l:statusline.=' %Y'
442 let l:statusline.=' %2*'
443 let l:statusline.='%='
444 let l:statusline.=' %1*'
445 let l:statusline.=' line '
446 let l:statusline.=''
447 let l:statusline.='%l'
448 let l:statusline.=' %3*'
449 let l:statusline.=' of '
450 let l:statusline.=''
451 let l:statusline.='%L '
452 let l:statusline.=''
453 return l:statusline
454 endfunction
455 set laststatus=2
456 set statusline=%!Status()
457 " }}}
458 " Fix Colors in colorscheme {{{
459 function! SpellHighlights() abort
460 highlight SpellBad ctermbg=256 ctermfg=210
461 highlight SpellLocal ctermbg=240 ctermfg=010
462 highlight SpellCap ctermbg=256 ctermfg=211
463 endfunction
464 augroup SpellColors
465 autocmd!
466 autocmd ColorScheme * call SpellHighlights()
467 augroup END
468 "}}}
469
470 " Mappings {{{
471 " Set leader to spacebar
472 map <space> <leader>
473
474 " See long lines as line breaks
475 noremap <expr> j (v:count? 'j' : 'gj')
476 noremap <expr> k (v:count? 'k' : 'gk')
477
478 " Remap tag-search to better place
479 nnoremap <C-$> g<C-]>
480 nnoremap <C-y> g<C-]>
481 nnoremap <C-w>y <C-w>g<C-]>
482
483 nnoremap <C-s> <C-e>
484
485 " Move while in insert mode
486 inoremap <C-f> <right>
487
488 " Switch between the last two files
489 " nnoremap <space><space> <C-^>
490
491 " " Very Magic search patterns
492 nnoremap / /\v
493 vnoremap / /\v
494
495 " Keep search matches in the middle of the window.
496 nnoremap n nzzzv
497 nnoremap N Nzzzv
498
499 " Search for beginning of command
500 cnoremap <c-n> <down>
501 cnoremap <c-p> <up>
502
503 " Move whole lines
504 nnoremap [e :<c-u>execute 'move -1-'. v:count1<cr>
505 nnoremap ]e :<c-u>execute 'move +'. v:count1<cr>
506
507 " Insert empty lines
508 nnoremap [<space> :<c-u>put! =repeat(nr2char(10), v:count1)<cr>'[
509 nnoremap ]<space> :<c-u>put =repeat(nr2char(10), v:count1)<cr>
510
511 " Clear highlight
512 nnoremap <silent><leader>l :noh<CR>
513
514 inoremap £ \
515
516 " Highlight last inserted text
517 nnoremap gV `[v`]
518
519 " Highlight VCS conflict markers
520 match ErrorMsg '^\(<\|=\|>\)\{7\}\([^=].\+\)\?$'
521
522 " Use backspace as delete in visual mode
523 vmap <BS> x
524
525 " Redraw screen
526 nnoremap <F3> :redraw!<CR>
527
528 " Keep selection when shifting
529 vmap <expr> > KeepVisualSelection(">")
530 vmap <expr> < KeepVisualSelection("<")
531
532 function! KeepVisualSelection(cmd)
533 set nosmartindent
534 if mode() ==# "V"
535 return a:cmd . ":set smartindent\<CR>gv"
536 else
537 return a:cmd . ":set smartindent\<CR>"
538 endif
539 endfunction
540
541 " Swap backticks and quotes
542 nnoremap ` '
543 nnoremap ' `
544
545 " Open vimrc
546 nnoremap <leader>ve :e $MYVIMRC<CR>
547 nnoremap <leader>vs :so $MYVIMRC<CR>
548
549 " Open dup in diffmode
550 nnoremap <leader>d :call OpenDup(@%)<CR>
551
552 function! OpenDup(file)
553 let l:filename = a:file . ".dup"
554 execute 'vsplit' l:filename
555 windo diffthis
556 endfunction
557
558 " Do not move on *
559 nnoremap <silent> * :let stay_star_view = winsaveview()<cr>*:call winrestview(stay_star_view)<cr>
560
561 " Search for selected text, forwards or backwards.
562 vnoremap <silent> * :<C-U>
563 \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
564 \gvy/<C-R><C-R>=substitute(
565 \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
566 \gV:call setreg('"', old_reg, old_regtype)<CR>
567 vnoremap <silent> # :<C-U>
568 \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
569 \gvy?<C-R><C-R>=substitute(
570 \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
571 \gV:call setreg('"', old_reg, old_regtype)<CR>
572
573 " nnoremap <F10> :botright copen<CR>
574
575 xnoremap <silent> p p:if v:register == '"'<Bar>let @@=@0<Bar>endif<CR>
576
577 " Error navigation
578 nnoremap <UP> :cprev<CR>
579 nnoremap <DOWN> :cnext<CR>
580 nnoremap <RIGHT> :cnf<CR>
581 nnoremap <LEFT> :cpf<CR>
582
583 nnoremap <leader>y :ptjump <c-r><c-w><CR>
584
585 function! ToggleDiff()
586 if &diff == 1
587 windo diffoff
588 else
589 windo diffthis
590 endif
591 endfunction
592 nnoremap <leader>g :call ToggleDiff()<CR>
593
594 if has('nvim')
595 tnoremap <space><esc> <C-\><C-n>
596 tnoremap <C-h> <C-\><C-n><C-w>h
597 tnoremap <C-j> <C-\><C-n><C-w>j
598 tnoremap <C-k> <C-\><C-n><C-w>k
599 tnoremap <C-l> <C-\><C-n><C-w>l
600 augroup enter_term
601 au!
602 au BufEnter * if &buftype == 'terminal' | :startinsert | endif
603 augroup END
604 endif
605
606 " Open buffers, tags... in vertical splits
607 nnoremap <leader>b :vert sbuffer
608 nnoremap <leader>t :vert stjump
609 nnoremap <leader>f :vert sfind
610
611
612 " }}}
613
614 " Functions {{{
615 " When editing a file, always jump to the last known cursor position.
616 " Don't do it for commit messages, when the position is invalid, or when
617 " inside an event handler (happens when dropping a file on gvim).
618 autocmd BufReadPost *
619 \ if &ft != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") |
620 \ exe "normal g`\"" |
621 \ endif
622
623 " Thg mappings
624 function! s:HGhist(file)
625 if !empty(a:file)
626 let path = a:file
627 else
628 let path = expand('%')
629 endif
630 exec 'silent! !thg filelog ' . path . ' &'
631 endfunction
632
633 command! -nargs=? -complete=file HGhist call s:HGhist(<q-args>)
634
635 augroup focus_lost
636 au!
637 au FocusLost * if !&readonly | :wa | endif
638 augroup END
639
640 augroup LuaHighlight
641 autocmd!
642 autocmd TextYankPost * silent! lua require'vim.highlight'.on_yank()
643 augroup END
644
645 " Resize splits after window resize {{{
646 augroup vim_resize
647 au!
648 au VimResized * exe "normal! \<c-w>="
649 augroup END
650 " }}}
651
652 " Automatically reload vimrc when saving {{{
653 augroup reload_vim
654 autocmd!
655 autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
656 augroup END
657 " }}}
658
659 " Reload diffs {{{
660 augroup diff_files
661 au!
662 au BufWritePost * if &diff == 1 | diffupdate | endif
663 augroup END
664 " }}}
665
666 " Custom folding by Steve Losh
667 function! MyFoldText() " {{{
668 let line = getline(v:foldstart)
669
670 let nucolwidth = &fdc + &number * &numberwidth
671 let windowwidth = winwidth(0) - nucolwidth - 3
672 let foldedlinecount = v:foldend - v:foldstart
673
674 " expand tabs into spaces
675 let onetab = strpart(' ', 0, &tabstop)
676 let line = substitute(line, '\t', onetab, 'g')
677
678 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
679 let fillcharcount = windowwidth - len(line) - len(foldedlinecount)
680 return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' '
681 endfunction " }}}
682 set foldtext=MyFoldText()
683 set foldopen+=jump
684
685 " Detect Filetype from content if file has no extension {{{
686 augroup newFileDetection
687 au!
688 autocmd CursorMovedI * call CheckFileType()
689 augroup END
690
691 function! CheckFileType()
692 if exists("b:countCheck") == 0
693 let b:countCheck = 0
694 endif
695 let b:countCheck += 1
696 if &filetype == "" && b:countCheck > 20 && b:countCheck < 200
697 filetype detect
698 " Delete the function if no filetype can be established, or the type has
699 " been found
700 elseif b:countCheck >= 200 || &filetype != ""
701 autocmd! newFileDetection
702 endif
703 endfunction
704 " }}}
705
706 " Convert hex <-> decimal {{{
707 command! -nargs=? -range Dec2hex call s:Dec2hex(<line1>, <line2>, '<args>')
708 function! s:Dec2hex(line1, line2, arg) range
709 if empty(a:arg)
710 if histget(':', -1) =~# "^'<,'>" && visualmode() !=# 'V'
711 let cmd = 's/\%V\<\d\+\>/\=printf("0x%x",submatch(0)+0)/g'
712 else
713 let cmd = 's/\<\d\+\>/\=printf("0x%x",submatch(0)+0)/g'
714 endif
715 try
716 execute a:line1 . ',' . a:line2 . cmd
717 catch
718 echo 'Error: No decimal number found'
719 endtry
720 else
721 echo printf('%x', a:arg + 0)
722 endif
723 endfunction
724
725 command! -nargs=? -range Hex2dec call s:Hex2dec(<line1>, <line2>, '<args>')
726 function! s:Hex2dec(line1, line2, arg) range
727 if empty(a:arg)
728 if histget(':', -1) =~# "^'<,'>" && visualmode() !=# 'V'
729 let cmd = 's/\%V0x\x\+/\=submatch(0)+0/g'
730 else
731 let cmd = 's/0x\x\+/\=submatch(0)+0/g'
732 endif
733 try
734 execute a:line1 . ',' . a:line2 . cmd
735 catch
736 echo 'Error: No hex number starting "0x" found'
737 endtry
738 else
739 echo (a:arg =~? '^0x') ? a:arg + 0 : ('0x'.a:arg) + 0
740 endif
741 endfunction
742 " }}}
743
744 " Recognize filetype for dup files {{{
745 autocmd! BufRead *.dup call DupSetSyntax(@%)
746
747 function! DupSetSyntax(name)
748 let extension = matchlist(a:name, '\v.+.\.([^\.]+).dup')[1]
749 if extension == "vhd"
750 setlocal filetype=vhdl
751 elseif extension == "v"
752 setlocal filetype=verilog
753 elseif extension == "sv"
754 setlocal filetype=verilog
755 else
756 echo "Unknown filetype"
757 endif
758 endfunction
759 " }}}
760
761 " Create arguments list from files in quickfix list
762 " Allows to Grep for a pattern and apply an argdo command on each of the
763 " matching files {{{
764 command! -nargs=0 -bar Qargs execute 'args' QuickfixFilenames()
765 function! QuickfixFilenames()
766 " Building a hash ensures we get each buffer only once
767 let buffer_numbers = {}
768 for quickfix_item in getqflist()
769 let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr'])
770 endfor
771 return join(map(values(buffer_numbers), 'fnameescape(v:val)'))
772 endfunction
773 "}}}
774
775 augroup mark_files
776 au!
777 au BufLeave test.tcl mark T
778 au BufLeave case.do mark C
779 au BufLeave drv_*.tcl mark D
780 au BufLeave *.rtl.vhd mark R
781 augroup END
782
783
784 " Toggle between opening/closing the quickfix window {{{
785 function! GetBufferList()
786 redir =>buflist
787 silent! ls!
788 redir END
789 return buflist
790 endfunction
791
792 function! ToggleList(bufname, pfx)
793 let buflist = GetBufferList()
794 for bufnum in map(filter(split(buflist, '\n'), 'v:val =~ "'.a:bufname.'"'), 'str2nr(matchstr(v:val, "\\d\\+"))')
795 if bufwinnr(bufnum) != -1
796 exec(a:pfx.'close')
797 return
798 endif
799 endfor
800 if a:pfx == 'l' && len(getloclist(0)) == 0
801 echohl ErrorMsg
802 echo "Location List is Empty."
803 return
804 endif
805 let winnr = winnr()
806 exec('botright '.a:pfx.'open')
807 if winnr() != winnr
808 wincmd p
809 endif
810 endfunction
811
812 nnoremap <silent> <F10> :call ToggleList("Quickfix List", 'c')<CR>
813 " }}}
814
815
816 " Make list-like commands more intuitive {{{
817 " Copied from https://gist.github.com/romainl/047aca21e338df7ccf771f96858edb86
818 function! CCR()
819 let cmdline = getcmdline()
820 if cmdline =~ '\v\C^(ls|files|buffers)'
821 " like :ls but prompts for a buffer command
822 return "\<CR>:b"
823 elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$'
824 " like :g//# but prompts for a command
825 return "\<CR>:"
826 elseif cmdline =~ '\v\C^(dli|il)'
827 " like :dlist or :ilist but prompts for a count for :djump or :ijump
828 return "\<CR>:" . cmdline[0] . "j " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
829 elseif cmdline =~ '\v\C^(cli|lli)'
830 " like :clist or :llist but prompts for an error/location number
831 return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
832 elseif cmdline =~ '\C^old'
833 " like :oldfiles but prompts for an old file to edit
834 set nomore
835 return "\<CR>:sil se more|e #<"
836 elseif cmdline =~ '\C^changes'
837 " like :changes but prompts for a change to jump to
838 set nomore
839 return "\<CR>:sil se more|norm! g;\<S-Left>"
840 elseif cmdline =~ '\C^ju'
841 " like :jumps but prompts for a position to jump to
842 set nomore
843 return "\<CR>:sil se more|norm! \<C-o>\<S-Left>"
844 elseif cmdline =~ '\C^marks'
845 " like :marks but prompts for a mark to jump to
846 return "\<CR>:norm! `"
847 elseif cmdline =~ '\C^undol'
848 " like :undolist but prompts for a change to undo
849 return "\<CR>:u "
850 else
851 return "\<CR>"
852 endif
853 endfunction
854 cnoremap <expr> <CR> CCR()
855 " }}}
856
857 " Text objects {{{
858 function! s:inIndentation()
859 " select all text in current indentation level excluding any empty lines
860 " that precede or follow the current indentationt level;
861 "
862 " the current implementation is pretty fast, even for many lines since it
863 " uses "search()" with "\%v" to find the unindented levels
864 "
865 " NOTE: if the current level of indentation is 1 (ie in virtual column 1),
866 " then the entire buffer will be selected
867 "
868 " WARNING: python devs have been known to become addicted to this
869 " magic is needed for this
870 let l:magic = &magic
871 set magic
872
873 " move to beginning of line and get virtcol (current indentation level)
874 " BRAM: there is no searchpairvirtpos() ;)
875 normal! ^
876 let l:vCol = virtcol(getline('.') =~# '^\s*$' ? '$' : '.')
877
878 " pattern matching anything except empty lines and lines with recorded
879 " indentation level
880 let l:pat = '^\(\s*\%'.l:vCol.'v\|^$\)\@!'
881
882 " find first match (backwards & don't wrap or move cursor)
883 let l:start = search(l:pat, 'bWn') + 1
884
885 " next, find first match (forwards & don't wrap or move cursor)
886 let l:end = search(l:pat, 'Wn')
887
888 if (l:end !=# 0)
889 " if search succeeded, it went too far, so subtract 1
890 let l:end -= 1
891 endif
892
893 " go to start (this includes empty lines) and--importantly--column 0
894 execute 'normal! '.l:start.'G0'
895
896 " skip empty lines (unless already on one .. need to be in column 0)
897 call search('^[^\n\r]', 'Wc')
898
899 " go to end (this includes empty lines)
900 execute 'normal! Vo'.l:end.'G'
901
902 " skip backwards to last selected non-empty line
903 call search('^[^\n\r]', 'bWc')
904
905 " go to end-of-line 'cause why not
906 normal! $o
907
908 " restore magic
909 let &magic = l:magic
910 endfunction
911
912 " "in indentation" (indentation level sans any surrounding empty lines)
913 xnoremap <silent> ii :<c-u>call <sid>inIndentation()<cr>
914 onoremap <silent> ii :<c-u>call <sid>inIndentation()<cr>
915
916 function! s:aroundIndentation()
917 " select all text in the current indentation level including any emtpy
918 " lines that precede or follow the current indentation level;
919 "
920 " the current implementation is pretty fast, even for many lines since it
921 " uses "search()" with "\%v" to find the unindented levels
922 "
923 " NOTE: if the current level of indentation is 1 (ie in virtual column 1),
924 " then the entire buffer will be selected
925 "
926 " WARNING: python devs have been known to become addicted to this
927
928 " magic is needed for this (/\v doesn't seem work)
929 let l:magic = &magic
930 set magic
931
932 " move to beginning of line and get virtcol (current indentation level)
933 " BRAM: there is no searchpairvirtpos() ;)
934 normal! ^
935 let l:vCol = virtcol(getline('.') =~# '^\s*$' ? '$' : '.')
936
937 " pattern matching anything except empty lines and lines with recorded
938 " indentation level
939 let l:pat = '^\(\s*\%'.l:vCol.'v\|^$\)\@!'
940
941 " find first match (backwards & don't wrap or move cursor)
942 let l:start = search(l:pat, 'bWn') + 1
943
944 " NOTE: if l:start is 0, then search() failed; otherwise search() succeeded
945 " and l:start does not equal line('.')
946 " FORMER: l:start is 0; so, if we add 1 to l:start, then it will match
947 " everything from beginning of the buffer (if you don't like
948 " this, then you can modify the code) since this will be the
949 " equivalent of "norm! 1G" below
950 " LATTER: l:start is not 0 but is also not equal to line('.'); therefore,
951 " we want to add one to l:start since it will always match one
952 " line too high if search() succeeds
953
954 " next, find first match (forwards & don't wrap or move cursor)
955 let l:end = search(l:pat, 'Wn')
956
957 " NOTE: if l:end is 0, then search() failed; otherwise, if l:end is not
958 " equal to line('.'), then the search succeeded.
959 " FORMER: l:end is 0; we want this to match until the end-of-buffer if it
960 " fails to find a match for same reason as mentioned above;
961 " again, modify code if you do not like this); therefore, keep
962 " 0--see "NOTE:" below inside the if block comment
963 " LATTER: l:end is not 0, so the search() must have succeeded, which means
964 " that l:end will match a different line than line('.')
965
966 if (l:end !=# 0)
967 " if l:end is 0, then the search() failed; if we subtract 1, then it
968 " will effectively do "norm! -1G" which is definitely not what is
969 " desired for probably every circumstance; therefore, only subtract one
970 " if the search() succeeded since this means that it will match at least
971 " one line too far down
972 " NOTE: exec "norm! 0G" still goes to end-of-buffer just like "norm! G",
973 " so it's ok if l:end is kept as 0. As mentioned above, this means
974 " that it will match until end of buffer, but that is what I want
975 " anyway (change code if you don't want)
976 let l:end -= 1
977 endif
978
979 " finally, select from l:start to l:end
980 execute 'normal! '.l:start.'G0V'.l:end.'G$o'
981
982 " restore magic
983 let &magic = l:magic
984 endfunction
985
986 " "around indentation" (indentation level and any surrounding empty lines)
987 xnoremap <silent> ai :<c-u>call <sid>aroundIndentation()<cr>
988 onoremap <silent> ai :<c-u>call <sid>aroundIndentation()<cr>
989 " }}}
990 " }}}
991
992 " Filetype specific settings {{{
993 " Latex {{{
994 " Open pdf
995
996 function! Latexprog()
997 if !filereadable("./Makefile")
998 setlocal makeprg=latexmk\ -interaction=nonstopmode\ -synctex=1\ -file-line-error\ -pdf\ %:r
999 endif
1000 endfunction
1001
1002 augroup latex
1003 autocmd!
1004 autocmd FileType tex call Latexprog()
1005 au BufNewFile,BufRead,BufEnter *.tex setlocal spell spelllang=en_gb
1006 au BufNewFile,BufRead,BufEnter *.tex setlocal textwidth=0
1007 augroup END
1008 " }}}
1009 " Markdown {{{
1010 let g:vim_markdown_folding_disabled=1
1011 " }}}
1012 " Text {{{
1013 augroup ft_text
1014 au!
1015 " au BufNewFile,BufRead,BufEnter *.txt setlocal spell spelllang=en_gb
1016 au BufNewFile,BufRead,BufEnter *.txt setlocal textwidth=0
1017 augroup END
1018 augroup ft_report
1019 au!
1020 au BufNewFile,BufRead,BufEnter *.rpt setlocal nowrap
1021 au BufNewFile,BufRead,BufEnter *.rpt call ColorRpt()
1022 au BufNewFile,BufRead,BufEnter *.log call ColorRpt()
1023 augroup END
1024
1025 function! ColorRpt()
1026 " Color numbers based on length
1027 syn match String "\v<\d{1,3}>"
1028 syn match Number "\v<\d{4,6}>"
1029 syn match Statement "\v<\d{7,9}>"
1030
1031 " Color errors
1032 syn match Error "\v^ERROR:.*$"
1033 endfunction
1034 " }}}
1035 " Git commit messages {{{
1036 augroup ft_git
1037 au!
1038 au FileType gitcommit setlocal spell spelllang=en_gb
1039 augroup END
1040 " }}}
1041 " Ruby {{{
1042 augroup ft_ruby
1043 au!
1044 autocmd BufRead *_spec.rb set filetype=rspec
1045 augroup END
1046 " }}}
1047 " Matlab {{{
1048 augroup ft_matlab
1049 au!
1050 autocmd FileType matlab setlocal commentstring=\%\ %s
1051 augroup END
1052 " }}}
1053 " C {{{
1054 augroup ft_c
1055 au!
1056 au FileType c setlocal foldmethod=syntax
1057 augroup END
1058 " }}}
1059 " VHDL {{{
1060 " VHDL ctags
1061 let g:tlist_vhdl_settings = 'vhdl;d:package declarations;b:package bodies;e:entities;a:architecture specifications;t:type declarations;p:processes;f:functions;r:procedures'
1062 let g:vhdl_indent_genportmap =0
1063 let g:vhdl_indent_rhassign = 1
1064
1065 augroup ft_vhdl
1066 au!
1067 autocmd FileType vhdl call VHDLColonAlign()
1068 augroup END
1069
1070 function! SetAutoAlign()
1071 inoremap <silent> => =><ESC>mzvip:EasyAlign/=>/<CR>`z$a
1072 endfunction
1073
1074 function! VHDLChipScopeMacro()
1075 let @c = "mtyiw'Sosignal \"_cs : std_logic;'Coattribute mark_debug of \"_cs : signal is \"true\";
1076 attribute dont_touch of \"_cs : signal is \"true\";'Do\"_cs <= \";=='t"
1077 endfunction
1078
1079 function! VHDLColonAlign()
1080 let g:easy_align_delimiters = {
1081 \ ':': { 'pattern': ':', 'left_margin': 1, 'right_margin': 1, 'stick_to_left': 0}
1082 \}
1083 endfunction
1084 " }}}
1085 " TCL {{{
1086
1087 augroup ft_tcl
1088 au!
1089 autocmd FileType tcl setlocal commentstring=#\ %s
1090 " autocmd FileType tcl compiler nagelfar
1091 autocmd BufRead *.do set filetype=tcl
1092 autocmd BufRead *.hal set filetype=tcl
1093 autocmd FileType tcl setlocal iskeyword+=:
1094 autocmd FileType tcl setlocal breakat-=:
1095 autocmd FileType tcl setlocal suffixesadd+=.tcl,.do
1096 augroup END
1097 " shortcuts
1098 iabbrev procargs array set arg [::argument_processing::proces_arguments $args];
1099 " }}}
1100 " GPG {{{
1101 " Don't save backups of gpg asc files
1102 set backupskip+=*.asc
1103
1104 " Convenient editing of ascii-armoured encrypted files
1105 augroup GPGASCII
1106 au!
1107 au BufReadPost *.asc :%!gpg -q -d
1108 au BufReadPost *.asc |redraw
1109 au BufWritePre *.asc :%!gpg -q -e -a
1110 au BufWritePost *.asc u
1111 au VimLeave *.asc :!clear
1112 augroup END
1113 " }}}
1114 " System Verilog {{{
1115 augroup ft_systemverilog
1116 au!
1117 au FileType systemverilog setlocal suffixesadd+=.sv,.v
1118 au FileType systemverilog setlocal foldmethod=marker
1119 au FileType systemverilog,verilog call SVAlign()
1120 au FileType systemverilog,verilog let b:delimitMate_quotes = "\" '"
1121 augroup END
1122
1123 function! SVAlign()
1124 let g:easy_align_delimiters = {
1125 \ ')': { 'pattern': '[()]', 'left_margin': 0, 'right_margin': 0, 'stick_to_left': 0}
1126 \}
1127 endfunction
1128 " }}}
1129 " Make {{{
1130 augroup ft_make
1131 autocmd!
1132 autocmd BufEnter *.make setlocal filetype=make
1133 autocmd FileType make setlocal noexpandtab
1134 augroup END
1135 " JSON {{{
1136 augroup ft_json
1137 autocmd!
1138 autocmd FileType json setlocal equalprg=jq
1139 augroup END
1140 " }}}
1141 " }}}
1142 " Python {{{
1143 augroup f_python
1144 autocmd!
1145 autocmd FileType python setlocal shiftwidth=4
1146 au FileType python setlocal formatprg=autopep8\ -
1147 autocmd FileType python setlocal path-=**
1148 augroup END
1149 lua << EOF
1150 vim.lsp.set_log_level("debug")
1151 EOF
1152 lua << EOF
1153 require'nvim_lsp'.pyls.setup{
1154 cmd = {"pyls"},
1155 on_attach = require'on_attach'.on_attach
1156 }
1157 EOF
1158
1159 let g:python_highlight_all=1
1160 " }}}
1161 " }}}
1162
1163 " Plugin settings {{{
1164 " Easy-align {{{
1165 " Start interactive EasyAlign in visual mode (e.g. vip<Enter>)
1166 vmap <Enter> <Plug>(EasyAlign)
1167
1168 " Start interactive EasyAlign for a motion/text object (e.g. gaip)
1169 nmap ga <Plug>(EasyAlign)
1170 " }}}
1171 " Gundo tree {{{
1172 nnoremap <leader>u :GundoToggle<CR>
1173 " }}}
1174 " Projectionist {{{
1175 let g:projectionist_heuristics = {
1176 \ "*.c": {
1177 \ "*.c": {
1178 \ "alternate": "{}.h",
1179 \ "type": "source",
1180 \ "template": ["#include \"{}.h\""],
1181 \ "make": "make -wC {project}"
1182 \ },
1183 \ "*.h": {
1184 \ "alternate": "{}.c",
1185 \ "type": "header",
1186 \ "template": ["#ifndef {uppercase}_H", "#define {uppercase}_H", "", "#endif"]
1187 \ },
1188 \ "Makefile": {"type": "makefile"},
1189 \ },
1190 \ "*.py": {
1191 \ "*.py": { "make": "ipython {}" }
1192 \ },
1193 \ }
1194 " }}}
1195 " Grep {{{
1196 let g:grepper = {
1197 \ 'tools': ['ag', 'hg'],
1198 \ 'highlight': 1,
1199 \ 'ag': {
1200 \ 'grepprg': 'rg --vimgrep',
1201 \ }}
1202
1203 nnoremap gs <plug>(GrepperOperator)
1204 xnoremap gs <plug>(GrepperOperator)
1205
1206
1207 command! -nargs=* -complete=file Ag Grepper -noprompt -tool ag -grepprg rg --vimgrep <args>
1208 " }}}
1209 " Vinegar/NetRW {{{
1210 autocmd FileType netrw setl bufhidden=delete
1211 " }}}
1212 " NCM {{{
1213 function! s:check_back_space() abort
1214 let col = col('.') - 1
1215 return !col || getline('.')[col - 1] =~# '\s'
1216 endfunction
1217
1218 " inoremap <silent><expr> <TAB>
1219 " \ pumvisible() ? "\<C-n>" :
1220 " \ <SID>check_back_space() ? "\<TAB>" :
1221 " \ coc#refresh()
1222 " inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
1223 " " Use <cr> to confirm completion, `<C-g>u` means break undo chain at current position.
1224 " " Coc only does snippet and additional edit on confirm.
1225 " inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
1226 " " Or use `complete_info` if your vim support it, like:
1227 " " inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "\<C-y>" : "\<C-g>u\<CR>"
1228 " autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif
1229 " Use <Tab> and <S-Tab> to navigate through popup menu
1230 " inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
1231 inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
1232
1233 " Auto close popup menu when finish completion
1234 autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif
1235 inoremap <silent><expr> <TAB>
1236 \ pumvisible() ? "\<C-n>" :
1237 \ <SID>check_back_space() ? "\<TAB>" :
1238 \ completion#trigger_completion()
1239
1240 let g:completion_enable_snippet = 'UltiSnips'
1241 "Fallback for https://github.com/Raimondi/delimitMate expanding on enter
1242 let g:completion_confirm_key_rhs = "\<Plug>delimitMateCR"
1243 let g:UltiSnipsJumpForwardTrigger="<c-b>"
1244 let g:UltiSnipsJumpBackwardTrigger="<c-z>"
1245 " }}}
1246 " Background make {{{
1247 nnoremap <F9> :PMake<CR>
1248 " }}}
1249 " LanguageClient {{{
1250 nmap <silent> gd <Plug>(coc-definition)
1251 nmap <silent> gy <Plug>(coc-type-definition)
1252 nmap <silent> gi <Plug>(coc-implementation)
1253 nmap <silent> gr <Plug>(coc-references)
1254
1255 " Use K to show documentation in preview window
1256 nnoremap <silent> K :call <SID>show_documentation()<CR>
1257
1258 function! s:show_documentation()
1259 if (index(['vim','help'], &filetype) >= 0)
1260 execute 'h '.expand('<cword>')
1261 else
1262 call CocAction('doHover')
1263 endif
1264 endfunction
1265 " }}}
1266 " Splice {{{
1267 let g:splice_initial_diff_grid=1
1268 let g:splice_initial_diff_compare=1
1269 let g:splice_initial_diff_path=0
1270 let g:splice_initial_scrollbind_grid=1
1271 let g:splice_initial_scrollbind_compare=1
1272 let g:splice_initial_scrollbind_path=1
1273 let g:splice_wrap="nowrap"
1274 " }}}
1275 " Chipscoper {{{
1276 nnoremap <leader>cm :call ChipScoperMark()<CR>
1277 nnoremap <leader>ci :call ChipScoperInsert()<CR>
1278 nnoremap <leader>cd :call ChipScoperUnMark()<CR>
1279 augroup cs_vhdl
1280 autocmd!
1281 autocmd FileType vhdl packadd chipscoper
1282 autocmd FileType vhdl silent! call ChipScoperSetup()
1283 augroup END
1284 " }}}
1285 " GDB {{{
1286 let g:nvimgdb_disable_start_keymaps = 1
1287 " }}}
1288 " context {{{
1289 let g:context_enabled = 0
1290 " }}}
1291 " ALE {{{
1292 let g:ale_virtualtext_cursor=1
1293 augroup fmt
1294 autocmd!
1295 autocmd BufWritePre * try | undojoin | Neoformat | catch /^Vim\%((\a\+)\)\=:E790/ | finally | silent Neoformat | endtry
1296 augroup END
1297 " }}}
1298 " }}}
1299
1300 function! SendOSCClipboard(lines, regtype)
1301 call SendViaOSC52(join(a:lines, "\n"))
1302 endfunction
1303
1304 let g:clipboard = {
1305 \ 'name': 'TMUX',
1306 \ 'copy': {
1307 \ '+': function('SendOSCClipboard'),
1308 \ '*': 'tmux load-buffer -',
1309 \ },
1310 \ 'paste': {
1311 \ '+': 'tmux save-buffer -',
1312 \ '*': 'tmux save-buffer -',
1313 \ },
1314 \ 'cache_enabled': 1,
1315 \ }
1316
1317 " Load local vimrc
1318 if filereadable($HOME . '/.vimrc.local')
1319 source ~/.vimrc.local
1320 endif
1321
1322 " Load project local vimrc
1323 if filereadable('.vimrc.local')
1324 source .vimrc.local
1325 endif