-
Notifications
You must be signed in to change notification settings - Fork 3
/
.vimrc
476 lines (379 loc) · 12.9 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
" https://stackoverflow.com/questions/18321538/vim-error-e474-invalid-argument-listchars-tab-trail
scriptencoding utf-8
set encoding=utf-8
" as per https://github.com/nvim-tree/nvim-tree.lua docs
let g:loaded_netrw = 1
let g:loaded_netrwPlugin = 1
" trick ideavim into skipping this
exec "source ~/.plugins.vim"
if (has("termguicolors"))
set termguicolors
endif
" colorscheme molokai " this has to come after 'filetype plugin indent on'
colorscheme onedark
" Fix vimdiff colors
highlight DiffText guifg=NONE guibg=#000000 gui=NONE
highlight DiffChange guifg=NONE guibg=#484540 gui=NONE
highlight DiffDelete gui=NONE guibg=#1e2127 guifg=#5f3a41
highlight DiffAdd gui=NONE guifg=NONE guibg=#3b453f
set exrc " enable per-directory .vimrc files
set secure " disable unsafe commands in local .vimrc files
" now using treesitter for syntax
" syntax on
" set t_Co=256
" inoremap <Tab> <Esc>`^
inoremap kj <Esc>`^
inoremap jj <Esc>`^
set noswapfile
"" Whitespace
set nowrap " don't wrap lines
set tabstop=2 shiftwidth=2 " a tab is two spaces (or set this to 4)
set softtabstop=2
set expandtab " use spaces, not tabs (optional)
set backspace=indent,eol,start " backspace through everything in insert mode
set shiftround
set showmatch
set colorcolumn=80
" when ':set wrap'
set linebreak
set breakindent
" suggest correct spelling in CTRL_N/CTRL_P
set complete+=kspell
" copy/paste to clipboard without prepending "*
set clipboard=unnamed,unnamedplus
" display incomplete commands
set showcmd
" make tab in v mode ident code
vmap <tab> >gv
vmap <s-tab> <gv
set fileformat=unix
"" Searching
set hlsearch " highlight matches
set incsearch " incremental searching
set ignorecase " searches are case insensitive...
set smartcase " ... unless they contain at least one capital letter
set smartindent
set autoread
if has('nvim')
" https://github.com/neovim/neovim/issues/1936
au FocusGained * :checktime
endif
set hidden
if !has('nvim')
set cryptmethod=blowfish2
endif
" command line completion
set wildchar=<Tab> wildmenu wildmode=list:longest,list:full
set switchbuf=useopen
" Thorfile, Rakefile, Vagrantfile and Gemfile are Ruby
au BufRead,BufNewFile {Gemfile,Rakefile,Capfile,Vagrantfile,Thorfile,config.ru} set ft=ruby
au BufRead,BufNewFile *.mdx set ft=mdx
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)
function s:RunShellCommand(cmdline)
let isfirst = 1
let words = []
for word in split(a:cmdline)
if isfirst
let isfirst = 0 " don't change first word (shell command)
else
if word[0] =~ '\v[%#<]'
let word = expand(word)
endif
let word = shellescape(word, 1)
endif
call add(words, word)
endfor
let expanded_cmdline = join(words)
botright new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
call setline(1, 'You entered: ' . a:cmdline)
call setline(2, 'Expanded to: ' . expanded_cmdline)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !'. expanded_cmdline
1
endfunction
function s:GrepOpenBuffers(search, jump)
call setqflist([])
let cur = getpos('.')
silent! exe 'bufdo vimgrepadd /' . a:search . '/ %'
let matches = len(getqflist())
if a:jump && matches > 0
sil! cfirst
else
call setpos('.', cur)
endif
echo 'BufGrep:' ((matches) ? matches : 'No') 'matches found'
endfunction
com! -nargs=1 -bang BufGrep call <SID>GrepOpenBuffers('<args>', <bang>0)
nnoremap <Leader>B :BufGrep
" remap 'increase number' since C-a is captured by tmux/screen
" Easier increment/decrement
nnoremap + <C-a>
nnoremap - <C-x>
" trick ideavim into skipping this
exec "set relativenumber"
set number
" Opens an edit command with the path of the currently edited file filled in
" Normal mode: <Leader>e
map <Leader>e :e <C-R>=expand("%:p:h") . "/" <CR>
" CTags
"
" $PATH appears different to vim for some reason and hence wrong ctags gets picked
" until then, you need to manually override ctags in /usr/bin/ with those from homebrew
" TODO fix vim path
nmap <Leader>rt :!git ls-files \| ctags --links=no -L-<CR><CR>
au FileType {ruby} nnoremap <leader>rt :!git ls-files \| ctags --languages=ruby --links=no -L-<CR><CR>
au FileType {javascript,javascriptreact} nnoremap <leader>rt :!git ls-files \| ctags --languages=javascript --links=no -L-<CR><CR>
" Remember last location in file
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
set scrolloff=3 " Keep 3 lines below and above the cursor"
" Make Y behave like other capitals
nnoremap Y y$
" Improve up/down movement on wrapped lines
nnoremap j gj
nnoremap k gk
" Stop messing with my arrow keys
if !has("gui_running")
let g:AutoClosePreservDotReg = 0
endif
nnoremap <leader>y "*y
" Prevent Vim from clobbering the scrollback buffer. See
" http://www.shallowsky.com/linux/noaltscreen.html
set t_ti= t_te=
" Fake '|' as text object
nnoremap di\| T\|d,
nnoremap da\| F\|d,
nnoremap ci\| T\|c,
nnoremap ca\| F\|c,
nnoremap yi\| T\|y,
nnoremap ya\| F\|y,
nnoremap vi\| T\|v,
nnoremap va\| F\|v,
" Fake '/' as text object
nnoremap di/ T/d,
nnoremap da/ F/d,
nnoremap ci/ T/c,
nnoremap ca/ F/c,
nnoremap yi/ T/y,
nnoremap ya/ F/y,
nnoremap vi/ T/v,
nnoremap va/ F/v,
set list listchars=trail:·
" disable folding
set nofoldenable
" alias backtick to signle quote
map ' `
fun RangerChooser()
exec "silent !ranger --choosefile=/tmp/chosenfile " . expand("%:p:h")
if filereadable('/tmp/chosenfile')
exec 'edit ' . system('cat /tmp/chosenfile')
call system('rm /tmp/chosenfile')
endif
redraw!
endfun
function! OpenWithRanger()
let rangerCallback = { 'name': 'ranger' }
function! rangerCallback.on_exit(id, code)
try
if filereadable('/tmp/chosenfile')
exec 'edit ' . readfile('/tmp/chosenfile')[0]
call system('rm /tmp/chosenfile')
endif
endtry
endfunction
enew
call termopen('ranger --choosefile=/tmp/chosenfile', rangerCallback)
startinsert
endfunction
map <Leader><Leader>r :call OpenWithRanger()<CR>
" function GoogleSearch()
" normal gv"xy
" let query = 'http://google.com/search?q=' .
" \ system('perl -MURI::Escape -e "print uri_escape(q#'. escape(@x, '#"') .'#)"')
" silent execute "! open " .
" \ shellescape(query, 'yes_please_escape_vim_special_characters_too_thank_you')
" endfunction
" vnoremap <Leader>s :call GoogleSearch()<cr>
" don't show ^I for go files
aut BufRead,BufNewFile *.go set nolist
" nnoremap <C-l> :redraw!<cr>
" My remapping of <C-^>. If there is no alternate file, then switch to
" previous buffer.
function SwitchToPrevBuffer()
if expand('#')=="" | silent! bprev
else
exe "normal! \<c-^>"
endif
endfu
nnoremap <C-^> :call SwitchToPrevBuffer()<CR>
hi LineNr ctermbg=NONE guibg=NONE ctermfg=14 guifg=#80a0ff
hi MatchParen ctermfg=208 ctermbg=233 cterm=bold
hi Search cterm=bold ctermfg=255 ctermbg=238 guifg=NONE guibg=#3B4048
hi link illuminatedWord Search
highlight ALEError cterm=bold gui=bold ctermbg=238 guibg=#3B4048
highlight ALEWarning cterm=bold gui=bold ctermbg=238 guibg=#3B4048
hi Comment gui=italic
" select last inserted text
nnoremap gi `[v`]h
" autocomplete dashes too
set iskeyword+=-
" : is NOT a part of a word
set iskeyword-=:
" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %
:" The leader defaults to backslash, so (by default) this
:" maps \* and \g* (see :help Leader).
:" These work like * and g*, but do not move the cursor and always set hls.
map <Leader>* :let @/ = '\<'.expand('<cword>').'\>'\|set hlsearch<C-M>
map <Leader>g* :let @/ = expand('<cword>')\|set hlsearch<C-M>
if executable('rg')
set grepprg=rg\ --vimgrep
set grepformat=%f:%l:%c:%m
elseif executable('ag')
set grepprg=ag\ --vimgrep
set grepformat=%f:%l:%c:%m
endif
" search only within unfolded text
set fdo-=search
" autocmd filetype crontab setlocal nobackup nowritebackup
set nobackup
set nowritebackup
au BufNewFile,BufRead Jenkinsfile setf groovy
" Paste with indentation
nnoremap p p=`]
set ttyfast
set lazyredraw
" If set, the following breaks deoplete completion with Pattern Not Found
" set noshowmode
hi Visual ctermbg=darkgrey
" paste without loosing copied text
xnoremap <leader>p "_dp
xnoremap <leader>P "_dP
packadd cfilter
" command! Ctags call system('ctags $(git ls-files)')
" also useful:
" - Ctrl-W p - go to the previous window
" - Ctrl-W t - go to top/left window
" - Ctrl-W b - go to bottom/right window
map <c-j> <C-W>j
map <c-k> <C-W>k
map <c-h> <C-W>h
map <c-l> <C-W>l
" auto close quickfix
" aug QFClose
" au!
" " au WinEnter * if winnr('$') == 1 && &buftype == "quickfix"|q|endif
" au WinEnter * if &buftype != "quickfix"|cclose|endif
" aug END
" select last paste in visual mode
nnoremap <expr> gb "'[" . strpart(getregtype(), 0, 1) . "']"
" autocmd BufWinEnter quickfix resize 20
" Autosave
fun! s:SavePreservingLastPasteMarks()
if expand('%') == ''
return
endif
" let paste_start_pos = getpos("'[")
" let paste_end_pos = getpos("']")
" " Saving file resets '] and '[ marks for some reason, so we need to carry them
" " across for `gb` (see above) to work.
silent! lockmarks w
" call setpos("'[", paste_start_pos)
" call setpos("']", paste_end_pos)
endf
" autocmd FocusLost * call <SID>SavePreservingLastPasteMarks()
" autocmd CursorHold * call <SID>SavePreservingLastPasteMarks()
autocmd TextChanged * call <SID>SavePreservingLastPasteMarks()
autocmd InsertLeave * call <SID>SavePreservingLastPasteMarks()
" autocmd TextChangedI * call <SID>SavePreservingLastPasteMarks()
" autocmd TextChangedP * call <SID>SavePreservingLastPasteMarks()
" au TextChanged * :echo 'balls'
" FileChangedShell
" BufWritePost
" FileWritePost
" set updatetime=10
set cursorline
" copy current file path into clipboard
nmap <leader><leader>c :let @*=expand("%:p")<cr>
nmap <leader><leader>C :let @*=expand("%:t")<cr>
" search for visually selected text
vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>
fun! s:JumpToNearestError(direction) abort
let errors = getloclist(0)
let current_ln = getcurpos()[1]
let target_pos = []
let cmp = a:direction == 'up' ? '<' : '>'
let nearest_error = get(filter(deepcopy(errors), 'v:val.lnum '. cmp .' '.current_ln), 0, {})
if len(nearest_error)
let target_pos = [0, nearest_error.lnum, nearest_error.col]
elseif len(errors)
let index = a:direction == 'up' ? -1 : 0
let first_or_last_error = errors[index]
let target_pos = [0, first_or_last_error.lnum, first_or_last_error.col]
endif
if len(target_pos)
call setpos('.', target_pos)
endif
endf
" only available in nvim at the moment
if has('nvim')
" show substitution result interactively
set inccommand=nosplit
endif
nnoremap <c-s> :w<cr>
" nnoremap ]l :call <SID>JumpToNearestError('down')<CR>
" nnoremap [l :call <SID>JumpToNearestError('up')<CR>
function! s:CompareQuickfixEntries(i1, i2)
if bufname(a:i1.bufnr) == bufname(a:i2.bufnr)
return a:i1.lnum == a:i2.lnum ? 0 : (a:i1.lnum < a:i2.lnum ? -1 : 1)
else
return bufname(a:i1.bufnr) < bufname(a:i2.bufnr) ? -1 : 1
endif
endfunction
function! s:SortUniqQFList()
let sortedList = sort(getqflist(), 's:CompareQuickfixEntries')
let uniqedList = []
let last = ''
for item in sortedList
let this = bufname(item.bufnr) . "\t" . item.lnum
if this !=# last
call add(uniqedList, item)
let last = this
endif
endfor
call setqflist(uniqedList)
endfunction
command! -nargs=0 UniqQFList call s:SortUniqQFList()
" fixes annoying horizontal scroll after escaping from fzf window
autocmd BufWinEnter,WinEnter * exe "normal ze"
" By default p over selected text overwrites unnamed register with the pasted
" text. This is annoying when you want to paste the same thing more than once.
" This fixes that. https://stackoverflow.com/a/5093286/51209
xnoremap <expr> p 'pgv"'.v:register.'y`>'
" Move by words in command line mode
" https://vi.stackexchange.com/a/31391/13743
cnoremap <C-h> <C-f>bb<C-c>
cnoremap <C-l> <C-f>ee<C-c>
function! CopyGitRange(line1, count, range, mods, arg, ...)
let url = substitute(execute(call('fugitive#BrowseCommand', [a:line1, a:count, a:range, 1, a:mods, a:arg] + a:000)), '^\n', '', '')
let lang = ExtToLang(expand('%:e'))
if a:range
let text = GetVisualText(a:range)
else
let text = getline('.')
endif
let @+ = "```" . lang . "\n" . text . "\n```\n\n" . url
endfunction
function! ExtToLang(ext)
if a:ext ==# 'mjs'
return 'js'
endif
return a:ext
endfunction
command! -bar -bang -range=-1 -nargs=* -complete=customlist,fugitive#CompleteObject GCopy call CopyGitRange(<line1>, <count>, +"<range>", "<mods>", <q-args>)
au FileType dap-repl lua require('dap.ext.autocompl').attach()
autocmd FileType html
\ setlocal formatprg=tidy\ -indent\ -quiet\ --show-errors\ 0\ --tidy-mark\ no\ --show-body-only\ auto
" For GBrowse
" TODO: support osx
command! -nargs=1 OpenBrowser exe '!xdg-open ' .. shellescape(fnameescape('<args>'))