# vim 開發環境 設定 ## Vundle 設定 建立.vim目錄,之後下載bundle的目錄到.vim底下。vim ```bash git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 在vimrc裡面加入以下文字確保可使用vundle下載相關外掛套件 ```bash=~/.vimrc set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' Plugin 'VundleVim/Vundle.vim' Plugin 'Valloric/YouCompleteMe' Plugin 'rhysd/vim-clang-format' Plugin 'tpope/vim-surround' Plugin 'Yggdroot/indentLine' Plugin '套件' call vundle#end() ``` ## 基礎設定 vim 基礎設定 ```bash=~/.vim/plugin/default.vim set nocompatible " be iMproved, required filetype off " required set hlsearch set incsearch set cursorline set cursorcolumn set nu set clipboard=unnamed,unnamedplus set splitright "add the vertical window right default set splitbelow "add the horizontal window down default ``` <span style="color:red">**clipboard遇到的問題:**</span> 在設定<span style="background:lightgrey">:set clipboard=unnamed</span>之後無法有剪貼簿和vim裡的register共用的功能。在使用<span style="background:lightgrey">vim --version</span>查看內容發現<span style="color:red">-clipboard</span>,需使用指令<span style="background:lightgrey">sudo apt install vim-gtk3</span>安裝套件後再使用<span style="background:lightgrey">vim --version</span>觀察<span style="color:red">-clipboard</span>變成<span style="color:red">+clipboard</span>便可正常使用。 建立~/.vim/plugin/autocmd.vim,並加入相對應的命令 ```bash! autocmd InsertLeave *.c,*.h :ClangFormat ``` ## 記憶折疊狀態 在~/.vim/plugin/autocmd.vim加入相對應的命令 ```bash! autocmd BufWinLeave *.* mkview autocmd BufWinEnter *.* silent loadview ``` ## clang-format(外掛) 先下載clang-format ```bash! sudo apt-get install clang-format ``` 建立相關風格(chromium)的設定檔到家目錄($HOME)底下 ```bash! clang-format -style=chromium -dump-config >$HOME/.clang-format ``` 加入clang-format的vim外掛:<span style="color: red;">**rhysd/vim-clang-format**</span> 使用<span style="color:red">:PlugInstall</span>安裝 ## taglist(外掛) 功能:顯示檔案中的macro,global variable,函式等資訊 安裝: 1. 下載plugin:<span style="background:lightgrey">https://sourceforge.net/projects/vim-taglist/files/vim-taglist/</span> 2. 在$HOME/.vim/plugin/variable.vim底下加入變數參數 ```bash! let Tlist_Show_One_File=1 let Tlist_Exit_OnlyWindow=1 set ut=100 ``` 3. 將解壓縮後的plugin底下的taglist.vim和doc底下的taglist.txt丟到$HOME/.vim/doc 4. 在$HOME/.vim/plugin/mapping.vim底下加入快捷鍵<F8> ```bash! nmap <F8> :TlistToggle<CR><CR> ``` ## nerdtree(外掛) 功能:顯示以根目錄開始的檔案結構,且可由選取資訊中的檔案list直接跳到選取的檔案。 安裝步驟: 1. 從[Nerdtree網址](https://www.vim.org/scripts/script.php?script_id=1658)下載最新套件。 2. 將下載的檔案解壓縮後,將plugin/NERD_tree.vim複製到\$HOME/.vim/plugin/,doc/NERD_tree.txt複製到\$HOME/.vim/doc 剩下的資料夾autoload, lib, nerdtree_plugin, \全部複製到~/.vim底下。 3. 在$HOME/.vim/plugin/variable.vim底下加入變數參數 ```bash! let NERDTreeWinPos=1 ``` 4. 在$HOME/.vim/plugin/mapping.vim底下加入快捷鍵<F8> ```bash! nmap <F8> :SrcExplToggle<CR> ``` ## SrcExpl(Source Explorer)(外掛) 功能:SrcExpl可以將當下function的定義顯示出來,或是將當下的變數宣告處顯示出來。 安裝步驟: 1. 下載plugin的檔案: <span style="background:lightgrey">git clone https://github.com/wesleyche/SrcExpl</span> 2. 將plugin/srcexpl.vim複製到\$HOME/.vim/plugin/,doc/srcexpl.txt複製到$HOME/.vim/doc。 3. 在$HOME/.vim/plugin/variable.vim底下加入變數參數 ```bash! let Tlist_Show_One_File=1 let Tlist_Exit_OnlyWindow=1 set ut=100 ``` 4. 在$HOME/.vim/plugin/mapping.vim底下加入快捷鍵<F8> ```bash nmap <F10> :SrcExplToggle<CR> ``` ## trinity(外掛) 功能:來整合taglist, nerdtree, srcexpl,使可以一鍵開啟三個plgin。 安裝步驟: 1. 下載plugin的檔案: <span style="background:lightgrey">git clone https://github.com/wesleyche/Trinity</span> 2. 將plugin/trinity.vim複製到$HOME/.vim/plugin/ 3. 在$HOME/.vim/plugin/mapping.vim底下加入快捷鍵<F7> ```bash nmap <F10> :SrcExplToggle<CR> ``` ## YouCompleteMe(外掛) 需要安裝的工具和套件: ```bash= sudo apt-get install python3-dev sudo apt-get install cmake ``` 此為自動補全的外掛,首先使用vim --version確認版本是否在**7.4**以上以及是否出現<span style="font-weight:bold">+python</span>或是<span style="font-weight:bold">+python3</span>,若無出現則需要安裝python或是python3。 在\$HOME/.vimrc加入 <span style="background:gray">Plugin 'Valloric/YouCompleteMe'</span> 最後使用<span style="background:gray">:PlugInstall</span>完成安裝。 編譯安裝YouCompleteMe: ```bash= python3 ~/.vim/bundle/YouCompleteMe/install.py //安裝c-family相關的套件 python3 ~/.vim/bundle/YouCompleteMe/install.py --clangd-completer //安裝全部套件 python3 ~/.vim/bundle/YouCompleteMe/install.py --all #安裝的相依套件需裝好,否則失敗後會造成後續其他語言的設定無法安裝 //相關設定可以參考[https://github.com/ycm-core/YouCompleteMe ``` 設定相關參數,在$HOME/.vim/plugin/variable.vim加入以下內容: ```bash " YouCompleteMe let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py' let g:ycm_confirm_extra_conf=0 let g:ycm_python_binary_path='/usr/bin/python3' ``` 一定要確定**syntax on**這個設定有啟動之後YouCompleteMe這個套件才會正常運作 ## 括號自動成對(外掛)auto-pairs 功能:在輸入小、中、大括號時會自動補齊另一半 在$HOME/.vimrc加入 ```bash Plugin 'jiangmiao/auto-pairs' ``` 使用<span style="color:red">:PlugInstall</span>安裝,之後便可使用。 ## 設定C語言開發環境 ctag: 安裝ctag ```bash sudo apt-get install ctags or sudo apt-get install exuberant-ctags ``` 讀取目錄底下所有相關的檔案(.c .h)並產生ctags ```bash! ctags -R [-f filename 預設是tags]. ``` 在$HOME/.vim/plugin/variable.vim底下加入變數參數 ```bash! set tags=./tags ``` 使用以下指令在變數或函式的定義處和呼叫處相互跳躍 ```bash! "跳至該函式或變數定義處 Ctrl + ] "跳回使用此函式或變數的呼叫處 Ctrl + t ``` cscope: 安裝cscope ```bash sudo apt-get install cscope ``` 在目錄底下讀取相關檔案(.c .h),並產生相關檔案(cscope.out cscope.in.out cscope.po.out) ```bash! cscope -Rbqk ``` 在$HOME/.vim/plugin/variable.vim底下加入變數參數 ```bash! set cscopetag set csto=0 if filereadable("cscope.out") cs add cscope.out elseif $CSCOPE_DB != "" cs add $CSCOPE_DB endif set cscopeverbose ``` 設定快捷鍵 ```bash! nmap zs :cs find s <C-R>=expand("<cword>")<CR><CR> nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR> nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR> nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR> nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR> nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR> nmap zi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR> nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR> ``` 快捷鍵設定內容參數 <span style="background:yellow"> :cs find s {name} : 找出C語言name的符號 :cs find g {name} : 找出name定義的地方 :cs find c {name} : 找出使用name的地方 :cs find t {name} : 找出name的字串 :cs find e {name} : 相當於egrep功能,但速度更佳 :cs find f {name} : 尋找檔案 :cs find i {name} : 尋找include此檔案的檔案 :cs find d {name} : 尋找name裡面使用到的函式 </span> ## 設定相關的對映按鍵 建立~/.vim/plugin/mapping.vim,內容為 <span style="background:gray">map <A-f> :ClangFormat <enter> </span>。 ```bash! nmap <F7> :TrinityToggleAll nmap <F8> :TlistToggle<CR><CR> nmap <F9> :NERDTreeFind<CR><CR> nmap <enter> o map <up> <nop> map <down> <nop> map <left> <nop> map <right> <nop> noremap <c-d> :q<cr> ``` [將vim打造成source insight](https://ivan7645.github.io/2016/07/12/vim_to_si/) ###### tags: `vim`