-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
462 lines (394 loc) · 12.5 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
set nocompatible
filetype plugin on
" For plugins
" https://github.com/google/vim-searchindex.git
set runtimepath+=~/.vim/bundle/vim-searchindex
" https://github.com/unblevable/quick-scope.git
set runtimepath+=~/.vim/bundle/quick-scope
" https://github.com/tpope/vim-commentary.git
set runtimepath+=~/.vim/bundle/vim-commentary
" https://github.com/tpope/vim-repeat.git
set runtimepath+=~/.vim/bundle/vim-repeat
" https://github.com/tpope/vim-fugitive.git
set runtimepath+=~/.vim/bundle/vim-fugitive
" https://github.com/nacitar/a.vim.git
set runtimepath+=~/.vim/bundle/a.vim
" https://github.com/sw561/vim-resizewindow.git
set runtimepath+=~/.vim/bundle/vim-resizewindow
" https://github.com/junegunn/gv.vim.git
set runtimepath+=~/.vim/bundle/gv.vim
" https://github.com/tpope/vim-eunuch.git
set runtimepath+=~/.vim/bundle/vim-eunuch
" https://github.com/tpope/vim-rhubarb.git
set runtimepath+=~/.vim/bundle/vim-rhubarb
" Inspired by http://vi.stackexchange.com/questions/6800/
function! Mycabbrev(lhs,rhs)
execute printf("cnoreabbrev <expr> %s getcmdtype() ==# ':' ? '%s' : '%s'",
\ a:lhs, a:rhs, a:lhs)
endfunction
let g:qs_highlight_on_keys = ['f', 'F', 't', 'T']
augroup gs_colors
autocmd!
autocmd ColorScheme * highlight QuickScopePrimary ctermfg=9
autocmd ColorScheme * highlight QuickScopeSecondary ctermfg=57
augroup END
nnoremap gs :Gstatus<CR><C-W>J
command! Gdall tabnew % | Git! diff
command! Gcached tabnew % | Git! diff --cached
call Mycabbrev("gda", "Gdall")
call Mycabbrev("gca", "Gcached")
call Mycabbrev("gv", "GV")
call Mycabbrev("gva", "GV --all")
" For using a.vim in LSC_AMR
let g:alternateExtensions_cpp_C = "H"
" Using vim-resizewindow for comfort
nmap <C-Up> <Plug>ResizeWindowUp
nmap <C-Down> <Plug>ResizeWindowDown
nmap <C-Left> <Plug>ResizeWindowLeft
nmap <C-Right> <Plug>ResizeWindowRight
" For when term is screen-256color
nmap [A <Plug>ResizeWindowUp
nmap [B <Plug>ResizeWindowDown
nmap [C <Plug>ResizeWindowRight
nmap [D <Plug>ResizeWindowLeft
" Settings
set autoindent
set copyindent
set noexpandtab
set tabstop=4
set softtabstop=4 " Useful in case where I use expandtab
set shiftwidth=0 " use tabstop value for autoindenting
set number
set relativenumber
set tabpagemax=20
set splitright
set splitbelow
set ignorecase
set smartcase
set scrolloff=2
set clipboard=unnamedplus
set confirm
set nojoinspaces
set wildignore=*.swp,*.png,*.pyc,*.o,*.so,*~,*.pdf,*.db,*.eps,*.gz,*.tar
set wildmenu
set noerrorbells
set vb t_vb=
set exrc " Can put local .vimrc in project directory
set nomodeline
set secure
set timeout timeoutlen=1000 ttimeoutlen=10
set hlsearch
nohl " Make sure that on reload of vimrc the last search is not highlighted
nnoremap <silent> ,/ :nohl<CR>
set listchars=tab:>-,eol:$
nnoremap ,t :set list!<CR>
set formatoptions=tcqjroln
set synmaxcol=300
set path=**
set linebreak
set completeopt+=menuone
set commentstring=//\ %s
set diffopt+=iwhite,vertical
set lazyredraw
" My custom color scheme - just some minor changes to the default settings
colo sand_light
nnoremap <F12> :up<CR>:colo sand_light<CR>
imap <F12> <Esc><F12>
" Turn on the mouse, for scrolling too
set mouse=n
noremap <ScrollWheelUp> 3<C-Y>
noremap <ScrollWheelDown> 3<C-E>
noremap <C-E> 3<C-E>
noremap <C-Y> 3<C-Y>
imap <C-E> <C-O><C-E>
imap <C-Y> <C-O><C-Y>
nmap <Down> <C-E>
nmap <Up> <C-Y>
vmap <Down> <C-E>
vmap <Up> <C-Y>
imap <Down> <C-O><C-E>
imap <Up> <C-O><C-Y>
" Make Y behave analogously to D and C
noremap Y y$
" New line with enter - start new paragraph by hitting enter twice
nnoremap <CR> o
" Swap 0 and ^, ^ is more useful, but 0 is easier to press
nnoremap 0 ^
nnoremap ^ 0
vnoremap 0 ^
vnoremap ^ 0
" In visual mode, don't include end of line blank characters
vnoremap $ g_
vnoremap g_ $
" In visual mode, search for the selected string with //
function! SearchSelection()
let @/ = substitute(@0, '/', '\\/', 'g')
call search(@/)
endfunction
vnoremap <silent> // y:call SearchSelection()<CR>
" Don't lose the visual selection when adjusting indentation
vnoremap < <gv
vnoremap > >gv
" Don't copy the contents of an overwritten selection.
vnoremap p "_dP
" Don't need exec mode
noremap Q <nop>
" Prefer to use CTRL-F to access history
nnoremap q: <nop>
nnoremap q/ <nop>
" From defaults.vim - Use CTRL-G u to break undo
inoremap <C-U> <C-G>u<C-U>
" Settings for the command window
autocmd CmdwinEnter * noremap <buffer> <CR> <CR>
autocmd CmdwinEnter * inoremap <buffer> <CR> <CR>
autocmd CmdwinEnter * noremap <buffer> <C-F> A<C-X><C-F>
autocmd CmdwinEnter * noremap <buffer> <C-E> <C-E>
autocmd CmdwinEnter * noremap <buffer> <C-Y> <C-Y>
autocmd CmdwinEnter * setlocal nonumber relativenumber
" Note that scrolloff is a global setting, so need to change it back.
" setlocal will not work in this case
autocmd CmdwinEnter * set scrolloff=0
autocmd CmdwinLeave * set scrolloff=2
" Space shortcut for cross line searching in prose
cnoremap <expr> ,<space> getcmdtype()=='/' ? '\_W\+' : ',<space>'
function! RepeatSearch()
" Modify the contents of the register with last search pattern
let @/ = substitute(@/, ' ', '\\_W\\+', 'g')
" Now search for it
call search(@/)
endfunction
nnoremap ,,/ :call RepeatSearch()<CR>
" Settings for netrw
let g:netrw_liststyle=3
" Jump through the quickfix list
function! Cnext()
try
cnext
catch /^Vim\%((\a\+)\)\=:E553/
cfirst
catch /^Vim\%((\a\+)\)\=:E\%(325\|776\|42\):/
endtry
endfunction
function! Cprev()
try
cprev
catch /^Vim\%((\a\+)\)\=:E553/
clast
catch /^Vim\%((\a\+)\)\=:E\%(325\|776\|42\):/
endtry
endfunction
nnoremap <silent> <C-N> :call Cnext()<CR>
nnoremap <silent> <C-P> :call Cprev()<CR>
function! OpenQFlist()
let height = max([2, min([10, len(getqflist())])])
execute "copen " . height
endfunction
nnoremap <silent> ,c :call OpenQFlist()<CR>
" Autocomplete filenames
inoremap <C-F> <C-X><C-F>
" Autocomplete using tag file
inoremap <C-]> <C-X><C-]>
" Quit command window by pressing CTRL-C once only
nnoremap <silent> <C-C> <C-C><C-C>
inoremap <silent> <C-C> <C-C><C-C>
" Use autocomplete in command mode by opening command window
cnoremap <C-N> <C-F>A<C-N>
cnoremap <C-P> <C-F>A<C-P>
" Undo autocomplete using <BS>
" This means if pumvisible() returns true do CTRL-E instead
" :help popupmenu-keys
inoremap <expr> <BS> pumvisible() ? '<C-E>' : '<BS>'
" If pop-up menu is visible, use C-J to go to search subdirectory, else add a
" line below the cursor.
inoremap <expr> <C-J> pumvisible() ? "<C-Y><C-X><C-F>" : "<Esc>m'o<Esc><C-O>a"
" Add a new line below the cursor
" inoremap <C-J> <Esc>m'o<Esc><C-O>a
" Remove line below cursor, opposite of <C-J>
inoremap <C-K> <Esc>m'jdd<Esc><C-O>a
" Use CTRL j to navigate down directories in wildmenu
set wildcharm=<Tab>
cnoremap <C-J> <Down>
" Make tags file for jumping around using <C-]> and back with <C-T>
command! MakeTags !ctags -R .
" Open tags in vertical split rather than horizontal
nnoremap <C-W><C-]> <C-W><C-]><C-W>t<C-W>H<C-W>p
" Open tag in new tab
nnoremap <C-T><C-]> <C-W><C-]><C-W>T
" If preceding with g, use :tselect not :tjump
nnoremap g<C-]> g]
" Make a split for the tag - and go back in old window
nnoremap <C-W><C-T> :vsplit<CR><C-W>h<C-T>
" Exclamation mark avoids having to press <CR> twice after the make
nnoremap ,m :!make<CR>
" Turn off spell checking for comments in tex files
let g:tex_comment_nospell=1
" Use :x instead of :wq only write if changes have been made
call Mycabbrev("wq","x")
call Mycabbrev("w","update")
call Mycabbrev("Q","q")
cnoremap w!! w !sudo tee > /dev/null %
" Function for making backspace behave as expected in normal mode
function! MyBackspace()
if col('.')==col('$')-1
" If at the end of a non-empty line, delete last character
startinsert!
else
" Otherwise delete character to left of cursor
startinsert
endif
endfunction
nnoremap <BS> :call MyBackspace()<Enter><BS>
" Folding
nnoremap <space> zf
vnoremap <space> zf
nnoremap ,<space> zd
" Comfortable movement between split windows
noremap <C-J> <C-W>j
noremap <C-K> <C-W>k
noremap <C-L> <C-W>l
noremap <C-H> <C-W>h
inoremap <C-L> <Esc><C-W>l
inoremap <C-H> <Esc><C-W>h
" The above mappings overwrite CTRL-L for redrawing the screen
nnoremap <expr> ,d &diff ? '<C-L>:diffupdate<CR>' : '<C-L>'
" For whatever reason, I can never remember :diffthis and :diffoff
call Mycabbrev("diffstart", "diffthis")
call Mycabbrev("diffon", "diffthis")
call Mycabbrev("diffend", "diffoff")
call Mycabbrev("diffstop", "diffoff")
" Shortcuts for using fuzzy find to open files
nnoremap ,fe :find *
nnoremap ,ft :tabfind *
nnoremap ,fs :sfind *
nnoremap ,fv :vert sfind *
" Shortcuts for using tabs
call Mycabbrev("tn","tab split")
call Mycabbrev("to","TabOnly")
call Mycabbrev("tc","tabclose")
call Mycabbrev("c", "tabclose")
call Mycabbrev("tm","TabMove")
nmap H gT
nmap L gt
nnoremap K <nop>
" Shortcuts for using buffers
command! MyBufferDelete bp|bd# " :bd will delete buffer without deleting window
call Mycabbrev("bd","MyBufferDelete")
nnoremap ,l :ls<CR>:b<Space>
function! GetModTime()
echo strftime('%c',getftime(expand('%')))
endfunction
command! GM call GetModTime()
command! CD cd %:h
" Commands for specific filetypes
set spelllang=en_gb
augroup FileTypeAuCmds
au!
autocmd FileType cpp
\ setlocal commentstring=\/\/\ %s |
\ setlocal matchpairs+=<:>
autocmd FileType tex,rst,markdown call Prose()
autocmd FileType haskell setlocal expandtab
autocmd BufRead,BufNewFile *.i setlocal filetype=swig
autocmd BufRead,BufNewFile *.gp setlocal filetype=gnuplot
autocmd FileType git setlocal foldlevel=1
autocmd BufRead,BufNewFile *.tex,*.pdf_tex setlocal filetype=tex
autocmd BufRead,BufNewFile *.out setlocal nowrap
autocmd FileType cmake setlocal commentstring=#%s
augroup END
function! Prose()
setlocal textwidth=79
let g:searchindex_star_case=0
if !&readonly
set spell
endif
set spellfile=./en.utf-8.add |
endfunction
" vim -b : edit binary using xxd-format!
augroup Binary
au!
au BufReadPre *.bin,*.bmp,*.gif let &bin=1
au BufReadPost *.bin,*.bmp,*.gif if &bin | %!xxd
au BufReadPost *.bin,*.bmp,*.gif set ft=xxd | endif
au BufWritePre *.bin,*.bmp,*.gif if &bin | %!xxd -r
au BufWritePre *.bin,*.bmp,*.gif endif
au BufWritePost *.bin,*.bmp,*.gif if &bin | %!xxd
au BufWritePost *.bin,*.bmp,*.gif set nomod | endif
augroup END
function! Skeleton_Candidates(ArgLead, CmdLine, CursorPos)
return "python\npython3\ncpp\nMakefile\ngnuplot\nbash\ntex\ngo"
endfunction
" Skeleton files
function! Skeleton(name)
setlocal autoread
if a:name == "python"
0read ~/RC_files/skeleton/python.py
write
silent !chmod u+x %
elseif a:name == "python3"
0read ~/RC_files/skeleton/python3.py
write
!chmod u+x %
elseif a:name == "cpp"
0read ~/RC_files/skeleton/cpp.cpp
write
elseif a:name == "Makefile"
0read ~/RC_files/skeleton/Makefile
write
elseif a:name == "gnuplot"
0read ~/RC_files/skeleton/gp.gp
write
elseif a:name == "bash"
0read ~/RC_files/skeleton/bash.sh
write
silent !chmod u+x %
elseif a:name == "tex"
0read ~/RC_files/skeleton/tex.tex
write
elseif a:name == "go"
0read ~/RC_files/skeleton/go.go
write
else
echohl WarningMsg | echo "Argument not understood" | echohl None
endif
setlocal noautoread
endfunction
command! -nargs=1 -complete=custom,Skeleton_Candidates Sk call Skeleton(<f-args>)
call Mycabbrev("sk","Sk")
" Tiny plugin to make writing class constructors easy in python
" Put the cursor on a line which looks like: (you can try it right here)
" >>> def __init__(self, a, b, c):
" Then type ,init and see what happens
function! PutSelf()
exec "normal! i\<tab>\<tab>self.\<esc>lyiwA = \<esc>p"
endfunction
nnoremap ,init 0f(lyi(o<Esc>p0df V:s/, /\r/ge<CR>:nohl<CR>V'<:call PutSelf()<CR>
" In versions of vim older than 7.4 relativenumber and number cannot be
" combined. As a result to switch to number, it is not sufficient to turn off
" relativenumber, number also has to be set.
function! ReadOnly()
setlocal readonly
setlocal nomodifiable
setlocal norelativenumber
setlocal nospell
if (v:version < 704)
set number
endif
endfunction
function! Edit()
setlocal modifiable
setlocal noreadonly
setlocal relativenumber
if &filetype == "tex" || &filetype == "rst" || &filetype == "markdown"
setlocal spell
endif
endfunction
nnoremap ,r :call ReadOnly()<CR>
nnoremap ,e :call Edit()<CR>
" Prefer not to see filetype in statusbar
let g:status_filetype = 0
" Use ,q to show the highlight group under the cursor, allows colourscheme to
" be changed
" http://vim.wikia.com/wiki/Identify_the_syntax_highlighting_group_used_at_the_cursor
map ,q :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>