Vim 增加編寫程式的便利

每次進行一個新的程式時都需要進行下列動作

ctags -R
cscope -Rbqk

cscope 參數說明

  • R : 將目錄及子目錄底下的所有文件都建立索引
  • b : 僅建立關聯數據庫,不導入使用者介面
  • q : 建立cscope.in.out和cscope.po.out,可增快搜尋速度
  • k : 不搜尋預設會include進來的函式(/usr/include)
"跳至該函式或變數定義
Ctrl + ]
"跳回使用此函式或變數處
Ctrl + t

安裝需要的套件

sudo apt-get install vim vim-youcompleteme elpa-xcscope exuberant-ctags

安裝 plugin 

ctags

" 在~/.vimrc裡配置相關設定,其他配置選項請參考官網說明

" nmap : 將F8設為開啟taglist的快捷鍵
set tags=./tags,./TAGS,tags;~,TAGS;~

cscope

" 在~/.vimrc裡配置相關設定,其他配置選項請參考官網說明

set cscopetag
set csto=0

if filereadable("cscope.out")
   cs add cscope.out   
elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
endif
set cscopeverbose

" :cs find s {name} : 找出C語言name的符號
nmap zs :cs find s <C-R>=expand("<cword>")<CR><CR>
" :cs find g {name} : 找出name定義的地方
nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR>
" :cs find c {name} : 找出使用name的地方
nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR>
" :cs find t {name} : 找出name的字串
nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR>
" :cs find e {name} : 相當於egrep功能,但速度更佳
nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR>
" :cs find f {name} : 尋找檔案
nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR>
" :cs find i {name} : 尋找include此檔案的檔案
nmap zi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
" :cs find d {name} : 尋找name裡面使用到的函式
nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR>

Vundle

參考資料

https://ubunlog.com/zh-TW/vundle-administra-complementos-vim/

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

.vimrc 內容中加上下列內容

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'


" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

使用vim 進入程式後,輸入下列指令,進行安裝

vim +PluginInstall

Tabnine – AI Code Completion

目前安裝失敗,請參考 https://github.com/tabnine/YouCompleteMe#linux-64-bit

cd ~/.vim/bundle
git clone https://github.com/zxqfl/tabnine-vim

增加.vimrc的內容

Plugin 'zxqfl/tabnine-vim'

使用vim 進入程式後,輸入下列指令,進行安裝


:PluginInstall

taglist

taglist可在切出一區塊,顯示此檔案裡的macro,global variable,函式等資訊,且會隨著瀏覽到哪個地方便以不同顏色標示,下載plugin file,下載地址 http://sourceforge.net/projects/vim-taglist/files/vim-taglist

  • plugin/taglist.vim複製到~/.vim/plugin/
  • doc/taglist.txt複製到~/.vim/doc
" 在~/.vimrc裡配置相關設定,其他配置選項請參考官網說明

" nmap : 將F8設為開啟taglist的快捷鍵
nmap <F8> :TlistToggle<CR><CR>

" Tlist_Show_One_File : 只顯示當下瀏覽檔案的func,不顯示之前瀏覽的檔案
let Tlist_Show_One_File=1

" Tlist_Exit_OnlyWindow : 如果taglist區塊是最後一個,則退出vim
let Tlist_Exit_OnlyWindow=1

" ut=100 : taglist會標示目前在操作哪個function or variable,但更新速度很慢,這裡把更新速度設成100ms
set ut=100

nerdtree

NERD tree可切出一區塊,顯示根目錄開始的檔案結構,且可由list直接跳到選取的檔案。下載plugin file,下載地址 http://www.vim.org/scripts/script.php?script_id=1658

  • plugin/NERD_tree.vim複製到~/.vim/plugin/
  • doc/NERD_tree.txt複製到~/.vim/doc
  • 資料夾autoload, lib, nerdtree_plugin, syntax全部複製到~/.vim底下
" 在~/.vimrc裡配置相關設定,其他配置選項請參考官網說明

