天天看點

Vim配置檔案以及Vim插件

自己用的Vim配置

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新檔案标題
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"建立.c,.h,.sh,.java檔案,自動插入檔案頭 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.py,*.java exec ":call SetTitle()" 
""定義函數SetTitle,自動插入檔案頭 
func SetTitle() 
    "如果檔案類型為.sh檔案 
    if &filetype == 'sh' 
        call setline(1,"\#!/bin/bash") 
        call append(line("."),"\#########################################################################") 
        call append(line(".")+1, "\# File Name:".expand("%"))
        call append(line(".")+2, "\# Author:Donny")
        call append(line(".")+3, "\# Email:[email protected]")
        call append(line(".")+4, "\# Created Time: ".strftime("%c"))
        call append(line(".")+5, "\#########################################################################")
        call append(line(".")+6, "")

    elseif &filetype == 'py'
        call setline(1,"\#!/usr/bin/env python")
        call append(line("."),"\#########################################################################")
        call append(line(".")+1, "\# File Name: ".expand("%"))
        call append(line(".")+2, "\# Author:Donny")
        call append(line(".")+3, "\# Email:[email protected]")
        call append(line(".")+4, "\# Created Time: ".strftime("%c"))
        call append(line(".")+5, "\#########################################################################")
        call append(line(".")+6, "")

    else
        call setline(1, "/*#########################################################################") 
        call append(line("."), "\# File Name: ".expand("%")) 
        call append(line(".")+1, "\# Author:Donny")
        call append(line(".")+2, "\# Email:[email protected]")
        call append(line(".")+3, "\# Created Time: ".strftime("%c"))
        call append(line(".")+4, "#########################################################################*/")
        call append(line(".")+5, "")

    endif
    if &filetype == 'cpp'
        call append(line(".")+6, "#include <bits/stdc++.h>")
        call append(line(".")+7, "using namespace std;")
        call append(line(".")+8, "")
    endif
    if &filetype == 'c'
        call append(line(".")+6, "#include<stdio.h>")
        call append(line(".")+7, "")
    endif
    "建立檔案後,自動定位到檔案末尾
endfunc 
autocmd BufNewFile * normal G

" ===
" === Auto load for first time uses
" ===
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

"按r編譯運作
map r :call CompileRunGcc()<CR>
func! CompileRunGcc()
  exec "w"
  if &filetype == 'c'
    exec "!g++ % -o %<"
    exec "!time ./%<"
  elseif &filetype == 'cpp'
    exec "!g++ % -o %<"
    exec "!time ./%<"
  elseif &filetype == 'java'
    exec "!javac %"
    exec "!time java %<"
  elseif &filetype == 'sh'
    :!time sh %
  elseif &filetype == 'python'
    silent! exec "!clear"
    exec "!time python %"
  elseif &filetype == 'html'
    exec "!firefox % &"
  elseif &filetype == 'markdown'
    exec "MarkdownPreview"
  elseif &filetype == 'vimwiki'
    exec "MarkdownPreview"
  endif
endfunc
"C,C++的調試

map <F8> :call Rungdb()<CR>

func! Rungdb()

    exec "w"

    exec "!g++ % -g -o %<"

    exec "!gdb ./%<"

endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""實用設定
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"設定LEADER鍵為空格
let mapleader=" "
"插件舊版本的一個東西
set nocompatible
"智能大小寫
set smartcase
"自動切換工作目錄
set autochdir
"打開檔案恢複到原來打開的位置
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" 設定當檔案被改動時自動載入
set autoread
"顯示行尾有多餘的空格(包括 Tab 鍵)
set listchars=tab:»■,trail:■
set list
"代碼補全 
set completeopt=preview,menu 
"允許插件  
filetype plugin on
"共享剪貼闆  
set clipboard=unnamed
"在底部顯示,目前鍵入的指令
set showcmd
"從不備份  
"set nobackup
"make 運作
:set makeprg=g++\ -Wall\ \ %
"自動儲存
set autowrite
set ruler                   " 打開狀态欄标尺
set cursorline              " 突出顯示目前行
set magic                   " 設定魔術
" 設定在狀态行顯示的資訊
set foldcolumn=0
set foldmethod=indent 
set foldlevel=3
set foldenable              " 開始折疊
" 不要使用vi的鍵盤模式,而是vim自己的
set nocompatible
" 文法高亮
set syntax=on
" 去掉輸入錯誤的提示聲音
set noeb
" 在處理未儲存或隻讀檔案的時候,彈出确認
set confirm
" 自動縮進
set autoindent
set cindent
" Tab鍵的寬度
set tabstop=4
" 統一縮進為4
set softtabstop=4
set shiftwidth=4
" 不要用空格代替制表符
set noexpandtab
" 在行和段開始處使用制表符
set smarttab
" 顯示行号
"set number
"檢查英語單詞拼寫
"set spell spelllang=en_us
" 曆史記錄數
set history=1000
"禁止生成臨時檔案
set nobackup
set noswapfile
"搜尋忽略大小寫
set ignorecase
"搜尋逐字元高亮
set hlsearch
set incsearch
"取消搜尋高亮快捷鍵"
noremap <LEADER><CR> :nohlsearch<CR>
"行内替換
set gdefault
"編碼設定
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"語言設定
set langmenu=zh_CN.UTF-8
set helplang=cn
" 我的狀态行顯示的内容(包括檔案類型和解碼)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
" 總是顯示狀态行
set laststatus=2
" 指令行(在狀态行下)的高度,預設為1,這裡是2
set cmdheight=2
" 偵測檔案類型
filetype on
" 載入檔案類型插件
filetype plugin on
" 為特定檔案類型載入相關縮進檔案
filetype indent on
filetype plugin indent on
" 儲存全局變量
set viminfo+=!
" 帶有如下符号的單詞不要被換行分割
set iskeyword+=_,$,@,%,#,-
" 字元間插入的像素行數目
set linespace=0
" 增強模式中的指令行自動完成操作
set wildmenu
" 使回格鍵(backspace)正常處理indent, eol, start等
set backspace=2
" 允許backspace和光标鍵跨越行邊界
set whichwrap+=<,>,h,l
" 可以在buffer的任何地方使用滑鼠(類似office中在工作區輕按兩下滑鼠定位)
"set mouse=a
"set selection=exclusive
"set selectmode=mouse,key
" 通過使用: commands指令,告訴我們檔案的哪一行被改變過
set report=0
" 在被分割的視窗間顯示空白,便于閱讀
set fillchars=vert:\ ,stl:\ ,stlnc:\
" 高亮顯示比對的括号
set showmatch
" 比對括号高亮的時間(機關是十分之一秒)
set matchtime=1
" 光标移動到buffer的頂部和底部時保持3行距離
set scrolloff=3
" 為C程式提供自動縮進
set smartindent


