1 " yyq's vimrc
  2 
  3 " Initialization... {{{
  4 if has('autocmd')
  5         " Remove ALL autocommands for the current group
  6         au!
  7 endif
  8 
  9 set nocompatible        "disable backwards-compatible with vi
 10 filetype off
 11 
 12 set viminfo=<100,'100,/50,:100,h,r$TEMP:,s10
 13 "           |    |    |   |    | |       + 不保存超过10KB寄存器
 14 "           |    |    |   |    | + 不保存TEMP目录下文件的相关信息
 15 "           |    |    |   |    + 载入viminfo文件时关闭hlsearch高亮
 16 "           |    |    |   + 保存命令历史条数
 17 "           |    |    + 保存搜索历史条数
 18 "           |    + 保存最近100个文件中的标记
 19 "           + 每个寄存器中保存的行数
 20 
 21 " }}}
 22 
 23  " General {{{
 24 source $VIMRUNTIME/vimrc_example.vim
 25 source $VIMRUNTIME/mswin.vim
 26 behave mswin
 27 
 28 if has('win32') || has ('win64')
 29         let $VIMFILES = '""' . $VIM . '\vimfiles"'
 30 else
 31         let $VIMFILES = $HOME."/.vim"
 32 endif
 33 
 34 set t_Co=256
 35 set background=dark
 36 
 37 set path+=./**
 38 set backup              " make backup files
 39 set backupdir=C:\Temp   " where to put backup files
 40 set backupskip=C:\Temp  " Don’t create backups when editing files in certain directories
 41 set noswapfile          " no swap files
 42 set undofile            " make undo files
 43 set undodir=C:\Temp
 44 set undolevels=1000 " use many muchos levels of undo
 45 set history=1000 " remember more commands and<LeftMouse> search history
 46 set ignorecase    " ignore case when searching
 47 set infercase     " ignore case whet auto-complete
 48 set smartcase     " ignore case if search pattern is all lowercase, case-sensitive otherwise
 49 set smarttab      " insert tabs on the start of a line according to shiftwidth, not tabstop
 50 set backspace=indent,eol,start        " allow backspacing over everything in insert mode
 51 set confirm       " Show confirm dialog
 52 set autoindent    " always set autoindenting on
 53 set copyindent    " copy the previous indentation on autoindenting
 54 set showmatch     " set show matching parenthesis
 55 set incsearch     " show search matches as you type
 56 set visualbell    " Silence the bell, use a flash instead
 57 set showcmd       " Show (partial) command in status line
 58 set showmode      " Show the current mode
 59 set wildmenu      " turn on command line completion wild style
 60 set wildmode=list:longest,full
 61 set wildignore=*.dll,*.exe,*.gif,*.jpg,*.mm ",*.png,*.snag,*.ssd,*.xmind
 62 set esckeys       " Allow cursor keys in insert mode
 63 set modeline      " Respect modeline in files
 64 set modelines=4
 65 set report=0      " Always display message
 66 set shortmess-=S  "display number of search matches & index of a current match
 67 set dictionary=D:\cygwin64\usr\share\dict\linux.words
 68 set thesaurus=$HOME\vimfiles\thesaurus\english.txt
 69 
 70 "set gdefault      " applies substitutions globally on lines
 71 " }}}
 72 
 73 " Shell {{{
 74 "set shell=$COMSPEC " sets shell to correct path for cmd.exe
 75 
 76 " Makes bash open in the working directory
 77 "let $CHERE_INVOKING=1
 78 " Default path for Cygwin 64-bit, change accordingly
 79 "set shell=D:\cygwin64\bin\bash.exe
 80 " Without --login, Cygwin won't mount some directories such as /usr/bin/
 81 "set shellcmdflag=--login\ -c
 82 " Default value is (, but bash needs "
 83 set shellxquote=\"
 84 " Paths will use / instead of \
 85 "set shellslash
 86 " PATH to determine available checkers
 87 "let $PATH .= ';D:\cygwin64\bin'
 88 " }}}
 89 
 90 " Lines {{{
 91 "set whichwrap=b,s,h,l,<,>,~,[,] " everything wraps
 92 "             | | | | | | | | |
 93 "             | | | | | | | | +-- "]" Insert and Replace
 94 "             | | | | | | | +-- "[" Insert and Replace
 95 "             | | | | | | +-- "~" Normal
 96 "             | | | | | +-- <Right> Normal and Visual
 97 "             | | | | +-- <Left> Normal and Visual
 98 "             | | | +-- "l" Normal and Visual (not recommended)
 99 "             | | +-- "h" Normal and Visual (not recommended)
100 "             | +-- <Space> Normal and Visual
101 "             +-- <BS> Normal and Visual
102 
103 " don't wrap lines
104 set wrap
105 map <F2> <Esc>:set nowrap<CR>
106 map <S-F2> <Esc>:set wrap<CR>
107 " Makes gj/gk move by virtual lines when used without a count, and by physical lines when used with a count.
108 noremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j')
109 noremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k')
110 
111 " Like gJ, but always remove spaces
112 fun! JoinSpaceless()
113     execute 'normal gJ'
114     " Character under cursor is whitespace?
115     if matchstr(getline('.'), '\%' . col('.') . 'c.') =~ '\s'
116         " When remove it!
117         execute 'normal dw'
118     endif
119 endfun
120 nnoremap <F10> :call JoinSpaceless()<CR>
121 " }}}
122 
123 " Paste {{{
124 " Vim will switch to paste mode, disabling all kinds of smartness and 
125 " just pasting a whole buffer of text. 
126 " When in paste-mode auto indent will be turned off.
127 set pastetoggle=<F5>
128 
129 set paste
130 " Use the OS clipboard by default
131 set clipboard=unnamed
132 " }}}
133 
134 " UI {{{
135 " highlight the current column&line
136 set cursorline cursorcolumn
137 highlight Cursor guifg=white guibg=#1874cd
138 highlight iCursor guifg=white guibg=#1874cd
139 highlight CursorLine guibg=white
140 
141 " set the menu & the message to English
142 set langmenu=en_US
143 let $LANG = 'en_US'
144 source $VIMRUNTIME/delmenu.vim
145 source $VIMRUNTIME/menu.vim
146 
147 set number
148 " set relativenumber
149 hi CursorLineNr guifg=silver
150 
151 set mouse=a                             " enable full mouse support in the console
152 autocmd GUIEnter * simalt ~x            " 启动时最大化
153 set ruler                               " Always show current positions along the bottom
154 
155 "}}}
156 
157 " Menus {{{
158 amenu <silent>Tabs.New<TAB>Open\ a\ new\ tab :tabnew<CR>
159 amenu Tabs.-SEPT1- :
160 amenu <silent>Tabs.&Next :tabnext<CR>
161 amenu <silent>Tabs.&Previous :tabprevious<CR>
162 amenu Tabs.-SEPT2- :
163 amenu Tabs.&Close :confirm tabclose<CR>
164 "}}}
165 
166 " ToolBar {{{
167 " Do not install the ToolBar if it is already done.
168 "if !exists("did_install_custom_toolbar")
169 "    let did_install_custom_toolbar = 1
170 
171 amenu 1.10 ToolBar.New :new<CR>
172 amenu 1.20 ToolBar.Open :browse confirm e<CR>
173 
174 an 1.400 ToolBar.-sep8- <Nop>
175 an icon=WinMax 1.410 ToolBar.Maximize <C-w>_
176 an icon=WinVSplit 1.420 ToolBar.ResizeEqual <C-w>=
177 
178 "}}}
179 
180 " ToolTips {{{
181 tmenu 1.10 ToolBar.New New
182 tmenu 1.20 ToolBar.Open Open
183 tmenu 1.30 ToolBar.Save Save
184 tmenu 1.40 ToolBar.SaveAll Save All
185 tmenu 1.50 ToolBar.Print Print
186 tmenu 1.60 ToolBar.Undo Undo
187 tmenu 1.70 ToolBar.Redo Redo
188 tmenu 1.80 ToolBar.Cut Cut
189 tmenu 1.90 ToolBar.Copy Copy
190 tmenu 1.100 ToolBar.Paste Paste
191 tmenu 1.310 ToolBar.Maximize Maximize Buffer
192 tmenu 1.320 ToolBar.ResizeEqual Resize Equal Buffer
193 "}}}
194 
195 " File {{{
196 set fileencoding=utf-8
197 set encoding=utf-8
198 set tenc=utf-8
199 set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
200 
201 "language message zh_CN.UTF-8
202 language message en_US.UTF-8
203 
204 set autoread            "error alert for destructive write
205 
206 "print the current clock time
207 set printheader=%<%f%h%m%40{strftime(\"%I:%M:%S\ \%p,\ %a\ %b\ %d,\ %Y\")}%=Page\ %N
208 
209 "Print the last modification time
210 set printheader=%<%f%h%m\ %40{strftime(\"%c\",getftime(expand(\"%%\")))}%=Page\ %N
211 
212 "Turning on options for filetypes; detection, plugins, and indentation
213 filetype plugin on
214 
215 if has("autocmd") && exists("+omnifunc")
216 autocmd Filetype *
217 \ if &omnifunc == "" |
218 \ setlocal omnifunc=syntaxcomplete#Complete |
219 \ endif
220 endif
221 
222 " using vimdiff from within vim
223 au FilterWritePre * if &diff | colorscheme default | endif
224 
225 " calling vimdiff from the command-line
226 if &diff
227     colorscheme default
228 else
229     colorscheme Tomorrow-Night "xoria256
230 endif
231 " }}}
232 
233 " statusline {{{
234 set laststatus=2 "enable status line always
235 set statusline=%2*%n%m%r%h%w%*\ %F\ %1*[FORMAT=%2*%{&ff}:%{&fenc!=''?&fenc:&enc}%1*]\ [TYPE=%2*%Y%1*]\ [COL=%2*%03v%1*]\ [ROW=%2*%03l%1*/%3*%L(%p%%)%1*]
236 
237 function! InsertStatuslineColor(mode)
238   if a:mode == 'i'
239     hi statusline guibg=peru
240   elseif a:mode == 'r'
241     hi statusline guibg=blue
242   else
243     hi statusline guibg=#1C1C1C
244   endif
245 endfunction
246 
247 au InsertEnter * call InsertStatuslineColor(v:insertmode)
248 au InsertLeave * hi statusline guibg=#1C1C1C guifg=white
249 
250 hi statusline guibg=#1C1C1C
251 hi User1 guifg=gray
252 hi User2 guifg=peru
253 hi User3 guifg=white
254 hi User4 gui=bold
255 " }}}
256 
257 " Font {{{
258 if has('gui_running')
259         " 确保所有的文件类型会在菜单“语法”(“Syntax”)下出现,而不是出现一个菜单项“Show filetypes in menu”
260         "let do_syntax_sel_menu=1
261         set guioptions-=m
262         set guioptions-=T
263         set guioptions-=r
264         set guioptions-=l
265         set guioptions-=b
266         " Alternate open/close menu/toolbar
267         nnoremap <C-F1> :if &go=~#'m'<Bar>set go-=m<Bar>else<Bar>set go+=m<Bar>endif<CR>
268         nnoremap <C-F2> :if &go=~#'T'<Bar>set go-=T<Bar>else<Bar>set go+=T<Bar>endif<CR>
269         nnoremap <C-F3> :if &go=~#'r'<Bar>set go-=r<Bar>else<Bar>set go+=r<Bar>endif<CR>
270 
271         if has("win16") || has("win32") || has("win95") || has("win64")
272                 set guifont=Sarasa\ Mono\ SC:h11,SpaceMono\ NF:h11,Inconsolata:h12,Consolas:h11,Courier_New:h11:cANSI
273                 "set guifontwide=Sarasa\ Mono\ SC:h11
274         else
275                 set guifont=MiscFixed\ Semi-Condensed\ 10
276         endif
277 endif
278 " }}}
279 
280 " Maps {{{
281 
282 " Alternate open/close a fold
283 nnoremap <space> za
284 "nmap <tab> V>
285 "nmap <s-tab> V<
286 "vmap <tab> >gv
287 "vmap <s-tab> <gv
288 " Stay in visual mode when indenting.
289 vnoremap < <gv
290 vnoremap > >gv
291 
292 nnoremap <C-Insert> :tabnew<CR>
293 nnoremap <C-Delete> :tabclose<CR>
294 
295 noremap <silent><tab>m :tabnew<cr>
296 noremap <silent><tab>e :tabclose<cr>
297 noremap <silent><tab>n :tabn<cr>
298 noremap <silent><tab>p :tabp<cr>
299 noremap <silent><s-tab> :tabnext<CR>
300 inoremap <silent><s-tab> <ESC>:tabnext<CR>
301 
302 " Search for visually highlighted text
303 vmap <silent> //    y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
304 
305 " Open help on current word in a new tab
306 noremap <silent> <F1> :execute "tab h " . expand("<cword>")<cr>
307 
308 " 显示/禁止行列光标
309 nmap <silent> <F6> <Esc>:call ToggleCursor()<CR>
310 
311 " Determining the highlight group that the word under the cursor belongs to
312 nmap <silent> <F7>   :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
313 
314 " Show all buffers in tabs, or to close all tabs (toggle: it alternately executes  :tab ball and :tabo).
315 let notabs = 0
316 nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
317 
318 function! ToggleCursor()
319      if &cursorcolumn
320           set nocursorline nocursorcolumn
321      else
322           set cursorline cursorcolumn
323      endif
324 endfunction
325 
326 " clear highlighted searches
327 nmap <silent> ,/ :nohlsearch<CR>
328 
329 function! ToggleHLSearch()
330         if &hls
331                 set nohls
332         else
333                 set hls
334         endif
335 endfunction
336 
337 " Using very magic mode 
338 nnoremap / /\v
339 vnoremap / /\v
340 cnoremap %s/ %s/\v
341 nnoremap :g/ :g/\v
342 
343 set grepprg=grep\ -rnIH\ --exclude-dir=.git
344 
345 let mapleader="," "Default \
346 
347 nmap <silent> <leader><space> <Esc>:call ToggleHLSearch()<CR>
348 
349 "Close tag
350 "inoremap <lt>/ </<C-x><C-o>
351 inoremap <C-Enter> </<C-x><C-o>
352 
353 " Mark any tabs that are not at the beginning of the lines
354 nnoremap <leader>mt :match errorMsg /[^\t]\zs\t\+/<CR>
355 
356 " Quickly edit/reload the vimrc file
357 nmap <silent> <leader>tv :tabe $MYVIMRC<CR>
358 nmap <silent> <leader>sv :so $MYVIMRC<CR>
359 autocmd BufWritePost $MYVIMRC source $MYVIMRC
360 
361 " Reselect the text that was just pasted
362 nnoremap <leader>p V`]
363 
364 " Open learn-vi folder
365 let g:browsefilter = "*.*"
366 nnoremap <leader>v :browse tabe E:\Anthony_GitHub\learn-vim<CR>
367 
368 " 打开另存为对话框
369 nnoremap <leader>w :browse sav<CR>
370 
371 " Set current directory to current file with ,cd
372 nnoremap <leader>cd :cd %:p:h<CR>:pwd<CR>
373 "open windows command prompt in the current file's directory
374 nnoremap <leader>cc :!start cmd /k cd %:p:h:8<cr>
375 "open explorer in the current file's directory
376 nnoremap <leader>ce :!start explorer %:p:h:8<cr>
377 
378 " 复制当前文件/路径到剪贴板
379 nmap <leader>fn :let @*=substitute(expand("%"), "/", "\\", "g")<CR>
380 nmap <leader>fp :let @*=substitute(expand("%:p"), "/", "\\", "g")<CR>
381 
382 vnoremap <leader>a <c-a>
383 vnoremap <leader>x <c-x>
384 
385 " toggle use of 'modeline' command.  Doesn't reload the
386 " settings until you do BufRead.  vimrc doesn't support the command
387 " separator '|' so we use '<bar>':
388  nnoremap <leader>ml :setlocal invmodeline <bar> doautocmd BufRead<cr>
389 
390 " }}}
391 
392 " Improve performance {{{
393 autocmd BufWinLeave * call clearmatches()
394 set nospell
395 " }}}
396 
397 " Abbreviations {{{
398 " Ctrl-] is used to expand an abbreviation without inserting any extra characters
399 abbreviate #b /**********************
400 abbreviate #e **********************/
401 iabbrev pcode <p style="text-indent:2em"><code class="inset"></code></p><Esc>2F<i
402 iabbrev icode <code class="inset"></code><Esc>F<i
403 "iabbrev icode <code class="inset">!cursor!</code><Esc>:call search('!cursor!','b')<cr>cf!
404 "cabbrev h tab h
405 " }}}
406 
407 " Others {{{
408 
409 " Template
410 autocmd! BufNewFile * silent! 0r $VIM/vimfiles/skel/Template.%:e
411 
412 " Fold
413 set foldenable              " Turn on folding
414 set foldmethod=marker       " Fold on the marker
415 set foldcolumn=2
416 
417 " Show invisible characters
418 set list
419 set listchars=tab:\|.,trail:,nbsp:.,extends:¬
420 " Enter the middle-dot by pressing Ctrl-k then .M
421 " Enter the right-angle-quote by pressing Ctrl-k then >>
422 " Enter the Pilcrow mark by pressing Ctrl-k then PI
423 " The command :dig displays other digraphs you can use.
424 
425 " save on losing focus
426 au FocusLost * :wa
427 
428 " visually select text then press ~ to convert the text to  UPPER CASE,
429 " then to lower case, then to Title Case.
430 function! TwiddleCase(str)
431   if a:str ==# toupper(a:str)
432     let result = tolower(a:str)
433   elseif a:str ==# tolower(a:str)
434     let result = substitute(a:str,'\(\<\w\+\>\)', '\u\1', 'g')
435   else
436     let result = toupper(a:str)
437   endif
438   return result
439 endfunction
440 vnoremap ~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgvl
441 
442 " }}}
443 
444 " HTML {{{
445 " Escape/unescape & < > HTML entities in range (default current line).
446 function! HtmlEntities(line1, line2, action)
447         let search = @/
448         let range = 'silent ' . a:line1 . ',' . a:line2
449         if a:action == 0 " must convert &amp; last
450                 execute range . 'sno/&lt;/</eg'
451                 execute range . 'sno/&gt;/>/eg'
452                 execute range . 'sno/&amp;/&/eg'
453         else " must convert & first
454                 execute range . 'sno/&/&amp;/eg'
455                 execute range . 'sno/</&lt;/eg'
456                 execute range . 'sno/>/&gt;/eg'
457         endif
458         nohl
459         let @/ = search
460 endfunction
461 
462 command! -range -nargs=1 HEntities call HtmlEntities(<line1>, <line2>, <args>)
463 
464 noremap <silent> <Leader>hu :HEntities 0<CR>
465 noremap <silent> <Leader>he :HEntities 1<CR>
466 
467 map <silent> <F9> <Esc>:HEntities 1<CR>
468 
469 " }}}
470 
471 " Autocommand {{{
472 
473 augroup filetypedetect
474         au BufNewFile,BufRead *.csc setfiletype csc
475         au BufNewFile,BufRead *.msh setfiletype mxl
476         au BufNewFile,BufRead *.mxl setfiletype mxl
477         au BufNewFile,BufRead *.rle setfiletype rle
478         au BufNewFile,BufRead *.log setfiletype log
479 augroup end
480 
481 "au Filetype html,xml,xsl source $VIM/vimfile/plugin/closetag.vim
482 "au FileType html,xml set omnifunc=xmlcomplete#CompleteTags
483 
484 augroup vimrc-auto-mkdir
485   autocmd!
486   autocmd BufWritePre * call s:auto_mkdir(expand('<afile>:p:h'), v:cmdbang)
487   function! s:auto_mkdir(dir, force)
488     if !isdirectory(a:dir)
489           \   && (a:force
490           \       || input("'" . a:dir . "' does not exist. Create? [y/N]") =~? '^y\%[es]$')
491       call mkdir(iconv(a:dir, &encoding, &termencoding), 'p')
492     endif
493   endfunction
494 augroup END
495 
496 augroup vgrep
497     autocmd!
498     autocmd QuickFixCmdPost [^l]* cwindow
499     autocmd QuickFixCmdPost l*    lwindow
500 augroup END
501 
502 " }}}
503 
504 " User Defined Command {{{
505 command! -bar DelTab %s/        //
506 command! DelLF %s/\n//
507 command! FmtCode DelTab|DelLF
508 "}}}
509 
510 " Source Code {{{
511 set pythonthreehome=C:\tools\Python3
512 set pythonthreedll=C:\tools\Python3\python38.dll
513 augroup make_python
514         au!
515         au FileType python set makeprg=C:\tools\Python3\python\ %
516 augroup end
517 "}}}
518 
519 " AfterPlugin {{{
520 " 取消用Ctrl-F调用查找窗口
521 unmap <C-F>
522 
523 "避免删除的当字符进入剪切板
524 nnoremap x "_x
525 
526 " show a colored column 
527 " so I can see when I write a too-long line of code)
528 set colorcolumn=70
529 highlight ColorColumn guibg=darkred
530 " }}}