summaryrefslogtreecommitdiff
path: root/.vimrc
blob: e5eabe5e56a6c2c56a159d98d095b764c23c3ac8 (plain)
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
" General vim defaults
" set background=
" set t_te= t_ti=
set ls=1 ruler
set ai ts=4 sw=4 noet
" Not a good idea to activate mouse, as it disables cut & paste in MacOS
"set mouse=a
syntax off
filetype on
filetype plugin on
filetype plugin indent on
let mapleader = ","

" Save all files at the same place
set backupdir=~/.vim/backup//
set directory=~/.vim/backup//
set undodir=~/.vim/backup//
set undofile
set backup

" set autochdir

" ignore case when searching, except mixing upper and lower
set ignorecase
set smartcase

" No bell
set belloff=all

" For MacVim
set guifont=6x13:h13

" fzf plugin
set rtp+=/opt/homebrew/opt/fzf
let g:fzf_preview = 'cat {}'

" set grepprg=rg\ --vimgrep\ --smart-case\ --hidden\ --follow

" nnoremap \ :Rg<CR>
" nnoremap <C-T> :Files<cr>
nnoremap <Leader>b :Buffers<cr>
nnoremap <Leader>s :Blines<cr>

" vimki plugin
autocmd filetype vimki syntax on
autocmd filetype vimki set autowrite
" autocmd filetype vimki setlocal spell spelllang=fr
autocmd filetype vimki nmap <leader>z :FZF ~/Wiki<CR>
" autocmd filetype markdown,text,vimki setlocal textwidth=72

" autocmd BufNewFile,BufRead *.md setlocal textwidth=0 wrapmargin=0 wrap linebreak

autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4

let g:netrw_browsex_viewer = "xdg-open"
let g:netrw_gx = "<cWORD>"
let g:netrw_banner = 0

" python and yaml formatting:
autocmd filetype python,yaml setlocal ts=2 sts=2 sw=2 et

:set wrap
:set linebreak
:set nolist
:set formatoptions-=t
" noremap <M-v> :!cscope -1 <cword>

" noremap <M-a> :!cscope -0 <cword>

" noremap <M-f> :!cscope -7 <cfile>


" Paragraph format / unformat
" An alternate way is to use "gqip" to format.
" noremap <leader>f !}fmt -72 -s -p
" noremap <leader>F !}fmt -c -w 2048
noremap <leader>f gqip

" noremap <buffer> K :s,^,//,
" noremap <buffer> <C-K> :s,^//,,
" noremap <buffer> # :s,^,#,
" noremap <buffer> @ :s,^#,,
"hi MatchParen term=reverse ctermbg=magenta ctermfg=none
" hi MatchParen cterm=reverse term=reverse ctermfg=white
"

" Fix spell highlighting
hi clear SpellBad
hi SpellBad cterm=underline
hi SpellBad gui=undercurl

noremap <leader>n :bn<cr>
noremap <leader><leader> :bd<cr>
noremap <leader>o :!open <cWORD>&<cr><cr>
noremap  <C-g> :tag <c-r><c-w><cr>
noremap <C-p> :Files<cr>

" MacOS: Open QuickLook on current file (for markdown preview mainly)
noremap <leader>w :!ql %<CR><CR>

function! CloseOnLast()
let cnt = 0
for i in range(0, bufnr("$"))
if buflisted(i)
		let cnt += 1
	endif
endfor
 if cnt <= 1
	q
else
	bd
endif
endfunction

noremap <leader><leader> :call CloseOnLast()<cr>

" vim-lsp settings
let g:lsp_diagnostics_enabled = 0
let g:lsp_document_code_action_signs_enabled = 0
let g:lsp_document_highlight_enabled = 0

if executable('gopls')
	au User lsp_setup call lsp#register_server({
        \ 'name': 'gopls',
        \ 'cmd': {server_info->['gopls', '-remote=auto']},
        \ 'allowlist': ['go', 'gomod', 'gohtmltmpl', 'gotexttmpl'],
        \ })
    autocmd BufWritePre *.go
        \ call execute('LspDocumentFormatSync') |
        \ call execute('LspCodeActionSync source.organizeImports')
endif

if executable('gnopls')
	au User lsp_setup call lsp#register_server({
        \ 'name': 'gnopls',
        \ 'cmd': {server_info->['gnopls', '-remote=auto']},
        \ 'allowlist': ['gno', 'gnomod'],
        \ })
    autocmd BufWritePre *.gno
        \ call execute('LspDocumentFormatSync') |
        \ call execute('LspCodeActionSync source.organizeImports')
endif

if executable('clangd')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'clangd',
        \ 'cmd': {server_info->['clangd', '-background-index']},
        \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp'],
        \ })
endif

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gs <plug>(lsp-document-symbol-search)
    nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> gi <plug>(lsp-implementation)
    nmap <buffer> gy <plug>(lsp-type-definition)
    nmap <buffer> <leader>rn <plug>(lsp-rename)
    nmap <buffer> [g <plug>(lsp-previous-diagnostic)
    nmap <buffer> ]g <plug>(lsp-next-diagnostic)
    nmap <buffer> K <plug>(lsp-hover)
endfunction

augroup lsp_install
    au!
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END

" Git blame
command! Blame normal!:let @a=expand('%')<CR>:let @b=line('.')<CR>:new<CR>:set bt=nofile<CR>:%!git blame -wM --date=short <C-R>a<CR>:<C-R>b<CR>