"設定分屏快捷鍵
"禁用s快捷鍵"
map s <nop>
"sl:右sh:左sk:上sj:下"
map sl :set splitright<CR>:vsplit<CR>
map sh :set nosplitright<CR>:vsplit<CR>
map sk :set nosplitbelow<CR>:split<CR>
map sj :set splitbelow<CR>:split<CR>

"設定分屏後光标移到方向
map . <C-w><C-w>

"設定上下左右移動分屏大小
"map <up> :res +5<CR>
"map <down> :res -5<CR>
"map <left> :vertical resize-5<CR>
"map <right> :vertical resize+5<CR>
map <LEADER>k :res +5<CR>
map <LEADER>j :res -5<CR>
map <LEADER>h :vertical resize-5<CR>
map <LEADER>l :vertical resize+5<CR>


"自動補全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
":inoremap [ []<ESC>i
":inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
	if getline('.')[col('.') - 1] == a:char
		return "\<Right>"
	else
		return a:char
	endif
endfunction
filetype plugin indent on 
"打開檔案類型檢測, 加了這句才可以用智能補全
set completeopt=longest,menu
"自動補全指令時候使用菜單式比對清單
set wildmenu
autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')

Plug 'vim-airline/vim-airline'
Plug 'connorholyday/vim-snazzy'

" Undo Tree
Plug 'mbbill/undotree/'

" File navigation
Plug 'preservim/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'

" Auto Complete
Plug 'ycm-core/YouCompleteMe'
"Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Other visual enhancement
Plug 'nathanaelkane/vim-indent-guides'
Plug 'itchyny/vim-cursorword'


" Python
Plug 'vim-scripts/indentpython.vim'

" Java Complete
Plug 'artur-shaik/vim-javacomplete2'



call plug#end()

" ===
" === UNDOTree
" ===
map ut :UndotreeToggle<CR>

" ===
" === NERDTree
" ===
map tt :NERDTreeToggle<CR>
let NERDTreeMapOpenExpl = ""
let NERDTreeMapUpdir = ""
let NERDTreeMapUpdirKeepOpen = "l"
let NERDTreeMapOpenSplit = ""
let NERDTreeOpenVSplit = ""
let NERDTreeMapActivateNode = "i"
let NERDTreeMapOpenInTab = "o"
let NERDTreeMapPreview = ""
let NERDTreeMapCloseDir = "n"
let NERDTreeMapChangeRoot = "y"


" ==
" == NERDTree-git
" ==
let g:NERDTreeGitStatusIndicatorMapCustom = {
    \ "Modified"  : "✹",
    \ "Staged"    : "✚",
    \ "Untracked" : "✭",
    \ "Renamed"   : "➜",
    \ "Unmerged"  : "═",
    \ "Deleted"   : "✖",
    \ "Dirty"     : "✗",
    \ "Clean"     : "✔︎",
    \ "Unknown"   : "?"
    \ }

" ===
" === You Complete ME
" ===
nnoremap gd :YcmCompleter GoToDefinitionElseDeclaration<CR>
nnoremap g/ :YcmCompleter GetDoc<CR>
nnoremap gt :YcmCompleter GetType<CR>
nnoremap gr :YcmCompleter GoToReferences<CR>

"javacomplete2 java自動補全
autocmd FileType java setlocal omnifunc=javacomplete#Complete



" ===
" === Python-syntax
" ===
let g:python_highlight_all = 1
" let g:python_slow_sync = 0