-
Notifications
You must be signed in to change notification settings - Fork 2
/
.vimrc
482 lines (388 loc) · 11.4 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
" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
" load plugins
if !empty(glob('~/.vimrc.plugin'))
source ~/.vimrc.plugin
endif
" filetype plugins
filetype plugin indent on
augroup vimrc
autocmd!
augroup END
" syntax hightlighting
syntax on
" syntax coloring lines that are too long just slows down the world
" set synmaxcol=256
" set mapleader
let mapleader=" "
let maplocalleader = " "
" hides buffers instead of closing them
set hidden
" do not reset position when switching buffers
set nostartofline
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
" keep 200 lines of command line history
set history=200
" show the cursor position all the time
set ruler
" display incomplete commands
set showcmd
" redraw lazy
set lazyredraw
" indent wrapped lines
set breakindent
set breakindentopt=shift:2
" tty scroll
if !has('nvim')
set ttyscroll=3
endif
" live substitution preview
set inccommand=nosplit
" set linenumbers
set number
" don't automagically write on :next
set noautowrite
" do incremental searching
set incsearch
" hightlight all search matches
set hlsearch
" show matching bracket
set showmatch
" we have a fast terminal
set ttyfast
" use the OS clipboard
set clipboard=unnamedplus
" auto. reread file when changed outside of vim
set ar
" ask to save files when closing vim
set confirm
" case insensitive, but be smart
set ignorecase
set smartcase
if ! has('gui_running')
" leave insert mode quickly
set ttimeoutlen=0
augroup FastEscape
autocmd!
augroup END
autocmd FastEscape InsertEnter * set timeoutlen=0
autocmd FastEscape InsertLeave * set timeoutlen=1000
endif
" keep lines before/beneath cursor
set scrolloff=5
set sidescrolloff=5
" change directory to the current buffer when opening files.
" set autochdir
autocmd vimrc BufEnter * silent! lcd %:p:h
" statusline show last line
set laststatus=2
" dont show mode in second line
set noshowmode
" avoid all hit-enter prompts and shorten some other info prompts
set shortmess=aoOtI
" backspace delete shiftwidth of spaces
set smarttab
" colordepth
set t_Co=256
" tell vim to keep a backup file
set backup
" tagfiles
set tags=./tags;
" prevent backups from overwriting each other. appends reversed path to the
" backup file.
au BufWritePre * let &backupext = '@'.substitute(expand('%:p:h'), '/', '%', 'g').'~'
" Remember info about open buffers on close
" set viminfo^=%
" adjust vim file history https://vi.stackexchange.com/a/26037
set viminfo='10000,<50,s10,h
" tell vim where to put its backup files
set backupdir=~/.vim/tmp
" tell vim where to put swap files
set directory=~/.vim/swp
" disable swap files
" set noswapfile
" persistent undo history across sessions
set undofile
set undodir=~/.vim/undo
" Make those folders automatically if they don't already exist.
if !isdirectory(expand(&undodir))
call mkdir(expand(&undodir), "p")
endif
if !isdirectory(expand(&backupdir))
call mkdir(expand(&backupdir), "p")
endif
if !isdirectory(expand(&directory))
call mkdir(expand(&directory), "p")
endif
" complete options
set completeopt+=menuone
set completeopt-=preview
set pumheight=8
" formatoptions
autocmd vimrc FileType * setlocal formatoptions=crqnbj
" 8 spaces are bad, use 4 and auto expand them
set expandtab
set tabstop=4
set shiftwidth=4
" always round indents to multiple of shiftwidth
set shiftround
" Turn on the WiLd menu
set wildmenu
set wildignore=*.o,*.d,*~,*.pyc,*.class,target/**
set wildmode=longest,list,full
set wildignorecase
" show invisible chars
set listchars=tab:»·,trail:·,extends:…
set list
" % to also switch between begin/end
runtime macros/matchit.vim
" highlight extra whitespaces
highlight ExtraWhitespace ctermbg=darkgreen guibg=darkgreen
autocmd vimrc ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+\%#\@<!$/
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
set mouse=a
endif
" For all text files set 'textwidth'
set textwidth=0
set wrapmargin=0
" ========colors/appearance
if !empty(glob("~/.vimrc.colors"))
source ~/.vimrc.colors
else
set background=dark
endif
" " use background color of terminal
" hi Normal ctermbg=none
" hi NonText ctermbg=none
" only enable cursorline in focused window
set cul
autocmd vimrc WinEnter * set cul
autocmd vimrc WinLeave * set nocul
autocmd vimrc BufEnter * set cul
autocmd vimrc BufLeave * set nocul
" make gvim look like vim :)
if has("gui_running")
set guifont=Monospace\ 11
hi Normal guifg=#dcdccc guibg=#3f3f3f
set guioptions-=m " Remove menu
set guioptions-=T " Remove toolbar
set guioptions-=r " Remove right scrollbar
set guioptions-=e " Use curses rendering for tab pages
set guioptions+=c " Use curses for simple dialog boxes
set guioptions-=i " No vim icon
set guioptions-=L " No left scrollbar
endif
" ========commands/functions
"spelling error
command! W w
command! Q q
command! Wq wq
command! WQ wq
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
command! DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis
" fix all whitespaces
command! WsFix call WhitespaceFix()
function! WhitespaceFix()
if !&binary && &filetype != 'diff'
let l:winview = winsaveview()
silent! %s/\s\+$//
call winrestview(l:winview)
endif
endfunction
" from: http://stackoverflow.com/questions/18157501/toggle-semicolon-or-other-character-at-end-of-line
function! s:ToggleEndChar(charToMatch)
let save_cursor = getpos(".") " backup cursor
s/\v(.)$/\=submatch(1)==a:charToMatch ? '' : submatch(1).a:charToMatch
call setpos('.', save_cursor) " restore cursor
silent! call repeat#set("\<Plug>ToggleEndChar".a:charToMatch, -1)
endfunction
" noremap! <unique> <Plug>ToggleEndChar; :call <SID>ToggleEndChar(';')<CR>
" noremap! <unique> <Plug>ToggleEndChar, :call <SID>ToggleEndChar(',')<CR>
" toggles the quickfix and location window.
command! Qtoggle call QFixToggle(0)
command! Ltoggle call QFixToggle(1)
function! QFixToggle(loc)
for i in range(1, winnr('$'))
let bnum = winbufnr(i)
if getbufvar(bnum, '&buftype') == 'quickfix'
lclose
cclose
return
endif
endfor
if a:loc
lopen
else
copen
endif
endfunction
" Y yanks till end of line
nnoremap Y y$
" toggle side effects on yank register when deleting/modifying text
function! DisableSideEffects()
noremap dd "_dd
noremap D "_D
noremap d "_d
noremap X "_X
noremap x "_x
noremap x "_x
vnoremap p "_dP
unmap <leader>p
nnoremap <leader>p v$h"_dP
endfunction
function! EnableSideEffects()
unmap dd
unmap D
unmap d
unmap X
unmap x
vunmap p
unmap <leader>p
nnoremap <leader>p v$hp
endfunction
function! ToggleSideEffects()
if mapcheck("dd", "n") == ""
call DisableSideEffects()
echo 'side effects off'
else
call EnableSideEffects()
echo 'side effects on'
endif
endfunction
" paste over rest of line
nnoremap <leader>p v$hp
" open commands for file lists
command! -complete=file -nargs=* Etabe call s:ETW('tabnew', <f-args>)
command! -complete=file -nargs=* Enew call s:ETW('new', <f-args>)
command! -complete=file -nargs=* Evnew call s:ETW('vnew', <f-args>)
command! -complete=file -nargs=* E call s:ETW('edit', <f-args>)
function! s:ETW(what, ...)
if empty(a:000)
edit
return
endif
for f1 in a:000
let files = glob(f1)
if files == ''
execute a:what . ' ' . escape(f1, '\ "')
else
for f2 in split(files, "\n")
execute a:what . ' ' . escape(f2, '\ "')
endfor
endif
endfor
endfunction
" return to last edit position when opening a file.
" except for git commits: Enter insert mode instead.
autocmd vimrc BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ if &filetype == 'gitcommit' |
\ setlocal spell |
\ else |
\ exe "normal! g`\"" |
\ endif |
\ endif
" ========keybindings
" MapFastKeycode: helper for fast keycode mappings
" makes use of unused vim keycodes <[S-]F15> to <[S-]F37>
function! <SID>MapFastKeycode(key, keycode)
if s:fast_i == 46
echohl WarningMsg
echomsg "Unable to map ".a:key.": out of spare keycodes"
echohl None
return
endif
let vkeycode = '<'.(s:fast_i/23==0 ? '' : 'S-').'F'.(15+s:fast_i%23).'>'
exec 'set '.vkeycode.'='.a:keycode
exec 'map '.vkeycode.' '.a:key
exec 'imap '.vkeycode.' '.a:key
let s:fast_i += 1
endfunction
let s:fast_i = 0
call <SID>MapFastKeycode('<S-Tab>', "\e}")
call <SID>MapFastKeycode('<C-Tab>', "\e{")
call <SID>MapFastKeycode('<M-q>', "\eq")
call <SID>MapFastKeycode('<M-h>', "\eh")
call <SID>MapFastKeycode('<M-j>', "\ej")
call <SID>MapFastKeycode('<M-k>', "\ek")
call <SID>MapFastKeycode('<M-l>', "\el")
call <SID>MapFastKeycode('<M-d>', "\ed")
call <SID>MapFastKeycode('<M-n>', "\en")
" toggle sideffects
nnoremap <leader><space> :call ToggleSideEffects()<CR>
" clear search highlighting
nnoremap <leader>/ :nohlsearch<CR>:echo<CR>
" save some keystrokes
nnoremap ; :
" nnoremap , ;
nnoremap \ :update<CR>
nnoremap <leader>\ :w suda://%<CR>:checktime<CR>
nnoremap + qm
nnoremap - @m
"w!! writes file with root rights
" cnoremap w!! w !sudo tee % >/dev/null
" break undo in insert mode
inoremap <C-U> <C-G>u
" switch buffers/tabs
nnoremap <Backspace> :bprevious<CR>
nnoremap <Enter> :bnext<CR>
nnoremap <C-q> <C-^>
nnoremap Q :bdelete<CR>
nnoremap gqq <S-v>gq
" switch to numbered buffer
nnoremap <Leader>1 :1b<CR>
nnoremap <Leader>2 :2b<CR>
nnoremap <Leader>3 :3b<CR>
nnoremap <Leader>4 :4b<CR>
nnoremap <Leader>5 :5b<CR>
nnoremap <Leader>6 :6b<CR>
nnoremap <Leader>7 :7b<CR>
nnoremap <Leader>8 :8b<CR>
nnoremap <Leader>9 :9b<CR>
nnoremap <Leader>0 :10b<CR>
" easier split window navigation
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
" keep selection when indenting
vnoremap < <gv
vnoremap > >gv
" overwrite :e with :E
cabbrev e <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'E' : 'e')<CR>
" delete everything but selection
vnoremap <leader>x :<C-U>1,'<-1:delete<CR>:'>+1,$:delete<CR>
nnoremap <leader>x <S-v>:<C-U>1,'<-1:delete<CR>:'>+1,$:delete<CR>
" fast way to edit ~/.vimrc*
nnoremap <Leader>vv :e ~/.vimrc<CR>
nnoremap <Leader>vp :e ~/.vimrc.plugin<CR>
nnoremap <Leader>vl :e ~/.vimrc.local<CR>
nnoremap <Leader>vz :e ~/.zshrc<CR>
nnoremap <Leader>vzl :e ~/.zshrc.local<CR>
nnoremap <Leader>va :e ~/.zaliases<CR>
nnoremap <Leader>vr :source ~/.vimrc<CR>
" toggle chars at end of line
nmap <silent> <Leader>< <Plug>ToggleEndChar;
nmap <silent> <Leader>, <Plug>ToggleEndChar,
" kind of reverse of J
" nmap K i<CR><ESC>
" c+s corrects the last word
inoremap <c-s> <c-g>u<Esc>[s1z=`]a<c-g>u
nnoremap <c-s> 1z=
" Smart Home:
" https://vi.stackexchange.com/a/23206
noremap <expr> <Home> col('.') == match(getline('.'), '\S') + 1 ? "\<Home>" : "^"
inoremap <expr> <Home> col('.') == match(getline('.'), '\S') + 1 ? "\<Home>" : "\<C-O>^"
noremap <expr> 0 col('.') == match(getline('.'), '\S') + 1 ? "\<Home>" : "^"
" inoremap <expr> 0 col('.') == match(getline('.'), '\S') + 1 ? "\<Home>" : "\<C-O>^"
autocmd vimrc BufEnter * set noreadonly " no delay
" local settings
if !empty(glob("~/.vimrc.local"))
source ~/.vimrc.local
endif