" ================================= FUNCTIONS ================================= function MaximizeWindow() " Used in GNOME silent !wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz endfunction function ShortTabLabel() " Only show filename on tabs let bufnrlist = tabpagebuflist (v:lnum) let label = bufname (bufnrlist[tabpagewinnr (v:lnum) -1]) let filename = fnamemodify (label, ':t') return filename endfunction function WasCommented(str) " Judge is this line a comment (by //) let str_len = strlen(a:str) | let i = 0 while i < str_len if a:str[i] != ' ' && a:str[i] != "\t" | break | endif | let i += 1 endwhile if i < str_len - 1 && a:str[i] == a:str[i+1] && a:str[i] == "/" | return 1 | else | return 0 | endif endfunction function ToggleCommentLine() " Toggle line with // let nr = line('.') | let str_line = getline(nr) if WasCommented(str_line) | let newstr = substitute(str_line, '//', '', '') else | let newstr = substitute(str_line, '^', '//', '') | endif call setline(nr, newstr) endfunction function WhichOpened() " Judge if Tagbar or NERDTree opened. 0 both are not, 1 only Tagbar, 2 only NERDTree, 3 both were opened. let retv = 0 if bufwinnr('__Tagbar__') != -1 | let retv += 1 | endif if bufwinnr('NERD_tree_1') != -1 | let retv += 2 | endif return retv endfunction func ToggleTagbar() let wo = WhichOpened() if (wo == 1 || wo == 3) | exec "TagbarClose" else | exec "TagbarOpen" | endif endf func ToggleNERDTree() let wo = WhichOpened() | exec "NERDTreeClose" if (wo != 2 && wo != 3) | exec "NERDTreeToggle" | exec "normal! \<c-w>h" | endif endf function ToggleFull() " Open or close Tagbar and NERDTree at the same time. let wo = WhichOpened() | exec "NERDTreeClose" if (wo == 0) | exec "NERDTreeToggle" | exec "normal! \<c-w>h" | exec "TagbarOpen" else | exec "TagbarClose" | endif endfunction function FindFilePath() " Let NERDTree unfold to current file. exec 'NERDTreeFind' | exec "normal! \<c-w>h" endfunction function ToggleQuickfix() let winList = getwininfo() for window in winList if window.quickfix == 1 if window.loclist == 1 | continue | else | :ccl | return | endif endif endfor :copen | :exec "normal! \<c-w>J" endfunction function ToggleLoclist() let winList = getwininfo() for window in winList if window.quickfix == 1 if window.loclist != 1 | continue | else | :lcl | return | endif endif endfor :lopen | :exec "normal! \<c-w>J" endfunction function ToggleWrap() if getwinvar(0, '&wrap') == 1 | exe "set nowrap" else | exe "set wrap" | endif endfunction function ShowCurrentTag() " about Tagbar let str = tagbar#currenttag('%s', '', 's') | echo str endfunction function MyChangeDir() let newdir = input("cd: ", strpart(execute('pwd'), 1), "dir") exe 'cd ' . newdir endfunction function CloseWindow() if len(getwininfo()) >= 2 | :q else | exec "echo \'This is the last window!\'" | endif endfunction function DeleteCurBuf() " Delete the buffer and don't close the window let l:cBuf = bufnr("%") if 0 == buflisted(l:cBuf) | exec "echo \'This buffer can not be deleted.\'" | return | endif if buflisted(bufnr("#")) | exec "b#" | else | exec "bp" | endif let l:newBufNum = bufnr("%") if l:cBuf == l:newBufNum | exec "echo \'This is the last opened file.\'" | return | endif exec "bw " . l:cBuf endfunction function SmartCloseWindow() let allWndInfo = getwininfo() if len(allWndInfo) <= 1 | call DeleteCurBuf() | return | endif let currentWndInfo = getwininfo(win_getid())[0] if currentWndInfo.loclist == 1 || currentWndInfo.quickfix == 1 | :q | return | endif if getbufvar(currentWndInfo.bufnr, '&modifiable') == 0 | :q | return | endif " unmodifiable window for winInfo in allWndInfo if winInfo.winnr == currentWndInfo.winnr | continue | endif if getbufvar(winInfo.bufnr, '&modifiable') == 1 | :q | return | endif endfor call DeleteCurBuf() endfunction " about Ack.vim function BuildIgnore() let dir_list = split(g:ack_ignore_dir, ',') let result = ' ' for dirname in dir_list let result = result . '--ignore ' . dirname . ' ' endfor return result endfunction let g:ack_root = ' ' function AckWithOptions(...) let ignores = BuildIgnore() if a:0 > 0 | exec 'Ack! -U' . ignores . a:1 . ' ' . g:ack_root else | exec 'Ack! -U' . ignores . expand('<cword>'). ' ' . g:ack_root | endif endfunction function AckSetSearchRoot() let newackroot = input("Enter a directory to set the root to: ", strpart(execute('pwd'), 1), "dir") if empty(newackroot) | return | endif let g:ack_root = newackroot endfunction command! -nargs=? MyAck call AckWithOptions(<f-args>) command! -nargs=0 MyAckSetRoot call AckSetSearchRoot() " ============================ basic settings ================================== set encoding=utf-8 "設置編輯器默認使用的編碼 if !has("win32") | language en_US.utf8| endif " 爲了保證狀態欄不變特別寬 set nocompatible "不兼容vi if has("gui_running") set guifont=Hurmit\ NF\ medium\ 13 set guioptions-=T "不顯示工具欄 set guioptions-=L "不顯示左邊的滾動條 set guioptions-=r "不顯示右邊的滾動條 set guioptions-=m "不顯示菜單欄 set guitablabel=%{ShortTabLabel()} set vb t_vb= "這兩行是不容許響鈴不閃屏提示 au GuiEnter * set t_vb= endif set number "顯示行號 set hlsearch "高亮搜索結果 set shiftwidth=4 "代碼自動縮進的列數 set tabstop=4 "設置tab字符顯示的列數 set expandtab "用空格代替tab字符 set autoindent "啓動代碼自動縮進功能 set hidden "避免當前編輯文檔未保存時,在新窗口打開新文檔 set cursorline "高亮光標所在行 set textwidth=0 "超事後自動拆行(0表示關閉此功能) set colorcolumn=81,121 "高亮指定的列 set mouse=a "支持鼠標 set completeopt=menu "以彈出菜單的方式顯示補全內容 set ffs=unix,dos "識別指定形式的換行符 set laststatus=2 "老是顯示狀態欄 set nobackup "不生成備份文件 set noundofile "不以文件方式記錄undo list set noswapfile "不生成swap文件 set nowrap "不自動折行 set sidescroll=5 "橫向滾動時一次滾動的列數 set foldmethod=indent "代碼摺疊方式 set nofoldenable "啓動時關閉摺疊 set cscopetag "tag有多個匹配項時能夠提供選擇的機會 set scrolloff=0 "設置光標離屏幕頂底的距離 "set imdisable "禁用輸入法,--謹慎設置-- " ================================ hot keys ==================================== nnoremap <leader>b :ls<CR> nnoremap <leader>c :call ToggleCommentLine()<cr> nnoremap <leader>e :browse confirm e<CR> nnoremap <leader>s :e ~/.vimrc<CR> nnoremap <leader>w :update<CR> nmap ;s <Plug>(easymotion-F) nmap ;f <Plug>(easymotion-f) nmap ;j <Plug>(easymotion-j) nmap ;k <Plug>(easymotion-k) nnoremap <s-Tab> :bn<CR> vnoremap <s-Tab> <esc>:bn<CR> inoremap <s-Tab> <esc>:bn<CR> nnoremap <c-Tab> :b#<CR> vnoremap <c-Tab> <esc>:b#<CR> inoremap <c-Tab> <esc>:b#<CR> nnoremap <c-s-tab> :bp<cr> vnoremap <c-s-tab> <esc>:bp<cr> inoremap <c-s-tab> <esc>:bp<cr> inoremap <c-f> <pagedown> inoremap <c-b> <pageup> nnoremap <m-q> :call SmartCloseWindow()<cr> nnoremap <m-e> :nohl<cr> vnoremap <m-e> <c-c>:nohl<cr> inoremap <m-e> <c-o>:nohl<cr> nnoremap <c-right> 3zl nnoremap <c-s-right> zL nnoremap <c-left> 3zh nnoremap <c-s-left> zH nnoremap <m-w> :call ToggleWrap()<cr> inoremap <m-w> <c-o>:call ToggleWrap()<cr> vnoremap <m-w> <c-c>:call ToggleWrap()<cr> inoremap <c-h> <c-o><bs><del> nmap <m-f3> :MyAck<cr> imap <m-f3> <c-o>:MyAck<cr> nmap <m-f> :MyAck nnoremap <f1> :call ToggleTagbar()<cr> inoremap <f1> <c-o>:call ToggleTagbar()<cr> vnoremap <f1> <c-c>:call ToggleTagbar()<cr> nnoremap <f2> :call ToggleNERDTree()<cr> vnoremap <f2> <c-c>:call ToggleNERDTree()<cr> inoremap <f2> <c-o>:call ToggleNERDTree()<cr> vnoremap <F3> <C-C><ESC>/<C-R>+<CR><ESC>N nnoremap <F3> /<C-R>=expand("<cword>")<CR><CR>N inoremap <F3> <c-o>/<C-R>=expand("<cword>")<CR><CR><c-o>N nnoremap <f5> <esc>:call FindFilePath()<cr> vnoremap <f5> <esc>:call FindFilePath()<cr> inoremap <f5> <esc>:call FindFilePath()<cr> "nmap <F6> <ESC>:CtrlPFunky<cr> "vmap <F6> <ESC>:CtrlPFunky<cr> "imap <F6> <ESC>:CtrlPFunky<cr> nnoremap <F7> <ESC>:CtrlP<CR> vnoremap <F7> <ESC>:CtrlP<CR> inoremap <F7> <ESC>:CtrlP<CR> nnoremap <F8> <ESC>:CtrlPMRU<CR> vnoremap <F8> <ESC>:CtrlPMRU<CR> inoremap <F8> <ESC>:CtrlPMRU<CR> nnoremap <F9> <ESC>:call ToggleQuickfix()<CR> vnoremap <F9> <ESC>:call ToggleQuickfix()<CR> inoremap <F9> <ESC>:call ToggleQuickfix()<CR> nnoremap <f10> <esc>:call ToggleLoclist()<cr> vnoremap <f10> <esc>:call ToggleLoclist()<cr> inoremap <f10> <esc>:call ToggleLoclist()<cr> nnoremap <silent> <F11> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/^/\/\/<CR>/^$x<CR> vnoremap <silent> <F11> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/^/\/\/<CR>/^$x<CR> inoremap <silent> <F11> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/^/\/\/<CR>/^$x<CR> nnoremap <silent> <F12> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/\/\//<CR>/^$x<CR> vnoremap <silent> <F12> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/\/\//<CR>/^$x<CR> inoremap <silent> <F12> <ESC>:<C-R>=line("'<")<CR>,<C-R>=line("'>")<CR>s/\/\//<CR>/^$x<CR> if !has("win32") " Some hot keys like windows vnoremap <C-C> "+y map <C-V> "+gP imap <C-V> <c-o>"+gP cmap <C-V> <C-R>+ noremap <C-S> :update<CR> vnoremap <C-S> <C-C>:update<CR> inoremap <C-S> <C-O>:update<CR> noremap <C-A> gggH<C-O>G inoremap <C-A> <C-O>gg<C-O>gH<C-O>G cnoremap <C-A> <C-C>gggH<C-O>G onoremap <C-A> <C-C>gggH<C-O>G snoremap <C-A> <C-C>gggH<C-O>G xnoremap <C-A> <C-C>ggVG endif " =================================== plugin settings ========================== filetype off " required set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin('~/.vim/bundle') Plugin 'VundleVim/Vundle.vim' Plugin 'easymotion/vim-easymotion' Plugin 'altercation/vim-colors-solarized' Plugin 'tomasr/molokai' Plugin 'mileszs/ack.vim' Plugin 'Yggdroot/LeaderF' " 部分代替ctrlp if has('gui_running') Plugin 'ctrlpvim/ctrlp.vim' Plugin 'majutsushi/tagbar' Plugin 'scrooloose/nerdtree' Plugin 'drmingdrmer/xptemplate' Plugin 'vim-airline/vim-airline' Plugin 'vim-airline/vim-airline-themes' Plugin 'ap/vim-buftabline' "文件列表標籤 endif call vundle#end() " required filetype plugin indent on " required " ack let g:ackprg = 'ag --vimgrep' let g:ack_ignore_dir='target,*.class,*.jar,tags,tags.files' " LeaderF "let g:Lf_ShortcutB = '<c-p>' let g:Lf_WindowHeight = 0.35 nnoremap <leader>f :LeaderfFunction<cr> " ------------------------------------------ if has('gui_running') " Tagbar let g:tagbar_left=1 "顯示在左邊 let g:tagbar_width=30 "窗口寬度 let g:tagbar_compact = 1 "隱藏幫助信息 " NERDTree let NERDTreeWinPos='right' "顯示在右邊 let NERDTreeShowLineNumbers=0 "不顯示行號 let NERDTreeWinSize=30 "窗口寬度 let NERDTreeMinimalUI=1 "隱藏幫助信息 " ctrlp let g:ctrlp_map = '<c-p>' let g:ctrlp_cmd='CtrlPBuffer' let g:ctrlp_by_filename = 1 let g:ctrlp_regexp = 1 let g:ctrlp_working_path_mode = 'rw' let g:ctrlp_max_height = 30 let g:ctrlp_max_files = 100000 let g:ctrlp_use_caching = 1 let g:ctrlp_clear_cache_on_exit = 0 let g:ctrlp_bufpath_mod = '' " 讓Buffer mode不顯示路徑 let g:ctrlp_cache_dir = '~/.ctrlp' let g:ctrlp_root_markers = ['pom.xml', '.project'] let g:ctrlp_custom_ignore = { \ 'dir': '\v[\/](build|bin|target)$', \ 'file': '\v\.(jar|class|MF|xlsx|files|proto|xsd|png|gif|svg|json|gz|less|scss|jpg|swf|ttf)$', \ 'link': 'SOME_BAD_SYMBOLIC_LINKS',} " vim-airline let g:airline_powerline_fonts = 1 let g:airline#extensions#tabline#enabled = 1 let g:airline#extensions#tabline#formatter = 'only_lastname' let g:airline#extensions#whitespace#enabled = 0 " 關閉狀態顯示空白符號計數 let g:airline#extensions#whitespace#symbol = '!' if has('win32') if !exists('g:airline_symbols') | let g:airline_symbols = {} | endif let g:airline_left_sep = '⮀' let g:airline_left_alt_sep = '⮁' let g:airline_right_sep = '⮂' let g:airline_right_alt_sep = '⮃' let g:airline_symbols.branch = '⭠' let g:airline_symbols.readonly = '⭤' let g:airline_symbols.linenr = '⭡' endif endif " ------------------------------------------ " All about Eclim nnoremap <c-leftmouse> <esc>:JavaSearchContext -t all -x implementors -s project -a edit<cr> nnoremap <c-j> <esc>:JavaSearchContext -t all -x implementors -s project -a edit<cr> nnoremap <m-leftmouse> <esc>:JavaSearchContext -t all -x declarations -s project -a edit<cr> nnoremap <m-j> <esc>:JavaSearchContext -t all -x declarations -s project -a edit<cr> let g:EclimDefaultFileOpenAction = 'edit' nnoremap <silent> <buffer> <leader>i :JavaImport<cr> nnoremap <silent> <buffer> <cr> :JavaSearchContext<cr> " ywvim " http://www.vim.org/scripts/script.php?script_id=2662 set noimcmdline let g:ywvim_ims=[ \['wb', '五筆', 'wubi.ywvim'], \['py', '拼音', 'pinyin.ywvim'], \['cj', '倉頡', 'cangjie.ywvim'], \['wb98', '五筆98', 'wubi98.ywvim'], \['zm', '鄭碼', 'zhengma.ywvim'], \['zy', '註音', 'zhuyin.ywvim'], \['ar30', '行列', 'array30.ywvim'], \] let g:ywvim_py = { 'helpim':'wb', 'gb':0 } let g:ywvim_zhpunc = 2 " 設置默認中文標點輸入開關, 1 爲開, 默認打開拼音輸入, 2 爲關 let g:ywvim_listmax = 5 let g:ywvim_esc_autoff = 0 let g:ywvim_autoinput = 2 let g:ywvim_circlecandidates = 1 let g:ywvim_helpim_on = 0 let g:ywvim_matchexact = 0 let g:ywvim_chinesecode = 1 let g:ywvim_gb = 0 let g:ywvim_preconv = 'g2b' let g:ywvim_conv = '' let g:ywvim_lockb = 1 "let g:ywvim_theme = 'dark' colorscheme molokai set bg=dark