-
Notifications
You must be signed in to change notification settings - Fork 17
/
vimrc
573 lines (446 loc) · 16.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
set nocompatible
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General settings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Disable modelines for security reasons
" See https://thehackernews.com/2019/06/linux-vim-vulnerability.html
set modelines=0
set nomodeline
" Don't allow autocmds from local rc files we don't own
set secure
" Turn on syntax highlighting
syntax on
" Limit syntax highlighting to 300 columns
set synmaxcol=300
" Only highlight 256 or fewer lines
syntax sync minlines=256
" Show line numbers
set number
" Turn on auto indenting
set autoindent
" Use previous indent
set copyindent
" Tab is for spaces
set tabstop=2
" Number of spaces to use for auto indenting
set shiftwidth=2
" Convert tabs to spaces
set expandtab
" Show matching brackets
set showmatch
" Highlight search results
set hlsearch
" Jump to the next match in the file after the cursor
set incsearch
" Wrap around the file when searching
set wrapscan
" Allow certain movement commands to move into the next/previous line when it
" makes sense
set whichwrap=h,l
" Show 4 lines after cursor, useful when reviewing search results
set scrolloff=4
" Set the title of the terminal window
set title
" Don't move cursor to start of line when switching buffers
set nostartofline
" Show the 80 column line
set colorcolumn=80,120
" Allow backspace to delete end of line, indent and start of line characters
set backspace=indent,eol,start
" Comment formatting options
" Auto wrap text
set formatoptions+=tc
" Automatically insert the comment leader after hitting 'o' or 'O'
set formatoptions+=o
" Automatically insert the comment leader after hitting return in insert mode
set formatoptions+=r
" Allow formatting comments with 'gq'
set formatoptions+=q
" Allow numbered lists in comments
set formatoptions+=n
" Allow paste option to be toggled with F2
set pastetoggle=<F2>
" Show the curent mode on the last line (command line)
set showmode
" Turn on autocompletion of commands
set wildmenu
set wildmode=full
" Turn on cursor column highlighting
set cursorcolumn
" Use a visual bell instead of an audible one
set visualbell
set noerrorbells
" But disable even the visible bell as the terminal flashing on OSX is
" annoying
set t_vb=
" The mouse is evil
set mouse=
" Show the status bar
set laststatus=2
" Custom status bar
set statusline=\ Filename:%-8t " Filename
set statusline+=\ Encoding:\%-8{strlen(&fenc)?&fenc:'none'} " File encoding
set statusline+=\ Line\ Endings:%-6{&ff} " Line Endings
set statusline+=\ Type:%-12y " File Type
set statusline+=%=%h%m%r " Flags
set statusline+=\ %l/%L " Cursor line and total lines
set statusline+=\ %c " Cursor column
set statusline+=\ %P " Percentage through file
" No need to show the buffer number
"set statusline+=\ Buf:%n " Buffer number
" Color status bar
highlight statusline ctermfg=cyan ctermbg=black guifg=cyan guibg=black
if has('macunix')
" allow yanking to OSX clipboard
" http://stackoverflow.com/questions/11404800/fix-vim-tmux-yank-paste-on-unnamed-register
if $TMUX == ''
set clipboard+=unnamed
else
set clipboard=unnamed
endif
else
" Allow yanking to clipboard on Ubuntu
set clipboard=unnamedplus
endif
" Use special chars in place of tab and eol
set listchars=eol:¬,tab:→\ ,extends:>,precedes:<,trail:·,space:·
" Ignored files
set wildignore+=/tmp/,*.so,*.swp,*.swo,*.zip,*.meta,*.prefab,*.png,*.jpg,*.beam
" Allow hidden buffers, useful for when you want to edit a buffer but not save
" it before working on another buffer.
set hidden
" Turn on spell checking for all files
set spell
" Use American English when spell checking is turned on
set spelllang=en_us
" Set the spell file to one stored in my dotfiles repo
set spellfile=$HOME/dotfiles/vim/spell/en.utf-8.add
" Regenerate binary .spl files from .add files
" https://vi.stackexchange.com/questions/5050/how-to-share-vim-spellchecking-additions-between-multiple-machines
for d in glob('~/.vim/spell/*.add', 1, 1)
if filereadable(d) && (!filereadable(d . '.spl') || getftime(d) > getftime(d . '.spl'))
silent exec 'mkspell! ' . fnameescape(d)
endif
endfor
" Keyword completion options
" Use completions from spell check
set complete+=kspell
" Use tags for completion as well
set complete+=t
" Configure as-you-type completions
" menu - show menu when there are multiple possible completions
" menuone - show menu even when there is only one possible completion
" preview - show extra info about the currently selected completion
set completeopt=menu,menuone,preview
" TODO: Figure out what all these settings do and check in the ones that are
" useful
"set spell noci nosi noai nolist noshowmode
" Don't redraw when executing macros
set lazyredraw
" Display the output of an *incomplete* command in the bottom right
set showcmd
" Use the old vim regex engine (version 1, as opposed to version 2, which was
" introduced in Vim 7.3.969). The Ruby syntax highlighting is significantly
" slower with the new regex engine.
" https://github.com/joshukraine/dotfiles/blob/79aae9ac707460877d0fb36a3ef6a9b1ea7c44ce/vim-performance.md
set re=1
" Maintain edit history between sessions in file
set undofile
" Directory for edit history
set undodir=~/.vim/undodir
" Use syntax for folding
set foldmethod=syntax
" By default open files completely unfolded
set foldlevel=99
" Use built in file browser (netrw) instead of NERDTree
" (https://shapeshed.com/vim-netrw/)
let g:netrw_banner = 0
" Make the browser 25% of the width of the editor
let g:netrw_winsize = 25
" Show directory as an expandable tree
let g:netrw_liststyle = 3
" Disable the banner at the top of the buffer
let g:netrw_banner = 0
" Open files in the previous window (e.g. not the netrw file browser window)
let g:netrw_browse_split = 4
" Open files on the right
let g:netrw_altv = 1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Shortcuts/Mappings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Toggle Tagbar
nmap <F9> :TagbarToggle<CR>
" Make moving between windows easier.
" http://vimcasts.org/episodes/working-with-windows/
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
" Map semicolon to colon for easier invocation of ex commands
map ; :
" Make comma the leader key
let mapleader = ","
" A more convenient buffer write command
map <leader>s <esc>:w<CR>
" Mappings for toggling between line numbers
map <leader>n :set nornu number<CR>
map <leader>r :set rnu number relativenumber<CR>
" Toggle visibility of whitespace characters
nmap <leader>l :set list!<CR>
" JSON formatting with jq
nnoremap <silent> <leader>fj :%!jq '.'<CR>
" Erlang term formatting with erlang_pretty_print
nnoremap <silent> <leader>fe :%!erlang_pretty_print -i<CR>
" Jira ticket command
nnoremap <silent> <leader>to :!open $BASE_TICKET_URL/<c-r>=expand("<cWORD>")<cr>/<CR>
" Disable arrow keys
nnoremap <Left> :echoe "Use h"<cr>
nnoremap <Right> :echoe "Use l"<cr>
nnoremap <Up> :echoe "Use k"<cr>
nnoremap <Down> :echoe "Use j"<cr>
inoremap <Left> <esc> :echoe "Use h"<cr>
inoremap <Right> <esc> :echoe "Use l"<cr>
inoremap <Up> <esc> :echoe "Use k"<cr>
inoremap <Down> <esc> :echoe "Use j"<cr>
" Allow replacing of searched text by using `cs` on the first result and `n.`
" on all consecutive results
vnoremap <silent> s //e<C-r>=&selection=='exclusive'?'+1':''<CR><CR>
\:<C-u>call histdel('search',-1)<Bar>let @/=histget('search',-1)<CR>gv
omap s :normal vs<CR>
" Allow easy searching for visually selected text
vnoremap /// y/<C-R>"<CR>
" Allow sudo after opening file
cmap w!! w !sudo tee >/dev/null %
" Press Space to turn off highlighting and clear any message already displayed.
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>
" Toggle everything that occupies space on the left side of the editor
function! ToggleLeftGuides()
if (g:left_guides_enabled == 1)
call HideLeftGuids()
let g:left_guides_enabled = 0
else
call ShowLeftGuids()
let g:left_guides_enabled = 1
endif
endfunction
" By default everything on the left side is enabled
let g:left_guides_enabled = 1
" Hide everything that occupies space on the left side of the file, so we can
" copy the file contents with ease
function! HideLeftGuids()
" Hide line numbers
set nonumber
" Hide GitGutter
GitGutterDisable
" Reset Syntastic, then set it to passive mode, this effectively hides the
" hints in the left side columns
SyntasticToggle
SyntasticReset
endfunction
" Show everything that occupies space on the left side of the file
function! ShowLeftGuids()
" Show line numbers
set number
" Show GitGutter
GitGutterEnable
" Run the Syntastic check
SyntasticCheck
endfunction
:nnoremap <F4> :call ToggleLeftGuides()<CR>
" Load trailing whitespace functions
source $HOME/.vim/whitespace.vim
"highlight NonText ctermfg=darkgreen guifg=darkgreen
highlight SpecialKey ctermfg=darkgreen guifg=darkgreen
if &filetype == 'html' || exists("java_javascript")
" JAVA SCRIPT
"syn include @htmlJavaScript syntax/javascript.vim
syn include @htmlJavaScript ftdetect/javascript.vim
endif
" BangOpen is useful for open an executable script on the $PATH without having
" to lookup the name manually. For example:
"
" :BangOpen which to_server
"
function! BangOpen(arg)
execute 'tabe ' . system(a:arg)
endfunction
command! -nargs=1 BangOpen :call BangOpen(<f-args>)
function! SetSpaces(arg)
echo "settings spaces to: " . a:arg
execute 'set tabstop=' . a:arg
execute 'set shiftwidth=' . a:arg
execute 'set softtabstop=' . a:arg
endfunction
command! -nargs=1 SetSpaces :call SetSpaces(<f-args>)
" Make Q repeat last macro
nnoremap Q @@
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Plugins and plugin settings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Load plugins
source $HOME/.vim/plugins.vim
" Start CtrlP on startup
autocmd VimEnter * CtrlP
" Vim-Erlang Skeleton settings
let g:erl_replace_buffer=0
" Custom simplified Erlang templates (without all the verbose comment blocks)
let g:erl_tpl_dir=$HOME."/dotfiles/templates/erlang"
" Erlang plugin settings. TODO: Pull these from the environment
" let g:erl_author=""
" let g:erl_company=""
" Align line-wise comment delimiters flush left instead of following code
" indentation
let g:NERDDefaultAlign = 'left'
" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" CtrlP directory mode
let g:ctrlp_working_path_mode = 0
set runtimepath^=$HOME/.vim/bundle/ctrlp.vim
" Vim pencil settings
" Hard wrapping was causing newlines I added to be removed from code in Markdown
let g:pencil#wrapModeDefault = 'soft'
let g:pencil#textwidth = 80
" Goyo writing mode callback functions
" I have to toggle vim-pencil in these callbacks because of
" https://github.com/reedes/vim-pencil/issues/75
function! s:goyo_enter()
autocmd FileType text,mkd.markdown,markdown,mkd call pencil#init()
:PencilSoft
endfunction
function! s:goyo_leave()
:PencilOff
endfunction
autocmd! User GoyoEnter nested call <SID>goyo_enter()
autocmd! User GoyoLeave nested call <SID>goyo_leave()
"open CtrlP in buffer mode
nnoremap <leader>b :CtrlPBuffer<CR>
" custom CtrlP ignores toggle
function! ToggleCtrlPIgnores()
if exists("g:ctrlp_user_command")
" unset the ignores
let g:ctrlp_custom_ignore = {}
unlet g:ctrlp_user_command
else
" always ignore these patterns
let g:ctrlp_custom_ignore = {
\'dir': 'ebin\|DS_Store\|git$\|bower_components\|node_modules\|logs',
\'file': '\v\.(beam|pyc|swo|jpg)$',
\}
" also ignore files listed in the .gitignore
" This command returns all text files under version control
let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . -co --exclude-standard', 'find %s -type f']
"let g:ctrlp_user_command = '(cd %s; git grep --cached -Il "") || find %s -type f'
end
endfunction
call ToggleCtrlPIgnores()
"let g:ctrlp_user_command = '(cd %s; git grep --cached -Il "") || find %s -type f'
:nnoremap <F6> call ToggleCtrlPIgnores()<CR>
" vim-indent-guides settings
"let g:indent_guides_enable_on_vim_startup = 1
"let g:indent_guides_start_level = 0
"let g:indent_guides_guide_size = 1
" ALE settings
let g:ale_lint_delay = 400
" Disable elixirLS Dialyzer checks
let g:ale_elixir_elixir_ls_config = {
\ 'elixirLS': {
\ 'dialyzerEnabled': v:false,
\ },
\ }
" Configure ALE fixers
" On all files, removing trailing lines and whitespace
" On Elixir files, run mix format
"
" See
" https://petermalmgren.com/rc-batch-day-9/
let g:ale_fixers = {
\ '*': ['remove_trailing_lines', 'trim_whitespace'],
\ 'elixir': ['mix_format']
\}
" Enable specific linters for certain file types
let g:ale_linters = {
\ 'erlang': ['dialyzer', 'erlc', 'erlfmt', 'elvis'],
\ 'go': ['go build', 'staticcheck']
\}
" Automatically run fixer macros on buffer save, and linters when leaving
" insert mode
let g:ale_fix_on_save = 1
let g:ale_lint_on_insert_leave = 1
" UltiSnips settings
let g:UltiSnipsExpandTrigger=",<tab>"
" My custom mapping
function! RunTestFileOrLast()
if (test#test_file(expand('%s')) == 1)
echo "In a test file"
:TestFile
else
echo "Not in a test file, running last"
:TestLast
endif
endfunction
" Run my custom RunTestFileOrLast function when I hit enter
nnoremap <CR> :call RunTestFileOrLast()<CR>
" Run all the tests in the project when I hit shift enter
nnoremap <S-CR> :TestSuite<CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" File type settings (file type-specific settings in vim/ftplugin/)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd BufRead,BufNewFile *.txt setfiletype text
autocmd BufRead,BufNewFile sys.config,*.app.src,*.app,*.erl,*.es,*.hrl,*.yaws,*.xrl,*.spec,elvis.config,rebar.lock,*.yrl setfiletype erlang
autocmd BufRead,BufNewFile Gemfile,Vagrantfile set filetype=ruby
autocmd BufRead,BufNewFile *.bats set filetype=sh
autocmd BufRead,BufNewFile Jenkinsfile set filetype=groovy
autocmd BufRead,BufNewFile *gitconfig set filetype=conf
" I don't think *.avdl files are valid scala but this is close enough
autocmd BufRead,BufNewFile *.avdl set filetype=scala
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Auto commands
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
augroup vimrc
autocmd!
" Include ! and ? as word characters, so dw will delete all of e.g. gsub!,
" and not leave the "!"
autocmd FileType ruby,eruby,yaml set iskeyword+=!,?
" Re-source vimrc whenever it changes
autocmd BufWritePost .vimrc,*vimrc source %
" Turn on spell checking for file types that contain text
autocmd FileType markdown,asciidoc,text setlocal spell
augroup END
augroup test
autocmd!
" Run tests automatically when a file is saved
autocmd BufWritePost * if test#exists() |
\ TestFile |
\ endif
augroup END
" Taken from https://jeffkreeftmeijer.com/vim-number/
augroup numbertoggle
: autocmd!
: autocmd BufEnter,FocusGained,InsertLeave,WinEnter * if &nu && mode() != "i" | set rnu | endif
: autocmd BufLeave,FocusLost,InsertEnter,WinLeave * if &nu | set nornu | endif
:augroup END
" Always highlight mispellings with and underline and bold text
hi SpellBad cterm=underline,bold
" set default theme, I like xcode
if !exists('g:rtf_theme')
let g:rtf_theme = 'xcode'
end
" Function for RTF syntax highlighting of the current buffer. Useful when I
" need to paste code with syntax highlighting into PowerPoint or another
" application that supports rich text.
function! RTFHighlight(line1,line2)
if !executable('pygmentize')
echoerr "Bummer! pygmentize not found."
return
endif
let content = join(getline(a:line1,a:line2),"\n")
let lexer=&filetype
" Run the pygmentize command
let command = "pygmentize -f rtf -O style=" . g:rtf_theme . " -l " . l:lexer
let output = system(command, content)
" Pipe the RTF text to pbcopy
let retval = system("pbcopy", output)
endfunction
" Invoking RTFHighlight on the current buffer will copy the highlighted code
" to clipboard
command! -range=% RTFHighlight :call RTFHighlight(<line1>,<line2>)