-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvimrc
More file actions
359 lines (290 loc) · 10.9 KB
/
vimrc
File metadata and controls
359 lines (290 loc) · 10.9 KB
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
" =============================================================================
" Shortcuts & Utilities (leader = backslash '\')
" =============================================================================
" fzf:
" \f Open GFiles (git-aware fuzzy file finder)
"
" Splits & Windows:
" :vs Vertical split
" :sp Horizontal split
" Ctrl-j / k Switch between splits
" :Vs Alias for :vs
"
" Tabs:
" Ctrl-n New tab
" Ctrl-x Close tab (with confirmation)
" Ctrl-h / l Switch tabs left/right
"
" General:
" :TwoSpace Set indent to 2 spaces
" :FourSpace Set indent to 4 spaces
" :C / :U Comment/uncomment lines (visual, filetype-aware)
" =============================================================================
"
" Setup:
" 1. Clone Vundle:
" git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
" 2. Install plugins (from shell):
" vim +PluginInstall +qall
" or from within vim:
" :PluginInstall
" =============================================================================
" Set up Vundle, and install your desired plugins.
set nocompatible
" Restore terminal screen when exiting vim (alternate screen buffer)
"
" Most terminals have two screen buffers: the normal one (where your shell
" prompt and command history live) and an alternate one that fullscreen
" programs can use temporarily. These two settings control what escape
" sequences vim sends to the terminal on startup and exit:
"
" t_ti (terminal init) = \e[?1049h
" Sent when vim starts. Tells the terminal to save the current normal
" screen and switch to the alternate screen buffer.
"
" t_te (terminal end) = \e[?1049l
" Sent when vim exits. Tells the terminal to switch back to the normal
" screen buffer and restore what was there before.
"
" Without these, vim draws on the normal screen buffer and its output
" (file contents, line numbers, ~ lines) stays visible after you quit.
let &t_ti = "\e[?1049h"
let &t_te = "\e[?1049l"
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" Manage Vundle with Vundle
Plugin 'gmarik/Vundle.vim'
" Faster cmdt fuzzy searching
Plugin 'junegunn/fzf'
Plugin 'junegunn/fzf.vim'
call vundle#end()
filetype plugin indent on
" Activate GFiles search with backslash-f
nmap <leader>f :GFiles<cr>
" Convert default indentation between 2 and 4 spaces.
command! TwoSpace set shiftwidth=2 | set softtabstop=2
command! FourSpace set shiftwidth=4 | set softtabstop=4
" Use correct line endings.
set ff=unix
" Min lines above/below the cursor
set scrolloff=5
" Fold by indent
set foldmethod=indent
" Disable F1
:nnoremap <F1> :echo<CR>
:inoremap <F1> <C-o>:echo<CR>
" Show editing mode in status (-- INSERT --)
set showmode
" Enable text wrapping
set tw=0
set wrap
" Disable backing up files on :w
set nobackup
" Show cursor position
set ruler
" Turn off modeline (for security?)
set nomodeline
" By default, use memory, not swap files
set noswapfile
" Set the directory for whatever swap files you do use
set directory=/tmp
" Don't show tab or EOL chars
set nolist
" Automatically reload files when they have changed
set autoread
" Auto Indent according to the c standard
set ai
set cindent
" Show line numbers
set nu
" Incremental search
set incsearch
" Highlight search results
set hlsearch
" Disable all existing auto-commands to avoid potentially running auto-commands twice
:autocmd!
" Disable the visual and error bell
autocmd VimEnter * set vb t_vb=
" Remove the top tool bar with visual buttons
set go-=T
" Remove the right scroll bar
set go-=r
" Use dark background for default color scheme
set background=dark
" Ignore these files during autocomplete
set wildignore=*.o,*.obj,*.bak,*.exe,*.a,*.dep
" Set default spacing parameters
set smartindent
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
" Change active split with Ctrl-k and Ctrl-j
nnoremap <c-j> <C-w><S-w>
nnoremap <c-k> <C-w>w
nnoremap <c-j> <C-w><S-w>
nnoremap <c-k> <C-w>w
" Tab commands:
" * Ctrl-n to make a new tab
" * Ctrl-x to close tab
" * Ctrl-h to move left one tab
" * Ctrl-l to move right one tab
" * Ctrl-m Ctrl-h to move the current tab to the left
" * Ctrl-m Ctrl-l to move the current tab to the right
fun! CloseTab()
if confirm("Close tab?", "&yes\n&no", 1) == 1
:tabclose
endif
endfun
nnoremap <c-n> :tabnew<cr>
nnoremap <c-x> :call CloseTab()<cr>
nnoremap <c-h> gT
nnoremap <c-l> gt
nnoremap <c-m><c-h> :tabm -1<cr>
nnoremap <c-m><c-l> :tabm +1<cr>
vnoremap <c-n> :tabnew<cr>
vnoremap <c-x> :call CloseTab()<cr>
vnoremap <c-h> gT
vnoremap <c-l> gt
vnoremap <c-m><c-h> :tabm -1<cr>
vnoremap <c-m><c-l> :tabm +1<cr>
" :Vs == :vs
cab Vs vs
" Enable filetype specific default rules
filetype plugin on
" Set FileTypes
if !exists("did_load_filetypes_custom")
let did_load_filetypes_custom = 1
autocmd! BufNewFile,BufReadPost *.c,*.h,*.cpp,*.cc,*.cu.cc set ft=cpp
autocmd! BufNewFile,BufReadPost *.java set ft=java
autocmd! BufNewFile,BufReadPost *.xul,*.html set ft=html
autocmd! BufNewFile,BufReadPost *.txt,README,INSTALL set ft=text
autocmd! BufNewFile,BufReadPost *.js,*.json,*.vue set ft=javascript
autocmd! BufNewFile,BufReadPost *.py set ft=python
autocmd! BufNewFile,BufReadPost *.xml,*.launch set ft=xml
endif
" Replace all tabs with space
autocmd! BufWinEnter *.cpp,*.cu.cc,*.cc,*.c,*.h,*.java,*.js,*.py :%s/\t/ /eg
" Show whitespace at the end of lines, but not the current line when in insert mode.
highlight ExtraWhitespace ctermbg=darkred guibg=#3d1f1f
autocmd ColorScheme * highlight ExtraWhitespace ctermbg=darkred guibg=#3d1f1f
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
" Helper functions
if !exists ("did_load_helper_functions")
let did_load_helper_functions = 1
" Eat whitespace character
function! EatSpace()
let c = nr2char(getchar(0))
return (c =~ '\s' || c =~ '\r' || c =~ '\n') ? '' : c
endfunc
" Eat whitespace and open bracket character
function! EatSpaceBracket()
let c = nr2char(getchar(0))
return (c =~ '\s' || c =~ '\r' || c =~ '\n' || c =~ '(') ? '' : c
endfunc
" Context sensitive abbreviation (iab)
" Prevent expansion within a comment, string or character
function! MapWithContext(key, seq)
let syn_context = synIDattr(synID(line('.'), col('.') - 1, 1), 'name')
if syn_context =~? 'comment\|string\|character\|doxygen'
return a:key
else
exe 'return "' . substitute(a:seq, '\\<\(.\{-}\)\\>', '"."\\<\1>"."', 'g') . '"'
endif
endfunction
" Uses MapWithContext to add file type specific abbreviations
function! IabForFT(ft, key, seq)
exe 'autocmd BufEnter ' . a:ft . ' iab <silent> ' . a:key . ' <C-R>=MapWithContext("' . a:key . '", "' . a:seq . '")<CR>'
endfunction
autocmd BufLeave * iabclear
" Escape seq for IabForFT
function! E(val)
return substitute(a:val, '<\(.\{-}\)>', '\\\\<\1\\>', 'g')
endfunction
endif " End Helper Functions
" FileType Specific Abbreviations
if !exists("did_load_abbreviations")
let did_load_abbreviations = 1
" CPP
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'define', '#define')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'def', '#define')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'inc', '#include')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'ifdef', '#ifdef')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'ind', '#ifndef')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'ifndef', '#ifndef')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'ei', '#endif')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'endif', '#endif')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'NULL', 'nullptr')
call IabForFT('*.h,*.cpp,*.c,*.cc,*.cu.cc', 'null', 'nullptr')
" PY
call IabForFT('*.py,*.ctw,*.tw,*.cinc,*.conf,*.thrift-cvalidator,*.ctest', 'while', E('while :<Left><C-R>=EatSpace()<CR>'))
call IabForFT('*.py,*.ctw,*.tw,*.cinc,*.conf,*.thrift-cvalidator,*.ctest', 'for', E('for :<Left><C-R>=EatSpace()<CR>'))
call IabForFT('*.py,*.ctw,*.tw,*.cinc,*.conf,*.thrift-cvalidator,*.ctest', 'if', E('if :<Left><C-R>=EatSpace()<CR>'))
call IabForFT('*.py,*.ctw,*.tw,*.cinc,*.conf,*.thrift-cvalidator,*.ctest', 'elif', E('elif :<Left><C-R>=EatSpace()<CR>'))
call IabForFT('*.py,*.ctw,*.tw,*.cinc,*.conf,*.thrift-cvalidator,*.ctest', 'def', E('def :<Left><C-R>=EatSpace()<CR>'))
" Java
call IabForFT('*.java', 'imp', 'import')
" Comment Blocks
call IabForFT('*.php,*.phpt,*.cpp,*.h,*.c,*.java', '///', E('////////////////////////////////////////////////////////////////////////////////<CR> <C-R>=EatSpace()<CR>'))
call IabForFT('*.lua,*.sql', '---', E('--------------------------------------------------------------------------------<CR> <C-R>=EatSpace()<CR>'))
call IabForFT('*.py,*.ctw,*.tw,Makefile*,TARGETS,*.sh,.bashrc,*.thrift,*.cinc,*.cconf', '###', E('################################################################################<CR># <C-R>=EatSpace()<CR>'))
endif " End file type abbreviations
" Custom indent function for cpp
function! CustomCppIndent()
let l:cline_num = line('.')
let l:pline_num = prevnonblank(l:cline_num - 1)
let l:pline = getline(l:pline_num)
let l:retv = cindent('.')
while l:pline =~# '\(^\s*{\s*\|^\s*//\|^\s*/\*\|\*/\s*$\)'
let l:pline_num = prevnonblank(l:pline_num - 1)
let l:pline = getline(l:pline_num)
endwhile
" Don't indent after a namespace
if l:pline =~# '^\s*namespace.*'
let l:retv = 0
endif
" Only indent by one space after a public/private/protected
if getline(l:cline_num) =~# '^\s*\(public\|private\|protected\):\s*$'
let l:retv = 1
endif
return l:retv
endfunction
autocmd BufReadPre,BufNewFile *.h,*.cc,*.cu.cc,*.h,*.cpp setlocal indentexpr=CustomCppIndent()
" Python Settings
function! DoPythonSettings()
setlocal softtabstop=4
setlocal tabstop=4
setlocal shiftwidth=4
" Remove whitespace before and after brackets
" silent! %s/\([^ #]\)[ ]\+)/\1)/g
" silent! %s/([ ]\+\([^ ]\)/(\1/g
" silent! %s/\([^ #]\)[ ]\+\]/\1]/g
" silent! %s/\[[ ]\+\([^ ]\)/[\1/g
" Fix Dictionary lint
" silent! %s/\("\|'\)[ ]\+:[ ]*\([^ ]\)/\1: \2/g
" silent! %s/\("\|'\)[ ]*: [ ]\+\([^ ]\)/\1: \2/g
" Fix iterator lint
" silent! %s/^\([ ]*for [^ ]\+,\)\([^ ]\+ in\)/\1 \2/g
setlocal smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
" Re-indent file
" silent! gg=G
" silent! %s/ \(}\|)\|]\)/\1/g
" silent! %s/ ??????
" Separate Simple* charts
" %s/\(}),\)\n\([ ]*[^ \]]\)/\1\r\r\2/g
" Comment and uncomment
command! -range C <line1>,<line2> s/\(.*\)/#\1/g
command! -range U <line1>,<line2> s/\(\s*\)#\(.*\)/\1\2/g
endfunction
autocmd BufEnter *.py call DoPythonSettings()
function! DoVimSettings()
" Comment and uncomment
command! -range C <line1>,<line2> s/\(.*\)/"\1/g
command! -range U <line1>,<line2> s/[ ]*"\(.*\)/\1/g
endfunction
autocmd! BufEnter *.vim,*.vimrc call DoVimSettings()
" Syntax Highlighting on
syntax on