~gpanders/dotfiles

ref: 5a7f10fa8cda8c007f108dc062ec6c896d41dc9b dotfiles/vim/.vimrc -rw-r--r-- 8.8 KiB
5a7f10fa — Greg Anders Move custom ftplugin files to .vim/after directory 3 years ago
                                                                                
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
" Source plugins list
source $HOME/.vim/plugins.vim

" Set the leader key
let mapleader = ','
let maplocalleader = '\'

" Set color scheme {{{
" Set background if $TERMBG is set, otherwise let vim decide
if $TERMBG == 'light'
  set background=light
elseif $TERMBG == 'dark'
  set background=dark
endif

if has('termguicolors')
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  set termguicolors
else
  let g:solarized_use16 = 1
endif
colorscheme solarized8
" }}}

" Sudo save
cnoreabbrev w!! w !sudo tee > /dev/null %

" Settings {{{
set tabstop=2              " Tab width
set softtabstop=2          " Tab width
set shiftwidth=2           " Tab width
set expandtab              " Use spaces instead of tabs
set number                 " Show line numbers
set hidden                 " Hide buffers instead of closing them
set ignorecase             " Make searching case insensitive
set smartcase              " ... unless the query has capital letters
set gdefault               " Use 'g' flag by default with :s/foo/bar/.
set magic                  " Use 'magic' patterns (extended regular expressions)
set hlsearch               " Highlight search results
set splitbelow             " Open horizontal splits below current pane
set splitright             " Open vertical splits to the right of current pane
set nostartofline          " Don't move cursor to start of line when scrolling
set scrolloff=2            " 2 lines above/below cursor when scrolling
set showmatch              " Show matching bracket in insert mode
set matchtime=2            " Show matching bracket for 2 tenths of a second
set matchpairs+=<:>        " Add angle brackets as match pairs
set colorcolumn=80         " Show column at 80 characters
set noswapfile             " Disable swap file
set foldmarker=\ {{{,\ }}} " Make default fold marker have a space
set modeline               " Enable vim modelines

" Show completion menu even when only one match
" set completeopt=menuone,preview
" }}}

" Don't press Shift to enter command mode
" map ; :

" Statusline {{{
" Set the statusline
function! StatuslineGitBranch()
  let branch = FugitiveHead()
  if branch != ""
    return " " . branch
  else
    return ""
  endif
endfunction

set laststatus=2
set statusline=
set statusline+=%1*\ %<%f\ %h%m%r\ 
set statusline+=%2*\ %n\ 
set statusline+=%4*
set statusline+=\ %{CapsLockStatusline()}\ 
set statusline+=%=
set statusline+=\ %{StatuslineGitBranch()}\ 
set statusline+=\ %y\ 
set statusline+=%2*\ %P\ 
set statusline+=%1*\ %l:%c%V\ 

" Set User1-9 highlight groups for statusline
" These are used by adding %N* to the statusline, where {N} is 1-9
" Solarized definitions
if &background ==# 'light'
  hi User1 ctermfg=15 ctermbg=11 guifg=#fdf6e3 guibg=#657b83
  hi User2 ctermfg=15 ctermbg=14 guifg=#fdf6e3 guibg=#93a1a1
  hi User3 ctermfg=11 ctermbg=15 guifg=#657b83 guibg=#fdf6e3
  hi User4 ctermfg=11 ctermbg=7 guifg=#657b83 guibg=#eee8d5
else
  hi User1 ctermfg=8 ctermbg=12 guifg=#002b36 guibg=#839496
  hi User2 ctermfg=8 ctermbg=10 guifg=#002b36 guibg=#586e75
  hi User3 ctermfg=12 ctermbg=8 guifg=#839496 guibg=#002b36
  hi User4 ctermfg=12 ctermbg=0 guifg=#839496 guibg=#073642
endif
" }}}

" Insert mode mappings {{{

" jk in Insert mode escapes to Normal mode
inoremap jk <Esc>

