-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.vimrc
More file actions
515 lines (414 loc) · 13.6 KB
/
.vimrc
File metadata and controls
515 lines (414 loc) · 13.6 KB
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
" ----------------------------------------
"
" Reusable variables
"
" ----------------------------------------
let s:popup_list_max_items = 20
let s:popup_list_max_width = 70
" ----------------------------------------
"
" Reusable functions
"
" ----------------------------------------
function! s:TruncatePath(path)
if strlen(a:path) <= s:popup_list_max_width
return a:path
endif
return '…' . strpart(a:path, strlen(a:path) - s:popup_list_max_width + 1)
endfunction
function! s:ShowPopupMenu(title, items, callback)
if empty(a:items)
echo "No items"
return
endif
call popup_menu(a:items, {
\ 'title': a:title,
\ 'minwidth': s:popup_list_max_width,
\ 'maxwidth': s:popup_list_max_width,
\ 'padding': [0,1,0,1],
\ 'callback': a:callback,
\ 'filter': 'popup_filter_menu',
\ 'mapping': 1,
\ })
endfunction
" ----------------------------------------
"
" Basic settings
"
" ----------------------------------------
" Disable compatibility with vi editor in order to use modern Vim features.
set nocompatible
" Setup leader symbol. This is the button that you press before
" using another combo keybinding.
nnoremap <SPACE> <Nop>
let mapleader=" "
" Enable mouse support.
set mouse=a
" Enable syntax highlighting.
syntax enable
" Configure indentation.
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set textwidth=100
set smartindent
" Use tabs for Makefile since required.
autocmd FileType make setlocal noexpandtab
" Use 2 spaces for JS/TS and their JSX variants.
autocmd FileType javascript,typescript,javascriptreact,typescriptreact setlocal tabstop=2 softtabstop=2 shiftwidth=2
" Spell checking.
set spelllang=en_us
set spell
" Highlight current line which mouse is on.
set cursorline
highlight Cursorline cterm=bold ctermbg=black
" Enable highlight search pattern.
set hlsearch
" Enable smart case search sensitivity.
set ignorecase
set smartcase
" Enable relative line numbers.
set number
set relativenumber
" Show the matching part of pairs [] {} and ().
set showmatch
" How long Vim waits (in milliseconds) before triggering certain idle events.
set updatetime=800
" Setup cursor appearance for different modes.
let &t_SI = "\e[5 q"
let &t_SR = "\e[3 q"
let &t_EI = "\e[1 q"
" Auto completion alias. Press Shift-Tab in Insert mode to
" view auto completion options. It does not rely on LSP.
inoremap <S-Tab> <C-n>
set complete=.,w,b,u,t
" Multi line editing.
" Select multiple lines in visual mode
" and press <leader>aa. Then you can add
" any prefix you want and press Escape.
" To remove prefix select lines in visual
" mode in a way that the last selection
" captures the whole prefix you want to remove.
" Press <leader>ar which will select prefix for
" each row. Then press x which will remove prefix.
" Can be used for commenting.
vnoremap <leader>aa <C-v>A
vnoremap <leader>ar <C-v>
" Refresh file. Useful if for any reason
" lps warnings are cached.
nnoremap <leader>rr :e!<CR>
" ----------------------------------------
"
" Code aliases
"
" ----------------------------------------
" Ctrl + l alias for Browser console.log API.
inoremap <C-l> console.log()<Left>
" Ctrl + p alias for Rust's println! macro.
inoremap <C-p> println!()<Left>
" ----------------------------------------
"
" Theme settings
"
" ----------------------------------------
" Enable color themes.
if !has('gui_running')
set t_Co=256
endif
" Enable true colors support.
set termguicolors
" Set theme.
" Light.
colorscheme shine
" Dark.
" colorscheme habamax
" ----------------------------------------
"
" Status line settings
"
" ----------------------------------------
" Always show status line.
set laststatus=2
" Displays the current Vim mode in the status line.
set showmode
" Show path to current file.
set statusline=%f
" Split on the left and right.
set statusline+=%=
" Show row and col info.
set statusline+=%(%l,%c%V\ %=\ %P%)
" Show currently opened file extension.
set statusline+=\ %y
" Show if buffer is read-only.
set statusline+=\ %r
" Show how many matches we have for the current search
" pattern in the current file when we use ?<something>.
function! StatusLineSearchCount()
if !v:hlsearch
return ''
endif
let sc = searchcount({ 'maxcount': 0 })
if empty(sc) || sc.total == 0
return ''
endif
return printf('[%d/%d]', sc.current, sc.total)
endfunction
set statusline+=\ %{StatusLineSearchCount()}
" Setup git branch in status line.
function! UpdateGitBranch()
let dir = expand('%:p:h')
if finddir('.git', dir . ';') != ''
let b:git_branch = substitute(system('git -C ' . dir . ' branch --show-current'), '\n', '', '')
else
let b:git_branch = ''
endif
endfunction
augroup gitbranch
autocmd!
autocmd BufEnter,DirChanged * call UpdateGitBranch()
augroup END
let &statusline .= ' %{empty(get(b:, "git_branch", "")) ? "" : " " . get(b:, "git_branch")}'
" ----------------------------------------
"
" Search settings
"
" ----------------------------------------
" Set absolute path to be able to search in all sub directories.
set path+=$PWD/**
" Visual menu for command completion.
set wildmenu
" 'wildignore' only removes entries from the wildmenu
" after entries have been found. It does not change search times.
set wildignore+=**/node_modules/**,**/dist/**,**/.git/**,**/build/**,*.pyc,*.o
" Make file search case insensitive.
set wildignorecase
" Search for a file.
" This setting should contain space after :find so you
" don't need to specify it manually after <leader>sf.
nnoremap <leader>sf :find
" Search in files with entering path.
" This setting should contain space after :e so you
" don't need to specify it manually after <leader>sp.
nnoremap <leader>sp :e
" Automatically add file extensions when searching for files.
set suffixesadd+=.py,.js,.jsx,.ts,.tsx,.c,.h,.cpp,.json,.rs,.css,.scss
" Set grep search tool to 'git grep'.
set gp=git\ grep\ -n
" Search for sub string in all project files.
nnoremap <leader>sg :grep ""<Left>
" Search for current word under the cursor in all project files.
nnoremap <leader>sgc :grep! <C-R><C-W>
" Just press Enter after performing grep search and it
" will automatically run :copen
augroup quickfix
autocmd!
autocmd QuickFixCmdPost [^l]* cwindow
autocmd QuickFixCmdPost l* lwindow
augroup END
" ----------------------------------------
"
" Resolving merge conflicts settings
"
" ----------------------------------------
" 1. Merge branch into target branch.
" 2. Then run 'git mergetool' manually from terminal or open vim and run <leader>dvm.
" 3. To accept changes you can write ':diffget N' where N is the number of window with
" corresponding changes.
nnoremap <leader>dvm :!git mergetool<CR>
" ----------------------------------------
"
" Git file changes
"
" ----------------------------------------
" Keep track of all changed file after we opened one.
let s:git_changed_files = []
function! s:OpenGitChangedFileFromPopup(id, result)
if a:result <= 0
return
endif
let file = s:git_changed_files[a:result - 1]
execute 'edit ' . fnameescape(file)
endfunction
function! GitChangedFilesPopup()
let s:git_changed_files = systemlist('git diff --name-only')
if empty(s:git_changed_files)
echo "No changed files"
return
endif
let filtered = s:git_changed_files[:s:popup_list_max_items - 1]
let display = map(copy(filtered), {_, v -> s:TruncatePath(v)})
call s:ShowPopupMenu('Changed files', display, function('s:OpenGitChangedFileFromPopup'))
endfunction
nnoremap <leader>dv :call GitChangedFilesPopup()<CR>
" Revert all changes for the current file.
nnoremap <leader>dvr :!git restore %<CR>:e!<CR>
" Show diff for the current file.
function! GitFileDiff()
let root = systemlist('git rev-parse --show-toplevel')[0]
let f = substitute(expand('%:p'), root.'/', '', '')
let original_name = expand('%:t')
vert new
setlocal buftype=nofile
setlocal bufhidden=wipe
execute 'file HEAD:' . original_name
execute 'silent! read !git show HEAD:' . f
" Delete the first line of the HEAD file that might contain
" an empty line or some metadata.
1delete
filetype detect
diffthis
wincmd p
diffthis
endfunction
nnoremap <leader>dvf :call GitFileDiff()<CR>
" ----------------------------------------
"
" File explorer settings
"
" ----------------------------------------
" Open native Vim netrw file explorer.
nnoremap <leader>e :Explore<CR>
" Enable relative line numbers inside netrw so it's easier to jump to the
" specific file/folder.
let g:netrw_bufsettings = 'noma nomod rnu nowrap ro nobl'
" You need to run all these commands from netrw file explorer.
function! NetrwMapping()
" Mark a file. You need this for further file manipulations like
" file deletion/moving/copying.
nmap <buffer> <TAB> mf
" Unmark all marked files.
nmap <buffer> <Leader><TAB> mu
" Current directory under the cursor becomes the target.
" You need this to mark some directory where you want to
" move/copy marked files in.
nmap <buffer> <S-TAB> mt
" Create a new file.
nmap <buffer> ff %
" Create a new directory.
nmap <buffer> fd d
" Edit file/directory name.
nmap <buffer> fe R
" Delete file or empty directory.
nmap <buffer> fr D
" Copy marked files to target directory.
nmap <buffer> fc mc
" After you mark your files you can put the cursor in a directory
" and this will assign the target directory and copy in one step.
nmap <buffer> fC mtmc
" Move marked files to target directory.
nmap <buffer> fx mm
" Same thing as fC but for moving files.
nmap <buffer> fX mtmm
" Run external shell command on marked files.
nmap <buffer> f; mx
" Bookmark current directory.
nmap <buffer> fb mb
" Go to previous bookmarked directory.
nmap <buffer> fbg gb
" Remove most recent bookmark.
nmap <buffer> fbr mB
" Show a list of marked files.
nmap <buffer> flm :echo join(netrw#Expose("netrwmarkfilelist"), "\n")<CR>
endfunction
augroup netrw_mapping
autocmd!
autocmd filetype netrw call NetrwMapping()
augroup END
" Disable top banner (I in netrw to show back).
let g:netrw_banner = 0
" ----------------------------------------
"
" Opened file settings
"
" ----------------------------------------
" Split window from the current file.
nnoremap <leader>v :Vexplore<CR>
" Increase size of the current split window.
nnoremap <leader>> :vertical resize +10<CR>
" Decrease size of the current split window.
nnoremap <leader>< :vertical resize -10<CR>
" Make all split windows equal size.
nnoremap <leader>= :wincmd =<CR>
" Open a new tab from the current file.
" You can jump to a specific tab with Ngt
" where N is a number of tab.
nnoremap <leader>t :tab split<CR>
" ----------------------------------------
"
" Previously opened files
"
" ----------------------------------------
function! ListFileBuffers()
let buffers = getbufinfo({'buflisted': 1})
call sort(buffers, {a, b -> b.lastused - a.lastused})
let items = []
for b in buffers
if filereadable(b.name)
call add(items, b)
endif
endfor
let filtered = items[:s:popup_list_max_items - 1]
let display = map(copy(filtered), {_, b -> s:TruncatePath(fnamemodify(b.name, ':.'))})
call s:ShowPopupMenu('Previous files', display, {id, result ->
\ result > 0 ? execute('buffer ' . filtered[result - 1].bufnr) : 0
\ })
endfunction
command! LsFiles call ListFileBuffers()
nnoremap <leader>? :LsFiles<CR>
" ----------------------------------------
"
" Language specific settings
"
" ----------------------------------------
" You need to install a language server for desired language
" since Vim cannot support all languages out of the box.
" LSP configuration.
" Install LSP plugin:
" 1. mkdir -p ~/.vim/pack/lsp/start
" 2. cd ~/.vim/pack/lsp/start
" 3. git clone https://github.com/prabirshrestha/vim-lsp.git
" Lsp for Rust.
" Install rust-analyzer globally: rustup component add rust-analyzer
" or in any other way that is applicable for your OS.
if executable('rust-analyzer')
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'rust-analyzer',
\ 'cmd': {server_info->['rust-analyzer']},
\ 'allowlist': ['rust'],
\ })
endif
" Lsp for JS/TS.
" Install language server globally: npm install -g typescript typescript-language-server.
if executable('typescript-language-server')
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'ts-lsp',
\ 'cmd': {server_info->['typescript-language-server', '--stdio']},
\ 'allowlist': ['javascript','javascriptreact','typescript','typescriptreact'],
\ })
endif
" Lsp for Python.
" Install language server globally: pip install 'python-lsp-server[all]'
if executable('pylsp')
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'pylsp',
\ 'cmd': {server_info->['pylsp']},
\ 'allowlist': ['python'],
\ })
endif
" Go to definition. Place your cursor on variable/function or anything else
" and run this command.
nnoremap gd :LspDefinition<CR>
" Show type information (hover).
nnoremap K :LspHover<CR>
" Code action. For example, if you have a missing import, you can
" put your cursor over it and press <leader>ca and select autoimport option.
nnoremap <leader>ca :LspCodeAction<CR>
" Make the code actions list to be a floating window.
let g:lsp_code_action_ui = 'float'
" Show LSP errors in the current file.
nnoremap <leader>si :e!<CR>:LspDocumentDiagnostics<CR>
" Apply formatting for the current file.
nnoremap <leader>fm :w<CR>:e!<CR>:LspDocumentFormat<CR>
" You can check more LSP specific actions in the vim-lsp plugin documentation.