summaryrefslogtreecommitdiff
path: root/ar/.config/vim/init.vim
blob: c8bcf81263db37a23213462949ef9ca63d885e4a (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
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

" AUTOCMD ---------------------------------------------------------------- {{{

" Close with q
autocmd FileType checkhealth,help,lspinfo,neotest-output,neotest-output-panel,neotest-summary,netrw,notify,qf,query,spectre_panel,startuptime,terminal,tsplayground noremap <buffer> q :bd<CR>

" Disables automatic commenting on newline:
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

" Nerd tree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

" Runs a script that cleans out tex build files whenever I close out of a .tex file.
autocmd VimLeave *.tex !texclear %

" Text files
let g:vimwiki_ext2syntax = {'.Rmd': 'markdown', '.rmd': 'markdown','.md': 'markdown', '.markdown': 'markdown', '.mdown': 'markdown'}
let g:vimwiki_list = [{'path': '~/.local/share/nvim/vimwiki', 'syntax': 'markdown', 'ext': '.md'}]
autocmd BufRead,BufNewFile /tmp/calcurse*,~/.calcurse/notes/* set filetype=markdown
autocmd BufRead,BufNewFile *.ms,*.me,*.mom,*.man set filetype=groff
autocmd BufRead,BufNewFile *.tex set filetype=tex

" Enable Goyo by default for mutt writing
autocmd BufRead,BufNewFile /tmp/neomutt* :Goyo 80 | call feedkeys("jk")
autocmd BufRead,BufNewFile /tmp/neomutt* map ZZ :Goyo!\|x!<CR>
autocmd BufRead,BufNewFile /tmp/neomutt* map ZQ :Goyo!\|q!<CR>

" Automatically deletes all trailing whitespace and newlines at end of file on save. & reset cursor position
autocmd BufWritePre * let currPos = getpos(".")
autocmd BufWritePre * %s/\s\+$//e
autocmd BufWritePre * %s/\n\+\%$//e
autocmd BufWritePre *.[ch] %s/\%$/\r/e " add trailing newline for ANSI C standard
autocmd BufWritePre *neomutt* %s/^--$/-- /e " dash-dash-space signature delimiter in emails
autocmd BufWritePre * cal cursor(currPos[1], currPos[2])

" When shortcut files are updated, renew bash and ranger configs with new material:
autocmd BufWritePost bm-files,bm-dirs !shortcuts

" Run xrdb whenever Xdefaults or Xresources are updated.
autocmd BufRead,BufNewFile Xresources,Xdefaults,xresources,xdefaults set filetype=xdefaults
autocmd BufWritePost Xresources,Xdefaults,xresources,xdefaults !xrdb %

" Recompile dwmblocks on config edit.
autocmd BufWritePost ${XDG_SOURCES_HOME:-$HOME/.local/src}/suckless/dwmblocks/config.h !cd ${XDG_SOURCES_HOME:-$HOME/.local/src}/suckless/dwmblocks/; sudo make install && { killall -q dwmblocks;setsid -f dwmblocks }

" Which key description
autocmd! User vim-which-key call which_key#register('<Space>', 'g:which_key_map')
let g:which_key_map = {}

" }}}


" BACKUP ----------------------------------------------------------------- {{{

if version >= 703
    set undodir=${XDG_CONFIG_HOME:-$HOME/.config}/vim/undodir
    set undofile
    set undoreload=10000
endif

" }}}


" PLUGINS INIT ----------------------------------------------------------- {{{

let config_path = empty($XDG_CONFIG_HOME) ? expand("$HOME/.config") : expand("$XDG_CONFIG_HOME")
if filereadable(config_path . "/vim/plugins.vim")
    silent! call mkdir(config_path . "/vim/plugged", "p")
    execute "source " . config_path . "/vim/plugins.vim"
endif

" goyo
let g:is_goyo_active = v:false
function! GoyoEnter()
    if executable('tmux') && strlen($TMUX)
        silent !tmux set status off
        silent !tmux list-panes -F '\#F' | grep -q Z || tmux resize-pane -Z
    endif

    let g:default_colorscheme = exists('g:colors_name') ? g:colors_name : 'desert'
    set background=light
    set linebreak
    set wrap
    set textwidth=0
    set wrapmargin=0

    Goyo 80x85%
    colorscheme seoul256
    let g:is_goyo_active = v:true
endfunction

function! GoyoLeave()
    if executable('tmux') && strlen($TMUX)
        silent !tmux set status on
        silent !tmux list-panes -F '\#F' | grep -q Z && tmux resize-pane -Z
    endif

    Goyo!
    execute 'colorscheme ' . g:default_colorscheme
    let g:is_goyo_active = v:false
endfunction

function! ToggleGoyo()
    if g:is_goyo_active
        call GoyoLeave()
    else
        call GoyoEnter()
    endif
endfunction

" }}}


" PLUGIN MAPPINGS & SETTINGS -------------------------------------------------------- {{{

" Open quickfix/location list
let g:which_key_map.o       = {
            \ 'name' : '+Open'        ,
            \ 'q' : 'Quickfix-list'   ,
            \ 'l' : 'Location-list'   ,
            \ }

" Check health
nnoremap <Leader>ch :CheckHealth<CR>
let g:which_key_map.c       = { 'name' : 'Check' }
let g:which_key_map.c.h     = 'Check-health'

" Bookmarks
let g:bookmark_no_default_key_mappings = 1
let g:bookmark_save_per_working_dir = 1
let g:bookmark_auto_save = 1
nmap <Leader>mm <Plug>BookmarkToggle
nmap <Leader>mi <Plug>BookmarkAnnotate
nmap <Leader>ma <Plug>BookmarkShowAll
nmap <Leader>m] <Plug>BookmarkNext
nmap <Leader>m[ <Plug>BookmarkPrev
nmap <Leader>mc <Plug>BookmarkClear
nmap <Leader>mx <Plug>BookmarkClearAll
nmap <Leader>mk <Plug>BookmarkMoveUp
nmap <Leader>mj <Plug>BookmarkMoveDown
nmap <Leader>mg <Plug>BookmarkMoveToLine

" Fugitive
nnoremap <Leader>gs :Git<CR>
let g:which_key_map.g       = { 'name' : 'Git/Goyo' }
let g:which_key_map.g.s     = 'Git'

" Goyo plugin makes text more readable when writing prose:
nnoremap <Leader>gy :call ToggleGoyo()<CR>
let g:which_key_map.g.y     = 'Toggle-goyo'

" Nerd tree
map <Leader>n :NERDTreeToggle<CR>
let g:which_key_map.n       = 'Toggle-nerd-tree'

" Undotree
nnoremap <Leader>u :UndotreeToggle<CR>
let g:which_key_map.u       = 'Toggle-undo-tree'

" vimwiki
map <Leader>vw :VimwikiIndex<CR>
let g:which_key_map.v       = { 'name' : '+Vim-wiki' }
let g:which_key_map.v.w     = 'Vim-wiki-index'

" vim-plug
nnoremap <Leader>pc :PlugClean<CR>
nnoremap <Leader>pi :PlugInstall<CR>
nnoremap <Leader>pu :PlugUpdate<CR>
let g:which_key_map.p       =   { 'name' : '+Plug' }
let g:which_key_map.p.c     =   'Plug-clean'
let g:which_key_map.p.i     =   'Plug-install'
let g:which_key_map.p.u     =   'Plug-update'

" whichkey
nnoremap <silent> <Leader>      :<C-U>WhichKey '<Space>'<CR>
nnoremap <silent> <localleader> :<C-U>WhichKey '\'<CR>

" lsp
if executable('pylsp')
    " pip install python-lsp-server
    au User lsp_setup call lsp#register_server({
                \ 'name': 'pylsp',
                \ 'cmd': {server_info->['pylsp']},
                \ 'allowlist': ['python'],
                \ })
endif

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    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> gt <plug>(lsp-type-definition)
    nmap <buffer> <Leader>lr <plug>(lsp-rename)
    nmap <buffer> [t <plug>(lsp-previous-diagnostic)
    nmap <buffer> ]t <plug>(lsp-next-diagnostic)
    nmap <buffer> K <plug>(lsp-hover)
    " nnoremap <buffer> <expr><C-D> lsp#scroll(+4)
    " nnoremap <buffer> <expr><C-U> lsp#scroll(-4)

    let g:lsp_format_sync_timeout = 1000
    autocmd! BufWritePre *.rs,*.go,*.py call execute('LspDocumentFormatSync')

    " refer to doc to add more commands
endfunction

let g:which_key_map.g = {
            \ 'name' : '+Goto'          ,
            \ 'd' : 'Definition'        ,
            \ 's' : 'Symbol'            ,
            \ 'S' : 'Workspace-symbol'  ,
            \ 'r' : 'References'        ,
            \ 'i' : 'Implementation'    ,
            \ 't' : 'Type-definition'   ,
            \ }

let g:which_key_map['['] = { 'name' : '+Previous' }
let g:which_key_map[']'] = { 'name' : '+Next' }
let g:which_key_map['[t'] = 'Diagnostic'
let g:which_key_map[']t'] = 'Diagnostic'
let g:which_key_map.K = 'Keyword'

augroup lsp_install
    au!
    " call s:on_lsp_buffer_enabled only for languages that has the server registered.
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END

let g:lsp_fold_enabled = 0
let g:lsp_log_verbose = 1
let g:lsp_log_file = expand('~/.cache/vim/vim-lsp.log')
let g:asyncomplete_log_file = expand('~/.cache/vim/asyncomplete.log')
let g:lsp_settings_filetype_python = ['pyright-langserver', 'ruff', 'ruff-lsp']

nnoremap <Leader>li :LspInstallServer<CR>

" vim-airline
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif
let g:airline_symbols.colnr = ' C:'
let g:airline_symbols.linenr = ' L:'
let g:airline_symbols.maxlinenr = ' '
let g:airline#extensions#whitespace#symbol = '!'

" colorscheme
if isdirectory(expand("${XDG_CONFIG_HOME:-$HOME/.config}/vim/plugged/catppuccin"))
    let g:airline_theme = 'catppuccin_mocha'
    colorscheme catppuccin_mocha
endif

" fzf
let g:fzf_vim = {}
let $FZF_DEFAULT_OPTS = "--layout=default --preview-window 'right:60%' --preview 'bat --style=numbers --line-range :300 {}'
              \ --bind ctrl-y:preview-up,
              \        ctrl-e:preview-down,
              \        ctrl-b:preview-page-up,
              \        ctrl-f:preview-page-down,
              \        ctrl-u:preview-half-page-up,
              \        ctrl-d:preview-half-page-down,
              \        shift-up:preview-top,
              \        shift-down:preview-bottom,
              \        alt-up:half-page-up,
              \        alt-down:half-page-down
              \ "

" tmux
if exists('$TMUX')
    let g:fzf_layout = { 'tmux': '90%,70%' }
    let  g:tmux_navigator_no_wrap = 1
else
    let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }
endif
let g:fzf_vim.preview_window = ['right,50%,<70(up,40%)', 'ctrl-/']
let g:fzf_vim.commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
let g:fzf_vim.tags_command = 'ctags -R'

function! s:build_quickfix_list(lines)
    call setqflist(map(copy(a:lines), '{ "filename": v:val, "lnum": 1 }'))
    copen
    cc
endfunction

let g:fzf_action = {
            \ 'ctrl-q' : function('s:build_quickfix_list'),
            \ 'ctrl-t' : 'tab split' ,
            \ 'ctrl-x' : 'split'     ,
            \ 'ctrl-v' : 'vsplit'    ,
            \ }

nnoremap <Leader>cl :Colors<CR>
nnoremap <Leader>fb :Files ~/.local/bin<CR>
nnoremap <Leader>fc :Files ~/.config<CR>
nnoremap <Leader>fd :Files ~/.dotfiles<CR>
nnoremap <Leader>ff :Files .<CR>
nnoremap <Leader>fF :Files ~<CR>
nnoremap <Leader>fg :GFiles<CR>
nnoremap <Leader>fG :GFiles?<CR>
nnoremap <Leader>fs :Files ~/.local/src/suckless<CR>
nnoremap <Leader>fv :Files ~/.config/vim<CR>
nnoremap <Leader>sb :Buffers<CR>
nnoremap <Leader>sc :Changes<CR>
nnoremap <Leader>sC :Commands<CR>
nnoremap <Leader>sg :Rg<CR>
nnoremap <Leader>sG :RG<CR>
nnoremap <Leader>shc :History:<CR>
nnoremap <Leader>shh :History<CR>
nnoremap <Leader>shp :Helptags<CR>
nnoremap <Leader>shs :History/<CR>
nnoremap <Leader>sj :Jumps<CR>
nnoremap <Leader>sk :Maps<CR>
nnoremap <Leader>sl :Locate<CR>
nnoremap <Leader>sm :Marks<CR>
nnoremap <Leader>sn :Snippets<CR>
nnoremap <Leader>st :Filetypes<CR>
nnoremap <Leader>gc :Commits<CR>
nnoremap <Leader>gC :BCommits<CR>

let g:which_key_map.c = 'Color-schemes'
let g:which_key_map.f = {
            \ 'name' : '+Find'          ,
            \ 'b' : 'Scripts'           ,
            \ 'c' : 'Config'            ,
            \ 'd' : 'Dotfiles'          ,
            \ 'f' : 'Files'             ,
            \ 'F' : 'Root-files'        ,
            \ 'g' : 'Git-files'         ,
            \ 'G' : 'Git-status'        ,
            \ 's' : 'Suckless'          ,
            \ 'v' : 'Vim-config'        ,
            \ }

let g:which_key_map.g = {
            \ 'name' : '+Git'           ,
            \ 'c' : 'Commits'           ,
            \ 'C' : 'Buffer-commits'    ,
            \ }

let g:which_key_map.s = {
            \ 'name' : '+Search'        ,
            \ 'b' : 'Buffers'           ,
            \ 'c' : 'Changes'           ,
            \ 'C' : 'Commands'          ,
            \ 'g' : 'Rip-grep'          ,
            \ 'G' : 'Rip-Grep'          ,
            \ 'h' : {
                \  'name' : '+History'      ,
                \ 'c' : 'Command-history'   ,
                \ 'h' : 'History'           ,
                \ 'p' : 'Help-tags'         ,
                \ 's' : 'Search-history'    ,
                \ },
            \ 'j' : 'Jumps'             ,
            \ 'k' : 'Key-maps'          ,
            \ 'l' : 'Locate'            ,
            \ 'm' : 'Marks'             ,
            \ 'n' : 'Snippets'          ,
            \ 't' : 'File-types'        ,
            \ }


" snippets
let g:SuperTabDefaultCompletionType    = '<C-N>'
let g:SuperTabCrMapping                = 0
let g:UltiSnipsExpandTrigger           = '<C-E>'
let g:UltiSnipsJumpForwardTrigger      = '<tab>'
let g:UltiSnipsJumpBackwardTrigger     = '<s-tab>'
let g:UltiSnipsEditSplit               = 'vertical'
let g:UltiSnipsAutoTrigger             = 1
let g:asyncomplete_auto_completeopt    = 0
let g:asyncomplete_auto_popup          = 1

set completeopt=menuone,noinsert,noselect,preview
autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif

if has('python3')
    call asyncomplete#register_source(asyncomplete#sources#ultisnips#get_source_options({
                \ 'name': 'ultisnips',
                \ 'allowlist': ['*'],
                \ 'completor': function('asyncomplete#sources#ultisnips#completor'),
                \ }))
endif

inoremap <expr> <Tab>   pumvisible() ? "\<C-N>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-P>" : "\<S-Tab>"
inoremap <expr> <CR>    pumvisible() ? asyncomplete#close_popup() : "\<CR>"

" whichkey
set timeoutlen=500

let g:which_key_map.a       =   'Select-all-the-text'
let g:which_key_map.b       =   { 'name' : '+Buffer' }
let g:which_key_map.b.n     =   'New/open-buffer'
let g:which_key_map.c       =   { 'name' : '+Format' }
let g:which_key_map.c.f     =   'Format-buffer'
let g:which_key_map.e       =   'Explorer'
let g:which_key_map.h       =   { 'name' : '+Hex' }
let g:which_key_map.h.x     =   'Toggle-hex/reverse-conversion'
let g:which_key_map.l       =   { 'name' : '+Lex/Lsp' }
let g:which_key_map.l.e     =   'Open-lex'
let g:which_key_map.l.i     =   'Lsp-install-server'
let g:which_key_map.l.r     =   'Rename'
let g:which_key_map.o       =   { 'name' : '+Open' }
let g:which_key_map.o.g     =   'Orthography'
let g:which_key_map.Q       =   'Force-quit-all'
let g:which_key_map.r       =   { 'name' : '+Replace' }
let g:which_key_map.r.w     =   'Replace word'
let g:which_key_map.s       =   { 'name' : '+Surround' }
let g:which_key_map.s.o     =   'Source-file'
let g:which_key_map.s.w     =   'Surround-word'
let g:which_key_map.t       =   'Go-to-tab'
let g:which_key_map["'"]    =   'Register'
let g:which_key_map['w']    =   {
            \ 'name' : '+windows' ,
            \ 'd' : ['<C-W>c'     , 'Delete-window']         ,
            \ 'h' : ['<C-W>h'     , 'Window-left']           ,
            \ 'H' : ['<C-W>5<'    , 'Expand-window-left']    ,
            \ 'j' : ['<C-W>j'     , 'Window-below']          ,
            \ 'J' : [':resize +5' , 'Expand-window-below']   ,
            \ 'k' : ['<C-W>k'     , 'Window-up']             ,
            \ 'K' : [':resize -5' , 'Expand-window-up']      ,
            \ 'l' : ['<C-W>l'     , 'Window-right']          ,
            \ 'L' : ['<C-W>5>'    , 'Expand-window-right']   ,
            \ 's' : ['<C-W>s'     , 'Split-window-below']    ,
            \ 'v' : ['<C-W>v'     , 'Split-window-below']    ,
            \ 'w' : ['<C-W>w'     , 'Other-window']          ,
            \ '2' : ['<C-W>v'     , 'Layout-double-columns'] ,
            \ '-' : ['<C-W>s'     , 'Split-window-below']    ,
            \ '|' : ['<C-W>v'     , 'Split-window-right']    ,
            \ '=' : ['<C-W>='     , 'Balance-window']        ,
            \ '?' : ['Windows'    , 'Fzf-window']            ,
            \ }

" }}}


" SHORTCUTS ---------------------------------------------------------------- {{{

if filereadable(expand("${XDG_CONFIG_HOME:-$HOME/.config}/nvim/shortcuts.vim"))
    silent! source ${XDG_CONFIG_HOME:-$HOME/.config}/nvim/shortcuts.vim
endif

" }}}