-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_vimrc
530 lines (432 loc) · 18.2 KB
/
dot_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
" Configuration file for vim
" Written for Debian GNU/Linux by W.Akkerman <wakkerma@debian.org>
" Some modifications by J.H.M. Dassen <jdassen@wi.LeidenUniv.nl>
" Modified by high / John Ericson.
" Functions
" Copy With Syntax Highlighting.
" https://vi.stackexchange.com/a/18979
"
" Run like this:
" :HtmlClip
function! CopyFormatted(line1, line2)
execute a:line1 . "," . a:line2 . "TOhtml"
%yank *
!start /min powershell -noprofile "gcb | scb -as"
bwipeout!
endfunction
command! -range=% HtmlClip silent call CopyFormatted(<line1>,<line2>)
" https://stackoverflow.com/a/57020870/2903596
function! IsWSL()
if has("unix")
let lines = readfile("/proc/version")
if lines[0] =~ "microsoft"
return 1
endif
endif
return 0
endfunction
" To make nvim work with my listchars with special characters. nvim requires .vimrc to be in utf-8 format.
set encoding=utf-8
" Set english language
"
" Help language
set helplang=en
" Vim language
" This doesn't work on my installation of Vim x64.
"language en
" Sets the language in the menubar of gvim to english instead of the default
" on the system. This must be done before loading menus, switching on filetype
" detection or syntax highlighting.
set langmenu=none
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible " Use Vim defaults (much better!)
" use Q for formatting, not ex-mode:
map Q gq
set backspace=2 " allow backspacing over everything in insert mode
" Now we set some defaults for the editor
set autoindent " always set autoindenting on
set textwidth=0 " Don't wrap words by default
" Good to have setuped to 75 when writing mail.
"set backup " keep a backup file
set nobackup " Don't keep a backup file
set viminfo='20,\"50 " read/write a .viminfo file, don't store more than
" 50 lines of registers
set history=50 " keep 50 lines of command line history
" Tabs
set ts=3 " number of spaces a <Tab> in the text stands for
set sw=3 " number of spaces used for each step of (auto)indent
"set sts=3 " if non-zero, number of spaces to insert for a <Tab>
set nosta sta " a <Tab> in an indent inserts 'shiftwidth' spaces
"set noet et " expand <Tab> to spaces in Insert mode
set nosi si " do clever autoindenting
set nocin cin " enable specific indenting for C code
if IsWSL() || exists("$SSH_CONNECTION")
set mouse=v " Use visual selection when not running locally, such as WSL/SSH.
else
set mouse=a " enable mouse
endif
" Suffixes that get lower priority when doing tab completion for filenames.
" These are files we are not likely to want to edit or read.
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
" Make vim consult the external termcap entries first, so we get all of
" Debian's termcap settings correct. This will be fixed upstream in 5.4
" This doesn't work in vim 6.4 in win32 / High
"set nottybuiltin term=$TERM
" We know xterm-debian is a color terminal
"if &term =~ "xterm-debian" || &term =~ "xterm-xfree86"
" set t_Co=16
" set t_Sf=[3%dm
" set t_Sb=[4%dm
"endif
" Vim5 comes with syntaxhighlighting.
" Do not enable when Neovim is used in VS Code.
if !exists('g:vscode')
if has("syntax")
syntax on " Default to no syntax highlightning
endif
endif
" Debian uses compressed helpfiles. We must inform vim that the main
" helpfiles is compressed. Other helpfiles are stated in the tags-file.
" This isn't needed on my windows install.
"set helpfile=$VIMRUNTIME/doc/help.txt.gz
if has("autocmd")
" Set some sensible defaults for editing C-files
augroup cprog
" Remove all cprog autocommands
au!
" When starting to edit a file:
" For *.c and *.h files set formatting of comments and set C-indenting on.
" For other files switch it off.
" Don't change the order, it's important that the line with * comes first.
autocmd BufRead * set formatoptions=tcql nocindent comments&
autocmd BufRead *.c,*.h set formatoptions=croql cindent comments=sr:/*,mb:*,el:*/,://
augroup END
" Also, support editing of gzip-compressed files. DO NOT REMOVE THIS!
" This is also used when loading the compressed helpfiles.
augroup gzip
" Remove all gzip autocommands
au!
" Enable editing of gzipped files
" read: set binary mode before reading the file
" uncompress text in buffer after reading
" write: compress file after writing
" append: uncompress file, append, compress file
autocmd BufReadPre,FileReadPre *.gz set bin
autocmd BufReadPre,FileReadPre *.gz let ch_save = &ch|set ch=2
autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
autocmd BufReadPost,FileReadPost *.gz set nobin
autocmd BufReadPost,FileReadPost *.gz let &ch = ch_save|unlet ch_save
autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
autocmd FileAppendPre *.gz !gunzip <afile>
autocmd FileAppendPre *.gz !mv <afile>:r <afile>
autocmd FileAppendPost *.gz !mv <afile> <afile>:r
autocmd FileAppendPost *.gz !gzip <afile>:r
augroup END
augroup bzip2
" Remove all bzip2 autocommands
au!
" Enable editing of bzipped files
" read: set binary mode before reading the file
" uncompress text in buffer after reading
" write: compress file after writing
" append: uncompress file, append, compress file
autocmd BufReadPre,FileReadPre *.bz2 set bin
autocmd BufReadPre,FileReadPre *.bz2 let ch_save = &ch|set ch=2
autocmd BufReadPost,FileReadPost *.bz2 |'[,']!bunzip2
autocmd BufReadPost,FileReadPost *.bz2 let &ch = ch_save|unlet ch_save
autocmd BufReadPost,FileReadPost *.bz2 execute ":doautocmd BufReadPost " . expand("%:r")
autocmd BufWritePost,FileWritePost *.bz2 !mv <afile> <afile>:r
autocmd BufWritePost,FileWritePost *.bz2 !bzip2 <afile>:r
autocmd FileAppendPre *.bz2 !bunzip2 <afile>
autocmd FileAppendPre *.bz2 !mv <afile>:r <afile>
autocmd FileAppendPost *.bz2 !mv <afile> <afile>:r
autocmd FileAppendPost *.bz2 !bzip2 -9 --repetitive-best <afile>:r
augroup END
endif " has ("autocmd")
" The following are commented out as they cause vim to behave a lot
" different from regular vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
set ruler " Show the line and column numbers of the cursor
"set ignorecase " Do case insensitive matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
set wrap " long lines wrap.
"set lbr " wrap long lines at a character in 'breakat' (local to window).
"set brk=\ \ " which characters might cause a line break.
let g:LargeFile=100
"colo default
set incsearch " Search while you type.
set nolist " Don't Show special characters like tab and newlines.
set listchars=tab:¬\ ,eol:¶
set colorcolumn=101 " Displays a line at 80 characters.
" Set color of colorcolumn to very dark blue. Overrides colorscheme.
" http://stackoverflow.com/questions/2440149/override-colorscheme/7383051#7383051
" autocmd ColorScheme * hi ColorColumn ctermbg=darkgrey guibg=#051021
"set noswapfile
" For swap files. Only store files here if the directories exists.
" If it doesn't exist Vim will say 'Unable to open cache file' when opening
" file.
" /Temp is for Windows.
set backupdir=$HOME/.tmp/vim/,/tmp/,/Temp//
set directory=$HOME/.tmp/vim/,/tmp/,/Temp/
set undodir=$HOME/.tmp/vim/,/tmp/,/Temp//
" Activate modeline.
set modeline
" Be able to set specific settings per file in ftdetect.
filetype plugin on
" Run PlugInstall if there are missing plugins.
" Based on: https://github.com/junegunn/vim-plug/wiki/tips#automatic-installation-of-missing-plugins
autocmd VimEnter * if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
\| PlugInstall --sync | source $MYVIMRC
\| endif
" Install plugins.
call plug#begin()
" Enable autocompletion, intellisense functionality.
" Recommended by https://www.youtube.com/watch?v=h4g0m0Iwmys and used by
" LazyVim (https://www.lazyvim.org/plugins/coding#nvim-cmp).
" Config based on: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
" For luasnip users.
Plug 'L3MON4D3/LuaSnip'
Plug 'saadparwaiz1/cmp_luasnip'
" Easily rename tabs.
Plug 'gcmt/taboo.vim'
Plug 'hashivim/vim-terraform'
" Plugin that prevents you from running neovim inside neovim by instead opening
" new files in the host Neovim.
Plug 'samjwill/nvim-unception'
" Enable multi-cursors. Works in VS Code as well.
Plug 'mg979/vim-visual-multi', {'branch': 'master'}
call plug#end()
" Mappings
" Auto copy the mouse selection
" https://vim.fandom.com/wiki/Auto_copy_the_mouse_selection
" Not pleased with how Method 1 works. Method 2 works a lot better.
" Method 1 (for Xwindows and mswindows), this map is different from the one shown in vim documentation:
" Only copies in Vim not, to System wide clipboard on Windows.
":noremap <LeftRelease> "+y<LeftRelease>
" Copies to System wide clipboard on Windows.
":noremap <LeftRelease> "*y<LeftRelease>
" Method 2 (works only on ms-windows vim63):
:set guioptions+=a
" Disable annoying beeping
set noerrorbells
set vb t_vb=
set bg=dark " background (light/dark). Reload syntax to initialize
" settings.
" Make vim use the OS clipboard by default.
set clipboard^=unnamed,unnamedplus
" Remove white background from tabline when several tabs are open.
hi TabLineFill cterm=underline
if has("win32")
" Use PowerShell Core as default shell on Windows.
set shell=pwsh
" This makes :!echo hello work.
lua vim.opt.shellcmdflag = '-NoLogo -Command'
lua vim.opt.shellxquote = ''
else
set shell=bash
endif
" Terminal settings
" Allows to use ESC to exit insert-mode in Terminal.
tnoremap <Esc> <C-\><C-n>
" Allows to use Ctrl+t ESC to exit insert-mode in Terminal. This is useful in
" nested Neovim (running Neovim inside Neovim). For example when you in Neovim
" ssh to another server and run Neovim there.
tnoremap <C-t><Esc> <Esc>
" Navigate between buffers with Ctrl+l and Ctrl+h in normal mode.
map <C-l> :bn!<CR> " Doesn't override Ctrl+l in terminal to reset screen.
map <C-h> :bp!<CR>
" Move tabs to left and right just like in Windows. Works in Neovim QT but not
" in Neovim in Windows Terminal because of terminal blocking.
nnoremap <C-S-PageUp> :tabmove -1<CR> " Move the current tab to the left
nnoremap <C-S-PageDown> :tabmove +1<CR> " Move the current tab to the right
" Neovim VSCode extension specific stuff.
if exists('g:vscode')
" Enable default VS Code Ctrl+k w keybinding to close all editors in a group.
nnoremap <C-k>w <Cmd>call VSCodeNotify('workbench.action.closeEditorsInGroup')<CR>
" Enable default VS Code Ctrl+k z keybinding to toggle Zen mode.
nnoremap <C-k>z <Cmd>call VSCodeNotify('workbench.action.toggleZenMode')<CR>
" Enable default VS Code Ctrl+k Ctrl+m keybinding to maximize editor group.
nnoremap <C-k> <C-m> <Cmd>call VSCodeNotify('workbench.action.toggleMaximizeEditorGroup')<CR>
" Enable my custom VS Code Ctrl+w m keybinding to toogle to maximize current editor.
nnoremap <C-w>m <Cmd>call VSCodeNotify('workbench.action.toggleEditorWidths')<CR>
" Add Vim fold keybindings to VS Code.
" Based on: https://github.com/vscode-neovim/vscode-neovim/issues/58#issuecomment-1229279216
nnoremap zM :call VSCodeNotify('editor.foldAll')<CR>
nnoremap zR :call VSCodeNotify('editor.unfoldAll')<CR>
nnoremap zc :call VSCodeNotify('editor.fold')<CR>
nnoremap zC :call VSCodeNotify('editor.foldRecursively')<CR>
nnoremap zo :call VSCodeNotify('editor.unfold')<CR>
nnoremap zO :call VSCodeNotify('editor.unfoldRecursively')<CR>
nnoremap za :call VSCodeNotify('editor.toggleFold')<CR>
" Fixes so you can navigate pass closed folds without VS Code opening them.
function! MoveCursor(direction) abort
if(reg_recording() == '' && reg_executing() == '')
return 'g'.a:direction
else
return a:direction
endif
endfunction
" Vim navigation keys.
nmap <expr> k MoveCursor('k')
nmap <expr> j MoveCursor('j')
" Cursor navigation keys.
nmap <expr> <Up> MoveCursor('k')
nmap <expr> <Down> MoveCursor('j')
" Tab navigation in the same way as buffer navigation in my normal Vim.
map <C-l> :call VSCodeNotify('workbench.action.nextEditor')<CR>
map <C-h> :call VSCodeNotify('workbench.action.previousEditor')<CR>
endif
" Neovim specific config.
if has('nvim')
" To make VS Code understand the terminal better.
" Still doesn't work well in VS Code for remote user running Live Share.
" Hide statusbar and cmdline when starting a Terminal.
augroup term
autocmd!
autocmd TermOpen * if winnr('$') == 1 | setlocal laststatus=0 | setlocal cmdheight=0 | endif
" Hide statusbar when a terminal is in focus.
autocmd BufEnter term://* if winnr('$') == 1 | setlocal laststatus=0 | setlocal cmdheight=0 | endif
" Show statusbar when a non-terminal buffer is in focus.
autocmd BufEnter * if expand('<afile>:p:h') !~ 'term://' | set laststatus=2 | setlocal cmdheight=1 | endif
" Show statusbar when changing to a new tab with several windows
autocmd TabEnter * if winnr('$') > 1 | setlocal laststatus=2 | endif
augroup END
" Avoid reporting "Process exited 0" after exiting shell (bash/PowerShell) inside Neovim.
" Also if no buffers are open then quit Neovim.
" Based on: https://github.com/neovim/neovim/issues/14986#issuecomment-902705190 and
" https://superuser.com/questions/668528/vim-quit-if-buffer-list-is-empty
:autocmd TermClose * execute 'bdelete! ' . expand('<abuf>') | if len(filter(range(1, bufnr('$')), '! empty(bufname(v:val)) && buflisted(v:val)')) == 0 | quit | endif
if exists('g:vscode')
" Neovim Ui Modifier
" Required by: https://marketplace.visualstudio.com/items?itemName=JulianIaquinandi.nvim-ui-modifier
" THEME CHANGER
function! SetCursorLineNrColorInsert(mode)
" Insert mode: blue
if a:mode == "i"
call VSCodeNotify('nvim-theme.insert')
" Replace mode: red
elseif a:mode == "r"
call VSCodeNotify('nvim-theme.replace')
endif
endfunction
augroup CursorLineNrColorSwap
autocmd!
autocmd ModeChanged *:[vV\x16]* call VSCodeNotify('nvim-theme.visual')
autocmd ModeChanged *:[R]* call VSCodeNotify('nvim-theme.replace')
autocmd InsertEnter * call SetCursorLineNrColorInsert(v:insertmode)
autocmd InsertLeave * call VSCodeNotify('nvim-theme.normal')
autocmd CursorHold * call VSCodeNotify('nvim-theme.normal')
augroup END
endif
endif
" Remove the "How-to disable mouse" in right click popup menu item and the separator above it.
" It throws errors when items doesn't exist in menu, so we silence it. Good
" when re-sourcing this file.
silent! aunmenu PopUp.How-to\ disable\ mouse
silent! aunmenu PopUp.-1-
" CTRL-C to Copy.
" Based on: https://github.com/vim/vim/blob/master/runtime/mswin.vim#L32C5-L33C23
vnoremap <C-C> "+y
set laststatus=2
" Config in newer config language Lua.
" Only parsable by Neovim.
" https://github.com/nanotee/nvim-lua-guide#using-lua-from-vimscript
lua << EOF
-- Enable autocompletion, intellisense functionality.
-- Recommended by https://www.youtube.com/watch?v=h4g0m0Iwmys and used by
-- LazyVim (https://www.lazyvim.org/plugins/coding#nvim-cmp).
-- Config based on: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
-- Set up nvim-cmp.
local cmp = require'cmp'
cmp.setup({
snippet = {
-- REQUIRED - you must specify a snippet engine
expand = function(args)
-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
end,
},
window = {
-- completion = cmp.config.window.bordered(),
-- documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
-- { name = 'vsnip' }, -- For vsnip users.
{ name = 'luasnip' }, -- For luasnip users.
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
}, {
{ name = 'buffer' },
})
})
-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {
sources = cmp.config.sources({
{ name = 'git' }, -- You can specify the `git` source if [you were installed it](https://github.com/petertriho/cmp-git).
}, {
{ name = 'buffer' },
})
})
if vim.g.vscode then
-- Required to make completion work in command mode when Neovim is embedded in VS Code.
-- Based on https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance#menu-type
cmp.setup({
view = {
entries = "native" -- can be "custom" (default), "wildmenu" or "native"
}
})
end
if not vim.g.vscode then
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
end
-- Set up lspconfig.
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
-- require('lspconfig')['<YOUR_LSP_SERVER>'].setup {
-- capabilities = capabilities
-- }
-- Get the current version of Neovim
local version = vim.version()
-- Check if the version is 0.10 or greater
if version.major > 0 or (version.major == 0 and version.minor >= 10) then
-- In Neovim 0.10 colors changed to a more gray dark look. This restores my
-- classic vim colors.
vim.cmd('colorscheme vim')
end
EOF