-
Notifications
You must be signed in to change notification settings - Fork 1
/
vimrc
403 lines (318 loc) · 10.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
" .vimrc file
" Maintainer: Max Wang <[email protected]>
" URL: https://github.com/mxw/dotfiles
" Depends: pathogen, solarized
" Local sourcing.
if filereadable($HOME."/.vimrc.before")
source $HOME/.vimrc.before
endif
" vim >> vi.
set nocompatible
" Source pathogen.
runtime pathogen/autoload/pathogen.vim
" Call pathogen.
filetype off
call pathogen#infect()
" Set a mapleader.
let mapleader=','
""""""""""""""""""""""""""""""""""""""""""
" General
""""""""""""""""""""""""""""""""""""""""""
" Display.
set ruler " show cursor coordinates
set title " show filename
set nonumber " hide line numbers
set showcmd " show normal mode commands as they are entered
set showmode " show editing mode in status line
set showmatch " flash matching delimiters
set nolist " no metacharacters
set listchars=tab:>- " but show tabs if we turn it on
" Autocomplete.
set wildmode=longest,list,full
set wildmenu
set wildignore+=*.o,*.pyc,*.aux,*.cmi,*.cmo,*.cmx
set completeopt=menu,menuone,preview
" Search.
set nohlsearch " don't persist search highlighting
set incsearch " search with a typeahead
set magic " use (some) regexp special characters
set ignorecase " ignore case...
set smartcase " ...iff all characters are lower case
set infercase " case-sensitive completion
" Scrolling.
set wrap " wrap overlong lines
set scrolloff=0 " don't scroll unless necessary
set scrolljump=1 " scroll one line at a time
" Turn things off.
set nofoldenable " no folding
set mouse= " no mouse
set noerrorbells " no error bells
set vb t_vb= " no visual bells
" Backspace over everything.
set backspace=indent,eol,start
" Get rid of security holes.
set nomodeline
set modelines=0
""""""""""""""""""""""""""""""""""""""""""
" Syntax highlighting and indent
""""""""""""""""""""""""""""""""""""""""""
" Turn on syntax highlighting and enable filetype stuff.
syntax enable
filetype plugin indent on
" Use solarized for color.
set t_Co=16
set background=dark
colorscheme solarized
" Tab and indent.
set autoindent " carry indent over to new lines
set shiftwidth=2 " two spaces per indent
set tabstop=2 " number of spaces per tab when viewing
set softtabstop=2 " number of spaces per tab when inserting
set expandtab " sub spaces for tabs
set smarttab " make tab key obey indent rules specified above
set formatoptions+=ro " autoindent on `o` and <CR>
" Highlight trailing whitespace.
hi ExtraWhitespace ctermbg=red guibg=red
au ColorScheme * hi ExtraWhitespace guibg=red
au BufEnter * match ExtraWhitespace /\s\+$/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave * match ExtraWhiteSpace /\s\+$/
""""""""""""""""""""""""""""""""""""""""""
" Cache and backups
""""""""""""""""""""""""""""""""""""""""""
" Save marks for ' files, registers for " files, and : lines of history.
set viminfo='20,"20,:50
" History and undo caches.
set history=50 " not too much history
set undolevels=1000 " lots of undo!
" Keep backup junk out of cwd.
set directory=~/tmp/swp//,/tmp//,.
set backupdir=~/tmp/swp//,/tmp//,.
" Save cursor position for reopening.
au BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ endif
""""""""""""""""""""""""""""""""""""""""""
" Buffers and windows
""""""""""""""""""""""""""""""""""""""""""
" Settings.
set hidden " keep hidden buffers around
set autoread " automatically re-read modified files
set splitright " hsplit to the right
set splitbelow " vsplit to the left
set laststatus=2 " always show a status line
" Window navigation.
noremap <C-j> <C-w>j
noremap <C-k> <C-w>k
noremap <C-h> <C-w>h
noremap <C-l> <C-w>l
" Buffer navigation.
nnoremap <C-n> :bnext<CR>
nnoremap <C-p> :bprev<CR>
" Quickfix and preview windows.
nnoremap <leader>co :copen<CR>
nnoremap <leader>cc :cclose<CR>
nnoremap <leader>zz :pclose<CR>
""""""""""""""""""""""""""""""""""""""""""
" Maps
""""""""""""""""""""""""""""""""""""""""""
" Switch modes more easily.
nnoremap ; :
inoremap jj <Esc>
" Make Y behave more like other operators.
nnoremap Y y$
" Make Q formatting; replace Ex mode with <leader>q.
noremap Q gq
nnoremap <leader>q Q
" Replace <C-a> to accommodate screen escape character.
inoremap <C-z> <C-a>
nnoremap <leader>a <C-a>
nnoremap <leader>x <C-x>
" Move by display line rather than file line.
nnoremap j gj
nnoremap k gk
" Navigate completion menus more easily.
inoremap <C-j> <Down>
inoremap <C-k> <Up>
" Jump to matching delimiters more easily.
nnoremap <leader><Tab> %
vnoremap <leader><Tab> %
" Other useful leader maps.
nnoremap <leader>m :make<CR>
nnoremap <leader>l <C-l>
nnoremap <leader>v <C-w>v
" Refresh syntax highlighting.
nnoremap <leader><leader> :syntax sync fromstart<CR>
" Toggle spellchecking and paste.
nnoremap <leader>s :setl spell!<CR>:setl spell?<CR>
nnoremap <leader>p :setl paste!<CR>:setl paste?<CR>
nnoremap <leader>t :setl list!<CR>:setl list?<CR>
" Remove trailing whitespace.
nnoremap <silent> <leader>w :%s/\s\+$//<CR>:let @/=''<CR>''
" Convert filetype to unix.
nnoremap <leader>ff :e ++ff=dos<CR>:setlocal ff=unix<CR>
" Make <C-Right> and <C-Left> behave as in emacs.
noremap <ESC>[1;5C <C-Right>
noremap <ESC>[1;5D <C-Left>
noremap! <ESC>[1;5C <C-Right>
noremap! <ESC>[1;5D <C-Left>
" Command-line pseudo-emacs keybindings.
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-b> <C-Left>
cnoremap <C-f> <C-Right>
cnoremap <C-d> <Delete>
cnoremap <C-k> <C-\>estrpart(getcmdline(), 0, getcmdpos() - 1)<CR>
" Fewer fat fingers.
noremap ZZ <C-z>
" Only cabbrev actual commands (rather than also, say, search terms).
fu! SingleQuote(str)
return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
endfu
fu! Cabbrev(key, value)
exe printf('cabbrev <expr> %s (getcmdtype() == ":" && getcmdpos() <= %d) ? %s : %s',
\ a:key, 1+len(a:key), SingleQuote(a:value), SingleQuote(a:key))
endfu
""""""""""""""""""""""""""""""""""""""""""
" Tags
""""""""""""""""""""""""""""""""""""""""""
" Search up the directory tree for tags.
set tags=tags;/
" Use cscope along with ctags if it's available.
if has("cscope")
" Defer to ctags.
set cscopetagorder=1
" Use :cstag instead of :tag and friends (<C-]>, etc.).
set cscopetag
" Add cscope databases in `pwd` or in $CSCOPE_DB.
set nocscopeverbose
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set cscopeverbose
else
" Use :tjump behavior for :tag and friends even without cscope.
call Cabbrev('tag', 'tjump')
nnoremap <C-]> g<C-]>
vnoremap <C-]> g<C-]>
nnoremap <C-W>] <C-W>g<C-]>
endif
command! -nargs=1 -complete=tag Vstag vsp | tag <args>
call Cabbrev('vstag', 'Vstag')
""""""""""""""""""""""""""""""""""""""""""
" LSP
""""""""""""""""""""""""""""""""""""""""""
function! s:find_nearest_parent_dir(path, filename) abort
let l:candidates = map(a:filename, {idx ->
\ lsp#utils#find_nearest_parent_file_directory(a:path, a:filename[idx])
\ })
let l:sorted = sort(l:candidates, {l, r -> strlen(r) - strlen(l)})
return l:sorted[0]
endfunction
if executable('xcrun')
au User lsp_setup call lsp#register_server({
\ 'name': 'sourcekit-lsp',
\ 'cmd': {server_info -> [
\ 'xcrun',
\ 'sourcekit-lsp',
\ '-Xswiftc',
\ '-sdk',
\ '-Xswiftc',
\ trim(system('xcrun --sdk iphonesimulator --show-sdk-path')),
\ '-Xswiftc',
\ '-target',
\ '-Xswiftc',
\ join([
\ 'x86_64-apple-ios',
\ trim(system('xcrun --sdk iphonesimulator --show-sdk-version')),
\ '-simulator',
\], '')
\ ]},
\ 'root_uri': {server_info -> lsp#utils#path_to_uri(
\ s:find_nearest_parent_dir(
\ lsp#utils#get_buffer_path(),
\ ['Package.swift', '.git/']
\ )
\ )},
\ 'allowlist': ['swift'],
\ })
endif
function! s:on_lsp_buffer_enabled() abort
setl omnifunc=lsp#complete
setl signcolumn=number
if exists('+tagfunc') | setl tagfunc=lsp#tagfunc | endif
nmap <buffer> gd <plug>(lsp-definition)
nmap <buffer> gr <plug>(lsp-references)
nmap <buffer> gi <plug>(lsp-implementation)
nmap <buffer> gt <plug>(lsp-type-definition)
nmap <buffer> <leader>rn <plug>(lsp-rename)
nmap <buffer> g[ <Plug>(lsp-previous-diagnostic)
nmap <buffer> g] <Plug>(lsp-next-diagnostic)
nmap <buffer> K <plug>(lsp-hover)
" refer to doc to add more commands
endfunction
augroup lsp_install
au!
au User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END
let g:lsp_diagnostics_echo_cursor = 1
let g:lsp_diagnostics_echo_delay = 100
""""""""""""""""""""""""""""""""""""""""""
" Plugins
""""""""""""""""""""""""""""""""""""""""""
" Airline - Use powerline glyphs.
let g:airline_powerline_fonts=1
" NERDTree - Toggle buffer.
nnoremap <leader><Space> :NERDTreeToggle<CR><C-w>=
" NERDTree - Quit vim when all other windows have been closed.
au BufEnter *
\ if (winnr("$") == 1 && exists("b:NERDTreeType") &&
\ b:NERDTreeType == "primary") |
\ q |
\ endif
" Fugitive - Leader mappings.
nnoremap <leader>ge :Gedit<CR>
nnoremap <leader>gb :Gblame<CR>
nnoremap <leader>gc :Gcommit<CR>
nnoremap <leader>gd :Gdiff<CR>
nnoremap <leader>gg :Ggrep<Space>
nnoremap <leader>gl :Glog<CR><CR><CR>:copen<CR>
nnoremap <leader>gs :Gstatus<CR>
nnoremap <leader>gh :Gbrowse<CR>
" Mercenary - Leader mappings.
nnoremap <leader>hb :HGblame<CR>
nnoremap <leader>hd :HGdiff<CR>
""""""""""""""""""""""""""""""""""""""""""
" Miscellaneous
""""""""""""""""""""""""""""""""""""""""""
" Use skeletal template files.
au! BufNewFile * silent! 0r ~/.vim/skel/template.%:e
" Set language defaults.
let g:tex_flavor='latex'
let g:sql_type_default='mysql'
" Set maximum line length.
au FileType liquid,markdown,readme,tex,text setl tw=79
" Kill any trailing whitespace on save.
fu! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfu
au FileType c,cabal,cpp,haskell,javascript,ocaml,php,python,ruby,readme,tex,text
\ au BufWritePre <buffer>
\ :call <SID>StripTrailingWhitespaces()
""""""""""""""""""""""""""""""""""""""""""
" Vimscript
""""""""""""""""""""""""""""""""""""""""""
nnoremap <leader>n :echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')<CR>
""""""""""""""""""""""""""""""""""""""""""
" Epilogue
""""""""""""""""""""""""""""""""""""""""""
" Local sourcing.
if filereadable($HOME."/.vimrc.after")
source $HOME/.vimrc.after
endif