-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.vimrc
451 lines (367 loc) · 15.7 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
" .vimrc
" Load FB-specific stuff
if filereadable(glob("~/.fb.vimrc"))
source ~/.fb.vimrc
endif
" vim-plug.
" To install, run `:PlugInstall` and then `:PlugUpdate`.
" To remove, run `:PlugClean`.
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree'
Plug 'christoomey/vim-tmux-navigator'
Plug 'ConradIrwin/vim-bracketed-paste'
Plug 'rhysd/vim-clang-format'
Plug 'tpope/vim-fugitive' " Git wrapper
Plug 'tpope/vim-rhubarb' " Github extension for vim-fugitive (:GBrowse)
Plug 'jlfwong/vim-mercenary' " Like vim-fugitive but for hg
Plug 'sbdchd/neoformat'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'fs111/pydoc.vim' " Python documentation (Shift+K for word under cursor)
Plug 'godlygeek/tabular' " Dependency for vim-markdown
Plug 'plasticboy/vim-markdown' " Markdown folds
" bullets.vim messes up markdown files in vscode. Disable in favor of vscode
" native extension.
" Plug 'dkarter/bullets.vim' " Auto lists and checkboxes/todo-lists
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
call plug#end()
let mapleader=","
" Split
set splitbelow " Horizional split to the bottom
set splitright " Vertical split to the right
" Indent
set autoindent " Carry indent over to new lines
set breakindent " Indent wrapped lines
set expandtab " Expand tab to spaces
set tabstop=2 " Replace tabs with 2 spaces
set shiftwidth=2 " Use 2 spaces when inserting tabs
" Scroll
set scrolljump=1 " Num lines to scroll up/down when going off screen with j/k
set scrolloff=3 " Num lines of context to show when scrolling up/down
set sidescroll=10 " Num columns to scroll left/right when going off screen
set sidescrolloff=5 " Num columns of context to show when scrolling left/right
" Search
set hlsearch " Highlight search hits
set incsearch " Enable incremental search
set ignorecase " Case-insensitive search
set smartcase " Except if the query has uppercase (doesn't apply for * command)
" Display
set cursorline " Highlight current line
set ruler " Display line and column number of the cursor
set nonumber " Hide line numbers
set nolist " Hide tabs and EOL chars
set showcmd " Show normal mode commands as they are entered
set showmode " Show editing mode in status (-- INSERT --)
set showmatch " Flash matching brackets on insertion
set matchtime=4 " Duration for set showmatch (in units of 100 ms)
set background=dark " Dark mode
set termguicolors " Enable 24-bit true colors
colorscheme desert
" Misc
set belloff=all " Silence bell for all events
set backspace=indent,eol,start " Backspace over everything
set undolevels=1000 " Number of undos stored
set clipboard=unnamed " Use system clipboard
set hidden " Allow hidden buffers
set mouse=a " Enable mouse support in all modes
set shortmess+=a " Abbreviate error messages
set shortmess+=A " Ignore warning when swap file exists
""""""""""""""""""""""""""""""""""""""""""
" Filetype plugins
""""""""""""""""""""""""""""""""""""""""""
filetype indent plugin on
syntax enable
augroup additional_filetypes
autocmd!
autocmd BufNewFile,BufRead *.cuh set filetype=cuda
autocmd BufNewFile,BufRead TARGETS set filetype=python
augroup END
""""""""""""""""""""""""""""""""""""""""""
" Util functions and custom commands
""""""""""""""""""""""""""""""""""""""""""
" Like windo but restore the current window.
" https://vim.fandom.com/wiki/Run_a_command_in_multiple_buffers.
function! WinDo(command)
let currwin=winnr()
execute 'windo ' . a:command
execute currwin . 'wincmd w'
endfunction
command! -nargs=+ -complete=command Windo call WinDo(<q-args>)
" Like bufdo but restore the current buffer.
" https://vim.fandom.com/wiki/Run_a_command_in_multiple_buffers.
function! BufDo(command)
let currBuff=bufnr("%")
execute 'bufdo ' . a:command
execute 'buffer ' . currBuff
endfunction
command! -nargs=+ -complete=command Bufdo call BufDo(<q-args>)
" Like tabdo but restore the current tab.
" https://vim.fandom.com/wiki/Run_a_command_in_multiple_buffers.
function! TabDo(command)
let currTab=tabpagenr()
execute 'tabdo ' . a:command
execute 'tabn ' . currTab
endfunction
command! -nargs=+ -complete=command Tabdo call TabDo(<q-args>)
" Return the previously searched string from the search register '/'. Could be
" used as an argument to fzf :Rg for instance (see mapping for <leader>s).
" Taken from ack.vim ack#AckFromSearch() - https://github.com/mileszs/ack.vim/blob/master/autoload/ack.vim
function! GetSearchRegister(ignore_word_boundary)
" Get the last search term from the register '/'
let search = getreg('/')
if a:ignore_word_boundary
" Biggrep doesn't handle vim's word boundary characters \< and \> well - strip them
let search = substitute(search, '\(\\<\|\\>\)', '', 'g')
else
" Translate vim regex to perl regex for rg - replace \< and \> word boundary
" characters with \b
let search = substitute(search, '\(\\<\|\\>\)', '\\b', 'g')
endif
return search
endfunction
" Clear the search register '/' to remove the last search term.
" See mapping for <leader>/ and associated comments.
function! ClearSearchRegister()
call setreg('/', [])
endfunction
" Set pdb trace
function! InsertPdb()
let trace = expand("import pdb; pdb.set_trace() # yapf: disable TODO slog")
execute "normal o".trace
endfunction
" Print phabricator URL for the current file. Works both in normal mode
" as well as with range selection in visual mode.
" TODO: make this universal - work with :GBrowse with clipboard support
function! GetPhabricatorURL() range
" Get current filename and any highlighted line number or range
let filename = expand( "%:f" )
let lineno = line('.')
if a:lastline - a:firstline > 0
let lineno = a:firstline . "-" . a:lastline
endif
" Get phabricator url via diffusion command and print
let url = trim(system("diffusion " . filename . ":" . lineno))
" Put the url in clipboard so it can be pasted into chrome
call setreg('+', url)
" Print the url
echom url
endfunction
" Replace a line of space-separated text into individual lines
function! SplitToLines() range
for lnum in range(a:lastline, a:firstline, -1)
let words = split(getline(lnum))
execute lnum . "delete"
call append(lnum-1, words)
endfor
endfunction
" Kill any trailing whitespace
function! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfunction
""""""""""""""""""""""""""""""""""""""""""
" Custom Keymaps
""""""""""""""""""""""""""""""""""""""""""
inoremap jj <ESC>
" Vim tabs
nnoremap tn :tabnew<CR>
nnoremap td :tabclose<CR>
nnoremap th :tabprev<CR>
nnoremap tl :tabnext<CR>
nnoremap tg :tabfirst<CR>
nnoremap te :tablast<CR>
nnoremap tt :tabedit<Space>
nnoremap tm :tabmove<Space>
nnoremap ts :tabs
" Vim windows
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
nnoremap <C-k> <C-w>k
nnoremap <C-j> <C-w>j
" Move to the first / leftmost window
nnoremap <C-g> <C-w>1w
" Move to the last / rightmost window
nnoremap <C-e> <C-w>100w
" Reload all tabs. We use our custom `Tabdo` instead of `tabdo`
" to restore the current tab and window, and `silent!` to suppress
" errors related to quickfix and nerdtree buffers. To only reload
" windows within the current tab, use `Windo`.
" https://vim.fandom.com/wiki/Run_a_command_in_multiple_buffers.
nnoremap <leader>r :silent! Tabdo e<CR>
" Vim folds
" Default fold commands start with z. Remap to f to avoid Emacs pinky.
map fo zo " Open fold under cursor
map fO zO " Open fold under cursor (recursive)
map fc zc " Close fold under cursor
map fC zC " Close fold under cursor (recursive)
map ft za " Toggle fold under cursor
map fT zA " Toggle fold under cursor (recursive)
map fl zm " Show less - fold one level more
map fL zM " Fold all
map fm zr " Show more - unfold one more level
map fM zR " Show/unfold all
map ff zM zv " Focus - fold all but the one under cursor
map fgg [z " Move to the start of the current fold
map fG ]z " Move to the end of the current fold
map fj zj " Move to the start of the next fold
map fk zk " Move to the end of the previous fold
" <leader>w to toggle line wrap
nnoremap <leader>w :set wrap!<CR>
" TODO: restructure the file
" Avoid jumping to the next match when using * to highlight word under the cursor.
" https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump
nnoremap * *``
nnoremap * :keepjumps normal! mi*`i<CR>
" Set pdb trace on <leader>b for python files
augroup pdb
autocmd!
autocmd FileType python map <leader>b :call InsertPdb()<CR>
augroup END
" Map <leader>l to print phabricator URL for current file. Works with visual
" ranges too.
map <leader>l :call GetPhabricatorURL()<CR>
""""""""""""""""""""""""""""""""""""""""""
" Plugins
""""""""""""""""""""""""""""""""""""""""""
" NERDTree
nnoremap <leader>nt :NERDTreeToggle<CR>
nnoremap <leader>nf :NERDTreeFind<CR>
" Remap some keys for consistency between plugins
let g:NERDTreeMapOpenVSplit = 'v' " Vertical split
let g:NERDTreeMapPreviewVSplit = 'gv' " Vertical split (maintain focus in NERDTree)
let g:NERDTreeMapOpenSplit = 's' " Horizontal split
let g:NERDTreeMapPreviewSplit = 'gs' " Horizontal split (maintain focus in NERDTree)
" Change vim CWD together with NERDTree's root dir
let g:NERDTreeChDirMode = 2
augroup nerdtree
autocmd!
" Focus cursor in new document on startup
autocmd VimEnter * wincmd p
" Close vim if the only window left open is a NERDTree
autocmd BufEnter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" Auto-create a NERDTree mirror in every vim tab (filtering out quickfix)
autocmd BufWinEnter * if &buftype != 'quickfix' | NERDTreeMirror | endif
" Uncomment to open NERDTree automatically when vim starts up
" autocmd VimEnter * NERDTree
augroup END
" fzf.vim
" Keymaps to open files in splits from fzf window
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-s': 'split',
\ 'ctrl-v': 'vsplit' }
" <leader>s to search file contents (using ripgrep underneath) and
" <leader>f for filenames (using fd if available and find otherwise)
nnoremap <leader>s :Rg <C-R>=GetSearchRegister(0)<CR>
nnoremap <leader>f :Files<CR>
" <leader>cs for xbgs and <leader>cf for xbgf to search in fbsource
command! -bang -nargs=* Xbgs
\ call fzf#vim#grep(
\ 'xbgs --color=on --ignore-case --forcedir "$(hg root)" '.shellescape(<q-args>),
\ 1,
\ fzf#vim#with_preview(),
\ <bang>0
\ )
command! -bang -nargs=* Xbgf
\ call fzf#vim#grep(
\ 'xbgf --color=on --ignore-case --forcedir "$(hg root)" '.<q-args>,
\ 1,
\ fzf#vim#with_preview(),
\ <bang>0
\ )
nnoremap <leader>cs :Xbgs <C-R>=GetSearchRegister(1)<CR>
nnoremap <leader>cf :Xbgf<Space>
" Map <leader>/ to clear the search register '/'. This avoids needing to type
" /asdf to clear search highlight. More importantly, we rely on this register
" to get the last search term to pass into fzf :Rg command (see mapping for
" <leader>s). We need a mechanism to clear this so that <leader>s outputs
" ':Rg ' (instead of ':Rg asdf') to be able to enter a fresh search term.
" Also do this automatically when vim starts up.
nnoremap <silent> <leader>/ :call ClearSearchRegister()<CR>
augroup fzf
autocmd!
autocmd VimEnter * :call ClearSearchRegister()
augroup END
" <leader>t to list open files in vim tabs and windows and quickly switch
nnoremap <leader>t :Windows<CR>
" <leader>gs for git status and list modified files in fzf in the current repo.
" Also display diff for each modified file in fzf preview.
nnoremap <leader>gs :GFiles?<CR>
" <leader>gll to show git log for the current repo in fzf.
" Also display commit diff in fzf preview.
nnoremap <leader>gll :Commits<CR>
" <leader>gl. to show git log for the current file in fzf.
" Also display each commit diff in fzf preview.
nnoremap <leader>gl. :BCommits<CR>
" vim-fugitive, vim-rhubarb, vim-mercenary
map <leader>gh :GBrowse<CR>
map <leader>gb :Git blame<CR>
map <leader>hb :HGblame<CR>
" pydoc.vim - https://github.com/fs111/pydoc.vim/blob/master/ftplugin/python_pydoc.vim
let g:pydoc_window_lines=0.3 " 30% of current window
" vim-markdown - https://github.com/plasticboy/vim-markdown
let g:vim_markdown_new_list_item_indent = 0 " Number of indent spaces on new list item
let g:vim_markdown_toc_autofit = 1 " Autofit Table of Contents (ToC) window
let g:vim_markdown_math = 1 " LaTeX extension on
let g:vim_markdown_conceal_code_blocks = 0 " Don't conceal code-blocks
augroup vim_markdown_folds
autocmd!
" Keymaps
autocmd FileType markdown map fj <Plug>Markdown_MoveToNextHeader
autocmd FileType markdown map fk <Plug>Markdown_MoveToPreviousHeader
autocmd FileType markdown map fJ <Plug>Markdown_MoveToNextSiblingHeader
autocmd FileType markdown map fK <Plug>Markdown_MoveToPreviousSiblingHeader
autocmd FileType markdown map fp <Plug>Markdown_MoveToParentHeader
autocmd FileType markdown map fi :Toc<CR> " Show Table of Contents
autocmd FileType markdown map f> :HeaderIncrease<CR> " Increase level of all or selected headers
autocmd FileType markdown map f< :HeaderDecrease<CR> " Decrease level of all or selected headers
" TODO: Unify url opening command for GBrowse, fb url, markdown preview, etc.
autocmd FileType markdown map fl <Plug>MarkdownPreview " Open markdown file in browser
" Automatically show table of contents for markdown files
" TODO: fix this
" autocmd FileType markdown autocmd BufReadPost,FileReadPost * :Toc
" Enable conceal for markdown files
autocmd FileType markdown setlocal conceallevel=2
" Disable wrap to avoid showing empty lines when wrapped text is concealed
" (such as long hyperlinks)
autocmd FileType markdown setlocal nowrap
augroup END
" bullets.vim - https://github.com/dkarter/bullets.vim
let g:bullets_nested_checkboxes = 0 " Decouple parent and child checkbox toggling
let g:bullets_checkbox_markers = ' X'
" neoformat
" Define ufmt command for neoformat to execute
let g:neoformat_python_ufmt = {
\ 'exe': 'ufmt',
\ 'args': ['format'],
\ 'replace': 1,
\ }
" Python formatters for neoformat to try in order of priority
let g:neoformat_enabled_python = ['ufmt', 'black']
""""""""""""""""""""""""""""""""""""""""""
" Lint
""""""""""""""""""""""""""""""""""""""""""
augroup lint_and_format
autocmd!
" Neoformat
" CTRL-F to format
autocmd FileType python map <C-f> :Neoformat<CR>
autocmd FileType python imap <C-f> <ESC>:Neoformat<CR>i
" Autoformat on save
" autocmd FileType python autocmd BufWritePre * Neoformat
" Clang-format. TODO: just use NeoFormat?
autocmd FileType c,cpp,cc,cuda,java,objc,proto map <C-f> :ClangFormat<CR>
autocmd FileType c,cpp,cc,cuda,java,objc,proto imap <C-f> <ESC>:ClangFormat<CR>i
" Uncomment below to auto-format on buffer write
" autocmd FileType c,cpp,cc,cuda,java,objc,proto ClangFormatAutoEnable
" Strip any training whitespace on buffer write
autocmd FileType c,cabal,cpp,cuda,python,haskell,erlang,javascript,php,ruby,readme,tex,text,thrift
\ autocmd BufWritePre <buffer>
\ :call <SID>StripTrailingWhitespaces()
" Highlight past 79 characters
" highlight CharLimit ctermbg=black ctermfg=white guibg=#592929
" autocmd FileType c,cabal,cpp,cuda,python,haskell,erlang,javascript,php,ruby,thrift
" \ match CharLimit /\%80v.\+/
augroup END