" nmap : 將F9設為開啟nerdtree的快捷鍵
nmap <F9> :NERDTreeFind<CR><CR>

" NERDTreeWinPos : 將nerdtree區塊放在右邊
let NERDTreeWinPos=1

SrcExpl(Source Explorer)

SrcExpl可以將當下function的定義顯示出來,或是將當下的變數宣告處顯示出來

// 使用git 或其他方式下載
git clone https://github.com/wesleyche/SrcExpl
  • plugin/srcexpl.vim複製到~/.vim/plugin/
  • doc/srcexpl.txt複製到~/.vim/doc
" 在~/.vimrc裡配置相關設定,其他配置選項請參考官網說明
" nmap : 將F10設為開啟srcexpl的快捷鍵
nmap <F10> :SrcExplToggle<CR>
" 若有安裝taglist or nerdtree則需輸入
let g:SrcExpl_pluginList = [
    \ "__Tag_List__",
    \ "_NERD_tree_"
    \ ]

trinity

trinity用來整合taglist, nerdtree, srcexpl,使可以一鍵開啟三個plgin的功能

// 使用git 或其他方式下載
git clone https://github.com/wesleyche/Trinity
  • 將plugin/trinity.vim複製到~/.vim/plugin/
" 在~/.vimrc裡配置相關設定
" nmap : 將F7設為一次打開taglist, nerdtree, srcexpl的快捷鍵
nmap <F7> :TrinityToggleAll

總結,增加下列內容到~/.vimrc

" 修正 NERDTreeDirArrows 錯誤問題
let g:NERDTreeDirArrows=0 

set tags=./tags,./TAGS,tags;~,TAGS;~

set cscopetag
set csto=0

if filereadable("cscope.out")
   cs add cscope.out   
elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
endif
set cscopeverbose

" :cs find s {name} : 找出C語言name的符號
nmap zs :cs find s <C-R>=expand("<cword>")<CR><CR>
" :cs find g {name} : 找出name定義的地方
nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR>
" :cs find c {name} : 找出使用name的地方
nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR>
" :cs find t {name} : 找出name的字串
nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR>
" :cs find e {name} : 相當於egrep功能,但速度更佳
nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR>
" :cs find f {name} : 尋找檔案
nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR>
" :cs find i {name} : 尋找include此檔案的檔案
nmap zi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
" :cs find d {name} : 尋找name裡面使用到的函式
nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR>

" nmap : 將F8設為開啟taglist的快捷鍵
nmap <F8> :TlistToggle<CR><CR>

" Tlist_Show_One_File : 只顯示當下瀏覽檔案的func,不顯示之前瀏覽的檔案
let Tlist_Show_One_File=1

" Tlist_Exit_OnlyWindow : 如果taglist區塊是最後一個,則退出vim
let Tlist_Exit_OnlyWindow=1

" ut=100 : taglist會標示目前在操作哪個function or variable,但更新速度很慢,這裡把更新速度設成100ms
set ut=100

" nmap : 將F9設為開啟nerdtree的快捷鍵
nmap <F9> :NERDTreeFind<CR><CR>

" NERDTreeWinPos : 將nerdtree區塊放在右邊
let NERDTreeWinPos=1

" nmap : 將F10設為開啟srcexpl的快捷鍵
nmap <F10> :SrcExplToggle<CR>
" 若有安裝taglist or nerdtree則需輸入
let g:SrcExpl_pluginList = [
    \ "__Tag_List__",
    \ "_NERD_tree_"
    \ ]

" nmap : 將F7設為一次打開taglist, nerdtree, srcexpl的快捷鍵
nmap <F7> :TrinityToggleAll

set number
set mouse=nv
set nocompatible
set nopaste
set pastetoggle=<f11>

"tab 縮排變成2個空白
:set tabstop=2
:set shiftwidth=2
:set expandtab

syn on

參考

https://ivan7645.github.io/2016/07/12/vim_to_si/