" Auto close braces in insert mode
inoremap {<CR> {<CR>}<Esc>ko

" Use <C-W> in inset mode to make the word under the cursor all caps
inoremap <C-U> <Esc>gUiw`]a

" Delete forward in insert mode
inoremap <C-L> <Del>
" }}}

" Normal mode mappings {{{

" Save by pressing <leader>w
nnoremap <silent> <leader>w :w<CR>

" Use :tjump by default
nnoremap <C-]> g<C-]>

" Use <C-K> in Normal mode to use grepprg
" Define a custom command to wrap :grep with :silent, :copen, and :redraw
command! -nargs=+ -complete=file -bar -bang Grep silent! grep<bang> <args>|copen|redraw!
nnoremap <C-K> :Grep!<space>

" Buffer shortcuts {{{
nnoremap <silent> <leader>1 :b1<CR>
nnoremap <silent> <leader>2 :b2<CR>
nnoremap <silent> <leader>3 :b3<CR>
nnoremap <silent> <leader>4 :b4<CR>
nnoremap <silent> <leader>5 :b5<CR>
nnoremap <silent> <leader>6 :b6<CR>
nnoremap <silent> <leader>7 :b7<CR>
nnoremap <silent> <leader>8 :b8<CR>
nnoremap <silent> <leader>9 :b9<CR>
nnoremap <silent> <leader>0 :b10<CR>

" List buffers and put :b on the command line
noremap <leader>b :buffers<CR>:b

noremap <C-W>c :bd<CR>

noremap <silent> <M-[> :bprev<CR>
noremap <silent> <M-]> :bnext<CR>
" }}}

" Augment [I and ]I to place an :ij prompt below results for easy jumping
noremap [I [I:ij  <C-R><C-W><S-Left><Left>
noremap ]I ]I:ij  <C-R><C-W><S-Left><Left>

" Augment [D and ]D to place an :dj prompt below results for easy jumping
noremap [D [D:dj  <C-R><C-W><S-Left><Left>
noremap ]D ]D:dj  <C-R><C-W><S-Left><Left>

" Open jumplist
map <leader>ju :jumps<CR>

" Open quickfix list and place :cc on the prompt
noremap <leader>cl :clist<CR>:sil cc<space>

" Open location list and place :ll on the prompt
noremap <leader>ll :llist<CR>:sil ll<space>

" Open undo list
map <leader>u :undol<CR>

" Show marks
map <leader>m :marks<CR>

" <leader>ev opens .vimrc in new window
nnoremap <leader>ev :vsplit $MYVIMRC<CR>

" <leader>sv sources .vimrc
nnoremap <leader>sv :source $MYVIMRC<CR>

" Toggle between normal and relative numbering
nnoremap <leader>r :call NumberToggle()<CR>

" Navigate through wrapped lines individually
nnoremap <silent> j gj
nnoremap <silent> k gk

" Use space bar to toggle folds
nnoremap <space> za

" Clear search buffer with <C-N>
nnoremap <silent> <C-L> :nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>

" }}}

" Visual mode mappings {{{
vnoremap * y/\V<C-R>"<CR>
vnoremap # y?\V<C-R>"<CR>
" }}}

" Use ripgrep or ag as grepprg if available
if executable('rg')
  " Use rg over grep
  set grepprg=rg\ --vimgrep\ --hidden
elseif executable('ag')
    " Use ag over grep
    set grepprg=ag\ --vimgrep\ --hidden
endif
set grepformat='%f:%l:%c:%m,%f:%l:%m'

" Relative numbering {{{
function! NumberToggle()
  if (&relativenumber == 1)
    set nornu
    set number
  else
    set rnu
  endif
endfunc
" }}}

" Pushing built-in commands beyond their limits {{{
" https://gist.github.com/Konfekt/d8ce5626a48f4e56ecab31a89449f1f0
function! <sid>CCR()
    if getcmdtype() isnot# ':'
      return "\<CR>"
    endif
    let cmdline = getcmdline()
    if cmdline =~# '\v^\s*(ls|files|buffers)!?\s*(\s[+\-=auhx%#]+)?$'
        " like :ls but prompts for a buffer command
        return "\<CR>:b"
    elseif cmdline =~# '\v/(#|nu%[mber])$'
        " like :g//# but prompts for a command
        return "\<CR>:"
    elseif cmdline =~# '\v^\s*(dli%[st]|il%[ist])!?\s+\S'
        " like :dlist or :ilist but prompts for a count for :djump or :ijump
        return "\<CR>:" . cmdline[0] . "j  " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
    elseif cmdline =~# '\v^\s*(cli|lli)%[st]!?\s*(\s\d+(,\s*\d+)?)?$'
        " like :clist or :llist but prompts for an error/location number
        return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
    elseif cmdline =~# '\v^\s*ol%[dfiles]\s*$'
        " like :oldfiles but prompts for an old file to edit
        set nomore
        return "\<CR>:sil se more|e #<"
    elseif cmdline =~# '^\s*changes\s*$'
        " like :changes but prompts for a change to jump to
        set nomore
        return "\<CR>:sil se more|norm! g;\<S-Left>"
    elseif cmdline =~# '\v^\s*ju%[mps]'
        " like :jumps but prompts for a position to jump to
        set nomore
        return "\<CR>:sil se more|norm! \<C-o>\<S-Left>"
    elseif cmdline =~ '\v^\s*marks\s*(\s\w+)?$'
        " like :marks but prompts for a mark to jump to
        return "\<CR>:norm! `"
    elseif cmdline =~# '\v^\s*undol%[ist]'
        " like :undolist but prompts for a change to undo
        return "\<CR>:u "
    else
        return "\<c-]>\<CR>"
    endif
endfunction
cnoremap <expr> <CR> <sid>CCR()
" }}}

" Autocommands {{{
augroup vimrc
  autocmd!
  " Have Vim jump to the last position when reopening a file
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$")
        \ | exe "normal! g`\"" | endif

  " Highlight the current line in the current window but disable in Insert mode {{{
  let blacklist = ['tex']
  au BufEnter * if index(blacklist, &ft) < 0 | set cursorline | endif
  au BufLeave * if index(blacklist, &ft) < 0 | set nocursorline | endif
  au InsertEnter * if index(blacklist, &ft) < 0 | set nocursorline | endif
  au InsertLeave * if index(blacklist, &ft) < 0 | set cursorline | endif
  " }}}

  " Open the quickfix window after any grep search
  au QuickFixCmdPost *grep* cwindow

  " term
  au TermOpen * startinsert

  " Press q to close certain windows
  au FileType help,qf nnoremap <silent> <buffer> q :q<CR>
  au FileType netrw nnoremap <silent> <buffer> qq :Rex<CR>

augroup END
" }}}

" Enable per-project configuration {{{
set exrc
set secure
" }}}

" Source plugin configuration files
" Do this last so that plugins can override default configuration
runtime! config/*.vim