-
Notifications
You must be signed in to change notification settings - Fork 5
/
vimrc
531 lines (445 loc) · 17.5 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
let mapleader = " "
" enable syntax highlighting
syntax on
" theme
set t_Co=256
set background=light
if has("termguicolors")
set termguicolors
endif
" Custom theme https://github.com/sonph/onehalf ===============================
highlight clear
syntax reset
let g:colors_name="onehalflight"
let colors_name="onehalflight"
let s:black = { "gui": "#383a42", "cterm": "237" }
let s:red = { "gui": "#e45649", "cterm": "167" }
let s:green = { "gui": "#50a14f", "cterm": "71" }
let s:yellow = { "gui": "#c18401", "cterm": "136" }
let s:blue = { "gui": "#0184bc", "cterm": "31" }
let s:purple = { "gui": "#004e9c", "cterm": "127" }
let s:cyan = { "gui": "#0997b3", "cterm": "31" }
let s:white = { "gui": "#fafafa", "cterm": "231" }
let s:fg = s:black
let s:bg = s:white
let s:comment_fg = { "gui": "#a0a1a7", "cterm": "247" }
let s:gutter_bg = { "gui": "#fafafa", "cterm": "231" }
let s:gutter_fg = { "gui": "#d4d4d4", "cterm": "252" }
let s:non_text = { "gui": "#e5e5e5", "cterm": "252" }
let s:cursor_line = { "gui": "#f0f0f0", "cterm": "255" }
let s:color_col = { "gui": "#f0f0f0", "cterm": "255" }
let s:selection = { "gui": "#bfceff", "cterm": "153" }
let s:vertsplit = { "gui": "#f0f0f0", "cterm": "255" }
function! s:h(group, fg, bg, attr)
if type(a:fg) == type({})
exec "hi " . a:group . " guifg=" . a:fg.gui . " ctermfg=" . a:fg.cterm
else
exec "hi " . a:group . " guifg=NONE cterm=NONE"
endif
if type(a:bg) == type({})
exec "hi " . a:group . " guibg=" . a:bg.gui . " ctermbg=" . a:bg.cterm
else
exec "hi " . a:group . " guibg=NONE ctermbg=NONE"
endif
if a:attr != ""
exec "hi " . a:group . " gui=" . a:attr . " cterm=" . a:attr
else
exec "hi " . a:group . " gui=NONE cterm=NONE"
endif
endfun
" User interface colors {
call s:h("Normal", s:fg, s:bg, "")
call s:h("Cursor", s:bg, s:blue, "")
call s:h("CursorColumn", "", s:cursor_line, "")
call s:h("CursorLine", "", s:cursor_line, "")
call s:h("LineNr", s:gutter_fg, s:gutter_bg, "")
call s:h("CursorLineNr", s:fg, "", "")
call s:h("DiffAdd", s:green, "", "")
call s:h("DiffChange", s:yellow, "", "")
call s:h("DiffDelete", s:red, "", "")
call s:h("DiffText", s:blue, "", "")
call s:h("IncSearch", s:bg, s:purple, "")
call s:h("Search", s:bg, s:purple, "")
call s:h("ErrorMsg", s:fg, "", "")
call s:h("ModeMsg", s:fg, "", "")
call s:h("MoreMsg", s:fg, "", "")
call s:h("WarningMsg", s:red, "", "")
call s:h("Question", s:purple, "", "")
call s:h("Pmenu", s:fg, s:cursor_line, "")
call s:h("PmenuSel", s:bg, s:blue, "")
call s:h("PmenuSbar", "", s:cursor_line, "")
call s:h("PmenuThumb", "", s:comment_fg, "")
call s:h("SpellBad", s:red, "", "")
call s:h("SpellCap", s:yellow, "", "")
call s:h("SpellLocal", s:yellow, "", "")
call s:h("SpellRare", s:yellow, "", "")
call s:h("StatusLine", s:blue, s:cursor_line, "")
call s:h("StatusLineNC", s:comment_fg, s:cursor_line, "")
call s:h("TabLine", s:comment_fg, s:cursor_line, "")
call s:h("TabLineFill", s:comment_fg, s:cursor_line, "")
call s:h("TabLineSel", s:fg, s:bg, "")
call s:h("Visual", "", s:selection, "")
call s:h("VisualNOS", "", s:selection, "")
call s:h("ColorColumn", "", s:color_col, "")
call s:h("Conceal", s:fg, "", "")
call s:h("Directory", s:blue, "", "")
call s:h("VertSplit", s:vertsplit, s:vertsplit, "")
call s:h("Folded", s:fg, "", "")
call s:h("FoldColumn", s:fg, "", "")
call s:h("SignColumn", s:fg, "", "")
call s:h("MatchParen", s:red, "", "underline")
call s:h("SpecialKey", s:fg, "", "")
call s:h("Title", s:green, "", "")
call s:h("WildMenu", s:fg, "", "")
" }
" Syntax colors {
" Whitespace is defined in Neovim, not Vim.
" See :help hl-Whitespace and :help hl-SpecialKey
call s:h("Whitespace", s:non_text, "", "")
call s:h("NonText", s:non_text, "", "")
call s:h("Comment", s:comment_fg, "", "italic")
call s:h("Constant", s:red, "", "")
call s:h("String", s:green, "", "")
call s:h("Character", s:green, "", "")
call s:h("Number", s:red, "", "")
call s:h("Boolean", s:red, "", "")
call s:h("Float", s:red, "", "")
call s:h("Identifier", s:blue, "", "")
call s:h("Function", s:fg, "", "")
call s:h("Statement", s:purple, "", "")
call s:h("Conditional", s:purple, "", "")
call s:h("Repeat", s:purple, "", "")
call s:h("Label", s:purple, "", "")
call s:h("Operator", s:fg, "", "")
call s:h("Keyword", s:red, "", "")
call s:h("Exception", s:purple, "", "")
call s:h("PreProc", s:yellow, "", "")
call s:h("Include", s:purple, "", "")
call s:h("Define", s:purple, "", "")
call s:h("Macro", s:purple, "", "")
call s:h("PreCondit", s:yellow, "", "")
call s:h("Type", s:yellow, "", "")
call s:h("StorageClass", s:yellow, "", "")
call s:h("Structure", s:yellow, "", "")
call s:h("Typedef", s:yellow, "", "")
call s:h("Special", s:blue, "", "")
call s:h("SpecialChar", s:fg, "", "")
call s:h("Tag", s:fg, "", "")
call s:h("Delimiter", s:green, "", "")
call s:h("SpecialComment", s:comment_fg, "", "italic,bold")
call s:h("Debug", s:fg, "", "")
call s:h("Underlined", s:fg, "", "")
call s:h("Ignore", s:fg, "", "")
call s:h("Error", s:red, s:gutter_bg, "")
call s:h("Todo", s:comment_fg, "", "italic,bold")
" }
" Plugins {
" GitGutter
call s:h("GitGutterAdd", s:green, s:gutter_bg, "")
call s:h("GitGutterDelete", s:red, s:gutter_bg, "")
call s:h("GitGutterChange", s:yellow, s:gutter_bg, "")
call s:h("GitGutterChangeDelete", s:red, s:gutter_bg, "")
" Fugitive
call s:h("diffAdded", s:green, "", "")
call s:h("diffRemoved", s:red, "", "")
" }
" Git {
call s:h("gitcommitComment", s:comment_fg, "", "")
call s:h("gitcommitUnmerged", s:red, "", "")
call s:h("gitcommitOnBranch", s:fg, "", "")
call s:h("gitcommitBranch", s:purple, "", "")
call s:h("gitcommitDiscardedType", s:red, "", "")
call s:h("gitcommitSelectedType", s:green, "", "")
call s:h("gitcommitHeader", s:fg, "", "")
call s:h("gitcommitUntrackedFile", s:cyan, "", "")
call s:h("gitcommitDiscardedFile", s:red, "", "")
call s:h("gitcommitSelectedFile", s:green, "", "")
call s:h("gitcommitUnmergedFile", s:yellow, "", "")
call s:h("gitcommitFile", s:fg, "", "")
hi link gitcommitNoBranch gitcommitBranch
hi link gitcommitUntracked gitcommitComment
hi link gitcommitDiscarded gitcommitComment
hi link gitcommitSelected gitcommitComment
hi link gitcommitDiscardedArrow gitcommitDiscardedFile
hi link gitcommitSelectedArrow gitcommitSelectedFile
hi link gitcommitUnmergedArrow gitcommitUnmergedFile
" }
" Fix colors in neovim terminal buffers {
let g:terminal_color_0 = s:black.gui
let g:terminal_color_1 = s:red.gui
let g:terminal_color_2 = s:green.gui
let g:terminal_color_3 = s:yellow.gui
let g:terminal_color_4 = s:blue.gui
let g:terminal_color_5 = s:purple.gui
let g:terminal_color_6 = s:cyan.gui
let g:terminal_color_7 = s:white.gui
let g:terminal_color_8 = s:black.gui
let g:terminal_color_9 = s:red.gui
let g:terminal_color_10 = s:green.gui
let g:terminal_color_11 = s:yellow.gui
let g:terminal_color_12 = s:blue.gui
let g:terminal_color_13 = s:purple.gui
let g:terminal_color_14 = s:cyan.gui
let g:terminal_color_15 = s:white.gui
let g:terminal_color_background = s:bg.gui
let g:terminal_color_foreground = s:fg.gui
" }
" =============================================================================
" load plugins
if has('nvim') && filereadable(expand("~/.config/nvim/lua/config/lazy.lua"))
lua require("config.lazy")
" alternate files
function! AltCommand(path, vim_command)
let l:alternate = system("alt " . a:path)
if empty(l:alternate)
echo "No alternate file for " . a:path . " exists!"
else
exec a:vim_command . " " . l:alternate
endif
endfunction
nnoremap <Leader>o :call AltCommand(expand('%'), ':e')<CR>
endif
set laststatus=2 " always display the status line
set colorcolumn=121 " make it obvious where 120 characters is
set listchars=tab:»·,trail:█ " show tabs and trailing whitespaces
set number relativenumber " hybrid relative number + absolute
" highlight trailing whitespaces and tabs
autocmd BufEnter,InsertLeave * setlocal list
autocmd InsertEnter * setlocal nolist
"
" don't use relative numbers in insert more
autocmd BufEnter,FocusGained,InsertLeave * setlocal relativenumber
autocmd BufLeave,FocusLost,InsertEnter * setlocal norelativenumber
" highlight trailing whitespaces and tabs
match ExtraWhitespace /\(\s\+$\|\t\)/
hi ExtraWhitespace ctermbg=172 guifg=#d78700
" required to detect filetype
filetype plugin indent on
set shell=/bin/bash
" status line
set statusline=%f\ %=col:%2c\ line:%2l
set backspace=2 " make backspace work like most other apps
set noswapfile " don't swap buffer to file
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set incsearch " do incremental searching
set history=500 " sets how many lines of history VIM has to remember
set nowrap " don't wrap long lines
set wildmenu " visual autocomplete for command menu
set ignorecase " /the would find 'the' or 'The', add \C if you want 'the' only
set smartcase " while /The would find only 'The' etc.
set nopaste " enable formatting while pasting
set pastetoggle=<F2> " switch paste mode
set clipboard=unnamedplus " yank to and paste the selection without prepending "*
set autowrite " save file before switching a buffer
set autoindent " indent
set showmatch " highlight matching brackets
set autoread " when file was changed
set lazyredraw " redraw only when we need to"
set hlsearch " highlight same words while searching with Shift + *
" line numbers
set number " show
set numberwidth=5 " line numbers width
set textwidth=240
set formatoptions+=w " for wraping long lines without broken words
" spaces
set tabstop=2 " tab width
set shiftwidth=2 " indent size
set softtabstop=2 " simulated tab width
set expandtab " spaces on tab press
set smartindent " indent automatically
set shiftround " uses shiftwidth when >, <
" map russian keyboard
set keymap=russian-jcukenwin
set iminsert=0
set imsearch=0
" removes the delay when hitting esc in insert mode
set ttimeout
set ttimeoutlen=1
" disable sound
set visualbell t_vb=
" automatically refresh changed files
set autoread
" fold method definitions
set foldenable " enable folding
set foldlevel=20 " open all folds by default
set foldmethod=indent " fold based on indent level
nnoremap fo za " folding shortcut
nnoremap fO zM " folding shortcut
nnoremap fu zR " folding shortcut
" coc.nvim settings:
set cmdheight=2 " give more space for displaying messages.
set updatetime=300 " having longer updatetime (default is 4000 ms = 4 s) leads to noticeable delays and poor user experience.
" make line number brighter
hi LineNr ctermfg=240 guifg=#585858
" When editing a file, always jump to the last known cursor position.
" Don't do it for commit messages, when the position is invalid, or when
" inside an event handler (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if &ft != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
" create directory if doesn't exist before saving
autocmd BufWritePre * if !isdirectory(expand('%:h')) | call mkdir(expand('%:h'), 'p') | endif
autocmd BufWritePre *.rb,*.haml,*.coffee,*.md,*.rake,*.clj,*.js,*.jsx,*.ts,*.tsx,*.go,*.ex,*.py :%s/\s\+$//e " remove trailing whitespaces
autocmd FileType ruby,eruby,yaml,clojure setlocal ai sw=2 sts=2 " autoindent with two spaces, always expand tabs
autocmd FileType ruby,eruby,yaml,elixir setlocal iskeyword=@,48-57,192-255,_ " make @,numbers,latin chars,_,? part of words
autocmd FileType markdown setlocal wrap colorcolumn=240 shiftwidth=2 " automatically wrap for Markdown
autocmd FileType markdown setlocal spell spelllang=ru_ru,en_us " enable spellchecking for Markdown messages
autocmd FileType gitcommit setlocal colorcolumn=72 " automatically wrap at 72 characters
autocmd FileType gitcommit setlocal spell " enable spellchecking for git commit messages
autocmd FileType css,scss,sass setlocal iskeyword+=- " allow stylesheets to autocomplete hyphenated words
autocmd FileType clojure setlocal iskeyword=@,48-57,_,192-255,?,-,*,!,+,=,<,>,:,$ " customize keywords
autocmd FileType crontab setlocal nobackup nowritebackup " allow to edit crontab -e
autocmd FileType go setlocal noexpandtab listchars=tab:\ \ ,trail:█ tabstop=4 shiftwidth=4 " go settings
" Elixir syntax highlight
autocmd BufRead,BufNewFile *.ex,*.exs set filetype=elixir
autocmd BufRead,BufNewFile *.eex,*.heex,*.leex,*.sface,*.lexs set filetype=eelixir
autocmd BufRead,BufNewFile mix.lock set filetype=elixir
" Insert debugger
autocmd BufRead *.rb nnoremap <A-p> Orequire 'pry'; binding.pry<Esc>
\| nnoremap <C-]> :call JumpToRubyDefinition()<CR>
autocmd BufRead *.js,*.jsx,*.ts,*.tsx nnoremap <A-p> Odebugger;<Esc>
autocmd BufRead *.ex,*.exs,*.eex nnoremap <A-p> Orequire IEx; IEx.pry<Esc>
" Format
autocmd FileType xml nnoremap <buffer> ff :%!xmllint --format --encode UTF-8 -<CR>
autocmd FileType html nnoremap <buffer> ff :%! cat % \| tidy -q -i -w 0 -ashtml 2>/dev/null<CR>
autocmd FileType json nnoremap <buffer> ff :%! cat % \| ruby -e "require 'json'; puts JSON.pretty_generate(JSON.parse(STDIN.read))"<CR>
autocmd FileType xml,json,javascriptreact,typescriptreact vmap <buffer> ff :%!tidy -q -i -w 0 -xml 2>/dev/null<CR>
function! JumpToRubyDefinition()
" Copy the whole word with "::" namespace delimiters
setlocal iskeyword+=:
setlocal iskeyword+=?
setlocal iskeyword+=!
normal yiw
setlocal iskeyword-=:
setlocal iskeyword-=?
setlocal iskeyword-=!
" Replace prepending "::" and "!
let l:word = substitute(substitute(@", "^::", "", ""), "^!", "", "")
" Jump to ctag definition
execute ":ltag " . word
endfunction
" wrap long lines
nnoremap wl gggqG
" redraw vim
nnoremap rr :so $MYVIMRC \| checktime<CR>
" replace text in a project with sed
nnoremap <Leader>RE :!fd --type file --extension jsx --print0 \| xargs -0 sed -i '' 's,search,replace,g'
nnoremap <Leader>re :%s,search,replace,gc
" Clear current search highlight by double tapping //
nnoremap <Esc> :noh<CR>
" switch to Russian keyboard
inoremap <A-c> <C-^>
" get off my lawn
nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>
" Quicker window movement
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
" max / min tab size
nnoremap ƒ :tabedit %<CR>
nnoremap Ï :tabclose<CR>
" visual blockwise selection
nnoremap <A-v> <C-v>
" move line up / down with Ctrl + j / k
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv
" jump up and down
nnoremap <A-d> <C-d>
nnoremap <A-u> <C-u>
" undo
nnoremap <A-r> <C-r>
" copy relative filepath
nnoremap cP :let @+=expand("%")<CR>
" copy relative filetype with line
nnoremap cLP :let @+=expand("%") . ':' . line(".")<CR>
" copy full filepath
nnoremap cFP :let @+=expand("%:p")<CR>
" navigating between buffers
nnoremap <A-w> :bd<CR>
nnoremap <A-q> :bd!<CR>
nnoremap <A-]> :bn<CR>
nnoremap <A-[> :bp<CR>
" save file with Alt + s
" note that remapping C-s requires flow control to be disabled, e.g. in .bashrc or .zshrc
noremap <A-s> <esc>:w<CR>
inoremap <A-s> <esc>:w<CR>
" Copy error message to clipboard
nnoremap <Leader>e :let @+ = v:statusmsg<CR>
" save file and exit
nnoremap <Leader>wq :wq<CR>
" don't save and exit
nnoremap <Leader>cq :cq<CR>
" cut content to new buffer
vnoremap <Leader>x x :w \| :enew<CR>pGE :w<Space>
" saveas with :S filepath
command! -nargs=1 -complete=file S :saveas <args>
" change without yanking
nnoremap c "_c
vnoremap c "_c
" delete / replace currently selected text with default register without yanking it
vnoremap p "_dP
vnoremap d "_d
" delete line / word / end of line without yanking (copying) it
nnoremap dd "_dd
nnoremap de "_de
nnoremap D "_D
" Enter visual mode in terminal
tnoremap <Esc> <C-\><C-n>
" Still allow exiting fzf screen with Esc
autocmd FileType fzf tnoremap <buffer> <esc> <c-c>
" Enter insert mode automatically
if has('nvim')
autocmd TermOpen * startinsert
endif
" setup undodir if +persistent_undo option included
if has('persistent_undo') && isdirectory($HOME . '/.vim/undo')
set undodir=~/.vim/undo
set undofile
end
" use ripgrep
if executable('rg')
set grepprg=rg\ --vimgrep " use rg over grep
endif
" open large files > 10 MB
let g:LargeFile = 10 * 1024 * 1024
function! LargeFile()
setlocal eventignore+=FileType " no syntax highlighting etc
setlocal bufhidden=unload " save memory when other file is viewed
setlocal undolevels=-1 " no undo possible
setlocal foldmethod=manual
setlocal noswapfile
endfunction
" files with filesize too large are recognized too (getfsize = -2)
autocmd BufReadPre * let f=getfsize(expand("<afile>")) | if f > g:LargeFile || f == -2 | call LargeFile() | endif
" show list of all filetypes
function! SortUnique(list, ...)
let dictionary = {}
for i in a:list
let dictionary[string(i)] = i
endfor
if ( exists( 'a:1' ) )
let result = sort( values( dictionary ), a:1 )
else
let result = sort( values( dictionary ) )
endif
return result
endfunction
command! Filetypes execute "echo
\ join(
\ SortUnique(
\ map(
\ split(
\ globpath(&rtp, 'ftplugin/*.vim') . globpath(&rtp, 'syntax/*.vim'),
\ '\n'),
\ \"fnamemodify(v:val, ':t:r')\")),
\'\n')"