-
Notifications
You must be signed in to change notification settings - Fork 1
/
_vimrc
372 lines (284 loc) · 10.1 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
" --------------------------------------------------------------
" Author: Bojack F. CHEN
"
" Description: This is my personal VIM configuration file.
" Note that plugin configurations are included by sourcing
" another file named 'plugin.vim', where plugin configurations
" are placed.
"
" Github repository: https://github.com/bojackchen/gvim.git
"
" Personal page: https://bojackchen.github.io
"
"
" Sections:
" -> General
" -> VIM user interface
" -> Color and font
" -> Backup, undo and swap
" -> Fold, tab and indent
" -> Visual mode
" -> Movement, tab and buffer
" -> Status line
" -> Editing mapping
" -> Spell check
" -> Misc
" -> Plugin
" -> Function
"
" --------------------------------------------------------------
" --------------------------------------------------------------
" -> General
" --------------------------------------------------------------
" Ward off unexpected things that my distro may have, as well
" as safely reset options when re-source .vimrc
" Use Vim settings rather than Vi settings
" This must be put at the first, because it will change other
" options
set nocompatible
" Keep 64 lines of command line history
set history=64
" Determine the file type base on its name and possibly its
" content, allowing for intelligent indent and plugins that
" are filetype specific
filetype plugin indent on
" Auto read the file when a file is changed from the outside
set autoread
" Redefine the map leader from '\' to ','
let mapleader = ","
" :W sudo saves the file (useful for handling the
" permission-denied error)
command! W w !sudo tee % > /dev/null
" --------------------------------------------------------------
" -> VIM user interface
" --------------------------------------------------------------
" In many terminal emulators the mouse works just fine, thus
" enable it for convenience
if has('mouse')
set mouse=a
endif
" Display current mode at the bottom line
set showmode
" Display partial commands in the last line of the screen.
set showcmd
" Set 8 lines to the cursor when moving vertically using j/k
set scrolloff=8
" Turn on wild menu for better command line completion
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc,.DS_Store
" Display the cursor position on the last line of the screen or
" in the status line of the window.
set ruler
" Display line number on the left
set number
" Set the command window height to 2 lines, to avoid many cases
" of having to 'Press <Enter> to continue'.
set cmdheight=2
" Easier and better switch between buffers with unsaved changed
set hidden
" Allow backspacing over autoindent, line breaks and start of
" insert action in insert mode
set backspace=indent,eol,start
" Allow left, right, h and l to go cross lines automatically
set whichwrap+=<,>,h,l
" Case insensitive search for vim, except when using capital
" letters
set ignorecase
" When searching try to be smart about cases
set smartcase
" Highlight search results. Use Ctrl + L to temporarily turn
" off highlight (See the mapping below).
set hlsearch
set incsearch
" Do not redraw while executing macros (good performance config)
set lazyredraw
" For regular expressions turn magic on
set magic
" Show matching brackets when text indicator is over them
set showmatch
" Set how many tenths of a second to blink when match is found
set mat=2
" Instead of failing a command because of unsaved changes, raise
" a dialogue asking if you wish to save
set confirm
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
set timeoutlen=500
" --------------------------------------------------------------
" -> Color and font
" --------------------------------------------------------------
" Enable syntax highlighting
syntax on
" Set background color to dark
set background=dark
" Set colorscheme and GUI font
colorscheme desert
set guifont=Monofur:h12
" Highlight current line with bold font, dark shadow background
" Both for color terminal and gui window
" Color code 59: Grey 37
set cursorline
hi CursorLine cterm=bold ctermbg=59 ctermfg=NONE
hi CursorLine gui=bold guibg=#666666 guifg=NONE
" Set window length and width
set lines=50 columns=128
" Set UTF-8 as standard encoding and en_US as the standard
" language
set encoding=UTF-8
" Use dos as the standard file type
set fileformat=dos
set fileformats=dos,unix
" --------------------------------------------------------------
" -> Backup, undo and swap
" --------------------------------------------------------------
" Turn off backup, writebackup, undo and swap file
set nobackup
set nowritebackup
set noundofile
set noswapfile
" --------------------------------------------------------------
" -> Fold, tab and indent
" --------------------------------------------------------------
" Map ,z to create fold based on indent.
nnoremap <silent> <leader>z :set foldmethod=indent<CR>
" Map <Space> to open and close fold in file.
nnoremap <silent> <space> @=(foldlevel('.')?'za':"\<space>")<CR>
" Use spaces instead of tab
set expandtab
" Be smart when using tab
set smarttab
" 1 tab is replaced with 2 spaces
set shiftwidth=2
set tabstop=2
" Number of spaces that Tab counts for when performing editing
" operations
set softtabstop=2
" Round indent to multiple of shiftwidth
set shiftround
" When opening a new line and no filetype specific indent is
" enabled, keep the same indent as the line you are currently
" on
" Useful for README, for example
set autoindent
" Do smart autoindent when starting a new line
set smartindent
" Wrap the long lines when displaying
set wrap
" --------------------------------------------------------------
" -> Visual mode
" --------------------------------------------------------------
" In visual mode pressing * or # searches for the current
" selection
" This is idea from Michael Naumann
vnoremap <silent> * :<C-U>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
vnoremap <silent> # :<C-U>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
" --------------------------------------------------------------
" -> Movement, tab and buffer
" --------------------------------------------------------------
" Map Ctrl + L (redraw screen) to also turn off search
" highlighting until the next search
nnoremap <C-L> :nohl<CR><C-L>
" Convenient way to move between windows
nnoremap <leader>j <C-W>j
nnoremap <leader>k <C-W>k
nnoremap <leader>h <C-W>h
nnoremap <leader>l <C-W>l
" Mappings for managing tabs
nnoremap <leader>t :tabnew<CR>
nnoremap <leader>to :tabonly<CR>
nnoremap <leader>tc :tabclose<CR>
nnoremap <leader>tn :tabnext<CR>
nnoremap <leader>tp :tabprevious<CR>
nnoremap <leader>tm :tabmove<CR>
" Map ,tl toggle between this and the most recently accessed tab
let g:lasttab = 1
nnoremap <leader>tl :exe "tabn ".g:lasttab<CR>
autocmd TabLeave * let g:lasttab = tabpagenr()
" Opens a new tab with the current buffer's path
" Useful when editing files in the same directory
nnoremap <leader>te :tabedit <C-R>=expand("%:p:h")<CR>/
" Close the current buffer
nnoremap <leader>bd :bd<CR>
" Close all the buffers
nnoremap <leader>ba :bufdo bd<CR>
" Map Tab to go to the next buffer and Shift + Tab to go to the
" previous buffer
nnoremap <tab> :bnext<CR>
nnoremap <s-tab> :bprevious<CR>
" --------------------------------------------------------------
" -> Status line
" --------------------------------------------------------------
" Always show the status line
set laststatus=2
" Format the status line
set statusline=%F\ %y\ %m%r:\ %l/%L,\ %c\ %=\ CWD:\ %{getcwd()}\ line:\ %p%%\ /\ display:\ %P
" --------------------------------------------------------------
" -> Editing mapping
" --------------------------------------------------------------
" Map 0 to first non-blank character
nnoremap 0 ^
" Move a line of text using Ctrl + [DU], normal and visual mode
nnoremap <C-D> mz:m+<CR>`z
nnoremap <C-U> mz:m-2<CR>`z
vnoremap <C-D> :m'>+<CR>`<my`>mzgv`yo`z
vnoremap <C-U> :m'<-2<CR>`>my`<mzgv`yo`z
" Map ,e to edit the _vimrc file
nnoremap <leader>e :e C:\Program\ Files\ (x86)\Vim\_vimrc<CR>
" Map ,p to edit the plugin.vim file
nnoremap <leader>p :e C:\Program\ Files\ (x86)\Vim\plugin.vim<CR>
" Map ,q to exit without saving, and add 'a' for all
nnoremap <leader>q :q!<CR>
nnoremap <leader>aq :qa!<CR>
" Map ,w to save without exiting, and add 'a' for all
nnoremap <leader>w :wq<CR>
nnoremap <leader>aw :wqa<CR>
" --------------------------------------------------------------
" -> Spell check
" --------------------------------------------------------------
" Pressing ,ss will toggle spell check
nnoremap <leader>ss :setlocal spell!<CR>
" Move to next or previous misspelled word
nnoremap <leader>sp [s
nnoremap <leader>sn ]s
" --------------------------------------------------------------
" -> Misc
" --------------------------------------------------------------
" Map F2 to toggle paste mode
set pastetoggle=<F2>
" Remove the Windows ^M when the encodings gets messed up
nnoremap <leader>m mmHmt:%s/<C-V><CR>//ge<CR>'tzt'm
" Return to last edit position when opening files
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" Delete trailing white space on save, for some file types
autocmd BufWritePre *.txt,*.js,*.sh,*.py :call CleanTrailingSpace()
" When vimrc is modified, reload it automatically
autocmd BufWritePost _vimrc source C:\Program\ Files\ (x86)\Vim\_vimrc
" --------------------------------------------------------------
" -> Plugin
" --------------------------------------------------------------
" Plugin configurations are sourced externally
source C:\Program\ Files\ (x86)\Vim\plugin.vim
" --------------------------------------------------------------
" -> Function
" --------------------------------------------------------------
" Get visual selection content
function! VisualSelection(direction, extra_filter) range
let l:saved_reg = @"
execute "normal! gvy"
let l:pattern = escape(@", "\\/.*'$^~[]")
let l:pattern = substitute(l:pattern, "\n$", "", "")
let @/ = l:pattern
let @" = l:saved_reg
endfunction
" Function for cleaning trailing spaces
function! CleanTrailingSpace()
let backup_cursor = getpos(".")
let backup_query = getreg("/")
" Find one or more spaces followed by eol and replace
silent! %s/\s\+$//e
call setpos(".", backup_cursor)
call setreg("/", backup_query)
endfunction