-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimrc.vim
318 lines (230 loc) · 7.23 KB
/
vimrc.vim
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
" ------------------选项------------------
set nocompatible
" 不产生备份文件
set nobackup
set ignorecase
set smartcase
set hlsearch
set incsearch
" backspace 删除键
set backspace=indent,eol,start
set expandtab
set wrap
set foldcolumn=1
set number
set history=1000
" set spell
" 保存 bufferlist
set viminfo+=%
" 显示特殊字符(区分 tab 和 空格)
set list
set listchars=tab:,.,trail:.,extends:#,nbsp:. " Highlight problematic whitespace
set wildmenu " show list instead of just completing
set wildmode=list:longest,full " command <Tab> completion, list matches, then longest common part, then all.
set whichwrap=b,s,<,>,[,] " backspace and cursor keys wrap to
set cursorline
set nostartofline
" Start scrolling three lines before the horizontal window border
" set scrolloff=3
set display=lastline
" default fileencoding
set fenc=utf-8
set fencs=utf-8,ucs-bom,euc-jp,gb18030,gbk,gb2312,cp936
" see help (complex)
set fo=tcrqn
" tab spacing (settings below are just to unify it)
set tabstop=2
" unify
set softtabstop=2
" unify
set shiftwidth=2
" set textwidth=78
set textwidth=0
" 状态栏设置
if has('cmdline_info')
set ruler " show the ruler
set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%) " a ruler on steroids
set showcmd " show partial commands in status line and
" selected characters/lines in visual mode
endif
if has('statusline')
set laststatus=2
" Broken down into easily includeable segments
set statusline=%t " Filename
set statusline+=%m%r%h%w " Options
set statusline+=\ [%<%{&ff}/%{&ft}/%{&fenc}] " filetype
" set statusline+=\ [%<%{expand('%:p:h')}] " path to current file
set statusline+=\ [%<%{getcwd()}] " current dir
set statusline+=\ %{fugitive#statusline()} " Git Hotness, 依赖 `Bundle 'tpope/vim-fugitive'`
set statusline+=%=%-10.(%l,%c%V%)\ %p%% " Right aligned file nav info
endif
set diffopt=vertical
set splitright
if has('persistent_undo')
" 无限回退, 确保目录存在
set undodir=~/.vim/vim-undodir
set undofile
endif
" ctrlp 下使用 / 可以过滤文件
" 统一路径分隔符
set shellslash
" set shell=D:/programs/Git/bin/sh.exe
" gf 文件查找问题
set includeexpr=GuessFilename(v:fname)
" 进入 php 文件 help 设置查看 php-manual
set rtp+=~/.vim/php-manual/
autocmd BufNewFile,Bufread *.ros,*.inc,*.php set keywordprg="help"
" 进入 js 文件 https://github.com/guileen/vim-node
autocmd FileType javascript set dictionary+=~/.vim/node-dict/dict/node.dict | set suffixesadd=.js
" 切换 buffer 后立即切换 cwd
autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h | endif
" 自动保存 session
set sessionoptions=buffers,curdir,resize,folds,tabpages
" 注意目录要存在
autocmd VimLeave * mks! ~/.vim/vim-sessions/session.vim
function! ReadSession()
let session_file = $HOME . '/.vim/vim-sessions/session.vim'
if filereadable(session_file)
execute "so " . session_file
else
echo 'session file "' . session_file . '" is not readable!'
endif
endfunction
" 启动 vim 时自动读取 session 文件, 目前重新定义 vim.bat/gvim.bat
" autocmd VimEnter * :call ReadSession()
" markdown .md 后缀支持
au BufRead,BufNewFile *.md set filetype=markdown
syntax on
filetype plugin indent on
colorscheme desert
" ------------------按键映射------------------
" 复制
vnoremap <silent> <S-y> "+y
" 搜索选中
vnoremap <silent> <Leader>/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
vnoremap <silent> <Leader>? y?<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
" search in visual area
" :help \%V
vnoremap # <Esc>/\%V\%V<Left><Left><Left>
" replace in visual area
vnoremap :sv <Esc>:%s;\%V\%V;;<Left><Left><left><Left><Left>
" 打开 cmd
if has('win32')
map <silent> <Leader>st :silent !start<CR>
endif
" 复制当前 ip 到剪切板
" map <silent> <Leader>ip :exe 'silent r!ipconfig' \| exe '/\mIPv4.\{-}\zs\d\{1,3}\(\.\d\{1,3}\)\{3}\ze' \| exe 'normal nvE"+y'<CR>
map <silent> <Leader>ip :call CopyIpAddressToClipboard()<CR>
function! CopyIpAddressToClipboard()
let cmdOutput = system('ipconfig')
let ipAddress = matchstr(cmdOutput, '\mIPv4.\{-}\zs\d\{1,3}\(\.\d\{1,3}\)\{3}\ze', 0)
echo ipAddress
" 复制到系统剪切板
let @+ = ipAddress
endfunction
map <silent> <Leader>q :close<CR><CR>
"" 窗口大小
map <silent> <Leader>> :vert res<CR>
map <silent> <Leader>< :res<CR>
map <silent> <Leader>; <C-W>=
" buffer 切换
nnoremap <silent> [b :bprevious<CR>
nnoremap <silent> ]b :bnext<CR>
nnoremap <silent> [B :bfirst<CR>
nnoremap <silent> ]B :blast<CR>
" command-mode 移动
cnoremap <M-f> <Right>
cnoremap <M-b> <Left>
" insert-mode 移动
imap <silent><C-f> <Right>
imap <silent><C-b> <Left>
imap <silent><M-f> <S-Right>
imap <silent><M-b> <S-Left>
imap <silent><C-a> <Home>
imap <silent><C-e> <End>
imap <silent><M-h> <Left>
imap <silent><M-j> <Down>
imap <silent><M-k> <Up>
imap <silent><M-l> <Right>
" 保存
nmap <silent><C-s> :w<CR>
imap <silent><C-s> <Esc>:w<CR>
nmap <silent><C-S-s> :wa<CR>
imap <silent><C-S-s> <Esc>:wa<CR>
" 快速移动
map <C-k> 5k
map <C-j> 5j
nnoremap j gj
nnoremap k gk
" 移动参考窗口
nnoremap <M-f> <C-w>w<C-f><C-w>w
nnoremap <M-b> <C-w>w<C-b><C-w>w
map zl zL
map zh zH
nnoremap ' `
nnoremap ` '
" 插入当前日期
nnoremap <F7> "=strftime("%c")<CR>P
inoremap <F7> <C-R>=strftime("%c")<CR>
" ------------------command------------------
" 垂直打开帮助窗口
cabbrev h <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'vertical help' : 'h')<CR>
" ------------------函数------------------
" use tidy to beautify html
" make sure tidy is in the path
function! BeautifyHtml()
setlocal equalprg=tidy\ -quiet\ -indent\ -wrap\ 0\ --show-errors\ 0
normal gg=G
endfunction
" 做批量替换时用来产生 唯一 id
" :%s;hello;\="hello-world-" . AutoIncrease();g
let g:auto_increase_index = 0
function! AutoIncrease()
let g:auto_increase_index = g:auto_increase_index + 1
return g:auto_increase_index
endfunction
" below is vim73's default settting
" source $VIMRUNTIME/vimrc_example.vim
" source $VIMRUNTIME/mswin.vim
" behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
" gf 找不到文件时去掉 文件名中的 ./ 和 ../
" :h path
function! GuessFilename(filename)
let ret = ''
if a:filename =~ '^\.\/\.\.\/'
let ret = strpart(a:filename, 5)
elseif a:filename =~ '^\.\.\/'
let ret = strpart(a:filename, 3)
elseif a:filename =~ '^\.\/'
let ret = strpart(a:filename, 2)
endif
if len(ret)
return ret
else
return a:filename
endif
endfunction