-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvimrc
385 lines (317 loc) · 9.42 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
" dotvim settings
"""""""""""""""""
" detect OS
let s:is_windows = has('win32') || has('win64')
let s:is_cygwin = has('win32unix')
let s:is_macvim = has('gui_macvim')
set completeopt=menu
" always use .vim
if s:is_windows
set runtimepath+=~/.vim
endif
if has('nvim')
set runtimepath+=~/.vim
endif
" dotvim settings
let s:settings = {}
let s:settings.colorscheme = 'jellybeans'
let s:settings.default_indent = 2
let g:autostrip = 1
" override defaults with the ones specified in g:dotvim_settings
for key in keys(s:settings)
if has_key(g:dotvim_settings, key)
let s:settings[key] = g:dotvim_settings[key]
endif
endfor
let s:cache_dir = '~/.vim/.cache'
function! s:get_cache_dir(suffix)
return resolve(expand(s:cache_dir . '/' . a:suffix))
endfunction
function! s:ensure_exists(path)
if !isdirectory(expand(a:path))
call mkdir(expand(a:path))
endif
endfunction
function! s:preserve_pos(command)
" preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" do the business:
execute a:command
" clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
" Editor setup
""""""""""""""
set nocompatible
syn on
set nu
set history=10000
set wildmenu
set smartcase
set autoindent
set expandtab
let &tabstop=s:settings.default_indent
let &softtabstop=0 " keep the same as tabstop
let &shiftwidth=s:settings.default_indent
set laststatus=2
set showmatch
set incsearch
set hlsearch
set ffs=unix,dos
set hidden
" note: the final ';' is important. tells it to search upwards for semicolon
set tags=.tags;
filetype plugin indent on
syntax enable
" make searches case-sensitive only if they contain upper-case characters
set ignorecase smartcase
" improve gui on mac
if s:is_macvim
set antialias
set guifont=Menlo:h14
endif
" persistent undo
if exists('+undofile')
set undofile
let &undodir = s:get_cache_dir('undo')
call s:ensure_exists(&undodir)
endif
" backups
set backup
let &backupdir = s:get_cache_dir('backup')
" swap files
let &directory = s:get_cache_dir('swap')
set noswapfile
call s:ensure_exists(s:cache_dir)
call s:ensure_exists(&backupdir)
call s:ensure_exists(&directory)
" Plugins
"""""""""
" Install vim-plug
let s:first_install = 0
if !isdirectory(expand('~/.vim/plugged'))
call mkdir(expand('~/.vim/plugged'))
let s:first_install = 1
endif
if !filereadable(expand('~/.vim/autoload/plug.vim'))
echo "Installing vim-plug..."
exe 'silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
endif
call plug#begin('~/.vim/plugged')
" languages
Plug 'sheerun/vim-polyglot'
" special language support
Plug 'sheerun/vim-go'
" yank history
"(disable excessive mappings)
let g:yankring_enabled = 1
Plug 'vim-scripts/YankRing.vim'
" statusline and tabline
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
let g:airline#extensions#tabline#enabled = 1
" commenting
Plug 'scrooloose/nerdcommenter'
let $CARGO_TARGET_DIR = 'target/analyze/'
" auto complete!
if has('nvim')
Plug 'neovim/nvim-lspconfig'
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope-fzf-native.nvim', { 'do': 'make' }
Plug 'nvim-telescope/telescope.nvim', { 'tag': '0.1.*' }
Plug 'folke/trouble.nvim'
Plug 'kyazdani42/nvim-web-devicons'
Plug 'folke/lsp-colors.nvim'
" LSP source for nvim-cmp
Plug 'hrsh7th/cmp-nvim-lsp'
" Autocompletion plugin
Plug 'hrsh7th/nvim-cmp'
" adds extra signature info
Plug 'hrsh7th/cmp-nvim-lsp-signature-help'
" Snippets plugin
Plug 'L3MON4D3/LuaSnip', {'tag': 'v1.*'}
" Snippets source for nvim-cmp
Plug 'saadparwaiz1/cmp_luasnip'
else
Plug '~/.fzf'
Plug 'junegunn/fzf.vim'
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h | %<(20,trunc)%an | %s"'
if has('lua')
Plug 'Shougo/neocomplete.vim'
let g:neocomplete#enable_at_startup=1
let g:neocomplete#data_directory=s:get_cache_dir('neocomplete')
let g:neocomplete#sources#syntax#min_keyword_length = 3
endif
endif
" allows closing buffer w/o closing window!
Plug 'rgarver/Kwbd.vim'
" color schemes
Plug 'nanotech/jellybeans.vim'
" linting / syntax checking
Plug 'google/vim-codefmt'
" git
Plug 'tpope/vim-fugitive'
" file browsing
Plug 'scrooloose/nerdtree', {'on':['NERDTreeToggle','NERDTreeFind']}
let NERDTreeShowHidden=1
let NERDTreeQuitOnOpen=0
let NERDTreeShowLineNumbers=0
let NERDTreeChDirMode=0
let NERDTreeShowBookmarks=1
let NERDTreeIgnore=['\.git','\.hg', '\.\.$', '\.$', '\~$']
let NERDTreeBookmarksFile=s:get_cache_dir('NERDTreeBookmarks')
Plug 'Xuyuanp/nerdtree-git-plugin'
" vimscript plugin library
Plug 'google/vim-maktaba'
" finish vim-plug
call plug#end()
if s:first_install
exec 'PlugInstall'
endif
" Shortcuts
"""""""""""""""""
" formatting
nnoremap <buffer><Leader>cf :FormatCode<CR>
vnoremap <buffer><Leader>cf :FormatLines<CR>
" fuzzy-searching
if has('nvim')
map <leader>t :Telescope git_files<cr>
map <leader>T :Telescope find_files<cr>
map <leader>r :Telescope lsp_dynamic_workspace_symbols<cr>
map <leader>R :Telescope lsp_document_symbols<cr>
map <leader>G :Telescope live_grep<cr>
else
map <leader>t :GFiles<cr>
map <leader>T :Files<cr>
map <leader>r :Tags<cr>
map <leader>R :BTags<cr>
endif
" open dir tree
nnoremap <leader>o :NERDTreeToggle<CR>
" open dir tree to current file
nnoremap <leader>O :NERDTreeFind<CR>
" misc
nnoremap <leader>P :YRShow<CR>
map <leader>q <Plug>Kwbd
" Custom functions
""""""""""""""""""
" :Rg
" Searching through file contents using `rg` with fzf for fuzzy-searchign
" through results
"
" --column: Show column number
" --line-number: Show line number
" --no-heading: Do not show file headings in results
" --fixed-strings: Search term as a literal string
" --ignore-case: Case insensitive search
" --no-ignore: Do not respect .gitignore, etc...
" --hidden: Search hidden files and folders
" --follow: Follow symlinks
" --glob: Additional conditions for search (in this case ignore everything in the .git/ folder)
" --color: Search color options
command! -bang -nargs=* Rg
\ call fzf#vim#grep(
\ 'rg --column --line-number --no-heading --color=always '.shellescape(<q-args>), 1,
\ <bang>0 ? fzf#vim#with_preview('up:60%')
\ : fzf#vim#with_preview('right:50%:hidden', '?'),
\ <bang>0)
function! StripTrailingWhitespace()
call s:preserve_pos("%s/\\s\\+$//e")
endfunction
function! MaybeStripTrailingWhitespace()
if g:autostrip
call StripTrailingWhitespace()
endif
endfunction()
function! SwitchToTest()
let dest = ""
if expand("%") =~ "_test\\.cpp"
let dest = substitute(expand("%"), "_test\\.cpp$", ".cpp", "")
elseif expand("%") =~ "\\.cpp$"
let dest = substitute(expand("%"), "\\.cpp$", "_test.cpp", "")
elseif expand("%") =~ "\\.h$"
let dest = substitute(expand("%"), "\\.h$", "_test.cpp", "")
elseif expand("%") =~ "_test\\.go$"
let dest = substitute(expand("%"), "_test\\.go$", ".go", "")
elseif expand("%") =~ "\\.go$"
let dest = substitute(expand("%"), "\\.go$", "_test.go", "")
endif
if dest != ""
execute "edit " . dest
endif
endfunction()
function! SwitchToHeader()
let dest = ""
if expand("%") =~ "\\.cpp$"
let dest = substitute(expand("%"), "\\.cpp$", ".h", "")
elseif expand("%") =~ "\\.c$"
let dest = substitute(expand("%"), "\\.c$", ".h", "")
execute "edit " . dest
elseif expand("%") =~ "\\.cc$"
let dest = substitute(expand("%"), "\\.cc$", ".h", "")
execute "edit " . dest
elseif expand("%") =~ "\.h"
let dest = substitute(expand("%"), "\\.h$", ".c", "")
if !filereadable(dest)
let dest = substitute(expand("%"), "\\.h$", ".cc", "")
endif
if !filereadable(dest)
let dest = substitute(expand("%"), "\\.h$", ".cpp", "")
endif
execute "edit " . dest
endif
if dest != ""
execute "edit " . dest
endif
endfunction()
function! AddHeaderGuard()
let guard = substitute(expand('%:t:r'), '\(\u*\l\+\)', '\1_', 'g')
let guard = substitute(guard, '\(\u\{2,\}\)\(\u\)', '\1_\2', 'g')
let guard = toupper(guard . 'h')
call append(line('^'), [ '#ifndef ' . guard, '#define ' . guard, '', '' ])
call append(line('$'), [ '#endif' ])
normal k
endfunction
" switch to header
nnoremap <leader>h :call SwitchToHeader()<cr>
" switch to test file
nnoremap <leader>H :call SwitchToTest()<cr>
" re-map to jump to tag definition
autocmd FileType * map <leader>g <c-]><cr>
autocmd FileType go map <leader>g :GoDef<cr>
" formatting shortcuts
nmap <leader>f$ :call StripTrailingWhitespace()<CR>
vmap <leader>s :sort<cr>
nnoremap <leader>w :w<cr>
map <leader>ig :call AddHeaderGuard()<cr>
vmap <leader>kf :FormatLines<cr>
nmap <leader>kf :FormatCode<cr>
map <leader>e :LspNextError<cr>
map <leader>E :LspPreviousError<cr>
" autocmd
"""""""""
autocmd BufRead,BufNewFile *.rs set filetype=rust
" auto strip trailing whitespace on save
autocmd BufWritePre * call MaybeStripTrailingWhitespace()
" change indent settings per file type
autocmd FileType ruby,haml,eruby,yaml,html,javascript,sass,cucumber setl ts=2 sw=2
autocmd FileType python,c,cpp,java setl ts=4 sw=4
" Enable spell check for git commit messages
autocmd FileType gitcommit setlocal spell
" I often typo these...
command Qa :qa
command Wq :wq
" setup colorscheme
if has_key(s:settings, 'colorscheme')
exec 'colorscheme '.s:settings.colorscheme
endif
lua << EOF
require("init")
EOF
" Credit:
" * https://github.com/bling/dotvim
" * https://github.com/sdball/dotfiles/