歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 將Vim改造為強大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete

將Vim改造為強大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete

日期:2017/2/28 13:43:51   编辑:Linux教程

1、安裝Vim和Vim基本插件
首先安裝好Vim和Vim的基本插件。這些使用apt-get安裝即可:
lingd@Ubuntu:~/arm$sudo apt-get install vim vim-scripts vim-doc
其中vim-scripts是vim的一些基本插件,包括語法高亮的支持、縮進等等。
vim中文幫助文檔tar包下載地址:
http://sourceforge.net/projects/vimcdoc/files/vimcdoc/
解壓後其中有個doc文件夾, 將其中的內容全部復制到~/.vim/doc, 或者vim安裝目錄下的doc目錄中, 此時vim中的help信息已經是中文的了.
網頁版中文幫助文檔網址http://vimcdoc.sourceforge.net/doc/help.html
首頁就時vim幫助文檔的目錄,閱讀起來更方便有效、更有針對性!

2、Vim配置文件
Vim強大的功能,其來源基本上就兩個地方:Vim插件以及Vim配置文件。
Vim本身的系統配置文件夾是在/usr/share/vim/和/etc/vim/兩個文件夾下。一般情況下,我們不會去改變這兩個文件夾下的配置文件,而是在用戶文件夾/home/user(其中,user為用戶名,我的用戶名是lingd)下建立自己的配置文件。進入用戶文件夾(/home/user/)之後,用gedit新建一個名叫.vimrc的文件:
lingd@ubuntu:~/arm$ cd ~
lingd@ubuntu:~$ gedit .vimrc
注:使用gedit主要是為了方便大段大段的文字粘貼!
然後把下面的文字拷貝進這個文件之後保存:

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set nocompatible

" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on " 語法高亮
endif
colorscheme ron " elflord ron peachpuff default 設置配色方案,vim自帶的配色方案保存在/usr/share/vim/vim72/colors目錄下

" detect file type
filetype on
filetype plugin on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected filetype
filetype plugin indent on
endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.

"set ignorecase " 搜索模式裡忽略大小寫
"set smartcase " 如果搜索模式包含大寫字符,不使用 'ignorecase' 選項。只有在輸入搜索模式並且打開 'ignorecase' 選項時才會使用。
set autowrite " 自動把內容寫回文件: 如果文件被修改過,在每個 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令時進行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令轉到別的文件時亦然。
set autoindent " 設置自動對齊(縮進):即每行的縮進值與上一行相等;使用 noautoindent 取消設置
"set smartindent " 智能對齊方式
set tabstop=4 " 設置制表符(tab鍵)的寬度
set softtabstop=4 " 設置軟制表符的寬度
set shiftwidth=4 " (自動) 縮進使用的4個空格
set cindent " 使用 C/C++ 語言的自動縮進方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "設置C/C++語言的具體縮進方式
"set backspace=2 " 設置退格鍵可用
set showmatch " 設置匹配模式,顯示匹配的括號
set linebreak " 整詞換行
set whichwrap=b,s,<,>,[,] " 光標從行首和行末時可以跳到另一行去
"set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes) "使用鼠標
set number " Enable line number "顯示行號
"set previewwindow " 標識預覽窗口
set history=50 " set command history to 50 "歷史記錄50條


"--狀態行設置--
set laststatus=2 " 總顯示最後一個窗口的狀態行;設為1則窗口數多於一個的時候顯示最後一個窗口的狀態行;0不顯示最後一個窗口的狀態行
set ruler " 標尺,用於顯示光標位置的行號和列號,逗號分隔。每個窗口都有自己的標尺。如果窗口有狀態行,標尺在那裡顯示。否則,它顯示在屏幕的最後一行上。

"--命令行設置--
set showcmd " 命令行顯示輸入的命令
set showmode " 命令行顯示vim當前模式

"--find setting--
set incsearch " 輸入字符串就顯示匹配點
set hlsearch

注:配置文件中,以單個雙引號開頭的文字為注釋。
保存文件之後,啟動Vim。此時,Vim已經是這種效果了(語法高亮挺漂亮的–這個是由vim-scripts中的插件支持的):

3、ctags安裝與配置
ctags可以建立源碼樹的標簽索引(標簽就是一個標識符被定義的地方,如函數定義),使程序員在編程時能迅速定位函數、變量、宏定義等位置去查看原形
以下是在ubuntu下ctags的下載安裝和配置過程:
下載並安裝ctags,終端輸入命令
lingd@ubuntu:~/arm$ sudo apt-get install ctags
建立源碼索引,比如我經常需要查閱Linux的內核代碼,而這些代碼放在/home/lingd/arm/linux-2.6.24.7目錄下
那麼在終端進入到該目錄後,輸入命令ctags -R *,你會發現多了一個tags文件,這個就是索引文件
lingd@ubuntu:~/arm$ cd linux-2.6.24.7
lingd@ubuntu:~/arm/linux-2.6.24.7$ ls
arch crypto include kernel mm samples usr
block Documentation init lib net scripts
COPYING drivers ipc MAINTAINERS README security
CREDITS fs Kbuild Makefile REPORTING-BUGS sound
lingd@ubuntu:~/arm/linux-2.6.24.7$ ctags -R *
lingd@ubuntu:~/arm/linux-2.6.24.7$ ls
arch crypto include kernel mm samples tags
block Documentation init lib net scripts usr
COPYING drivers ipc MAINTAINERS README security
CREDITS fs Kbuild Makefile REPORTING-BUGS sound
向vim注冊索引文件tags的路徑,
lingd@ubuntu:~/arm/linux-2.6.24.7$ vi ~/.vimrc
在打開文件的最後添加如下內容(當然,具體路徑根據你自己的情況)

"--ctags setting--
" 按下F5重新生成tag文件,並更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=~/arm/linux-2.6.24.7/tags "add new tags file(剛剛生成tags的路徑,在ctags -R 生成tags文件後,不要將tags移動到別的目錄,否則ctrl+]時,會提示找不到源碼文件)

set tags+=./tags表示在當前工作目錄下搜索tags文件
set tags+=~/arm/linux-2.6.24.7/tags表示在搜尋tags文件的時候,也要搜尋~/arm/linux-2.6.24.7/文件夾下的tags文件。
然後保存並退出vi。這樣,你就可以用vim在任意地方查看有關Linux的函數原形
------------------------------------
tag命令用法:
Ctrl+] 跳到當前光標下單詞的標簽
Ctrl+O 返回上一個標簽
Ctrl+T 返回上一個標簽
:tag TagName 跳到TagName標簽
以上命令是在當前窗口顯示標簽,當前窗口的文件替代為包標簽的文件,當前窗口光標跳到標簽位置。如果不希望在當前窗口顯示標簽,可以使用以下命令:
:stag TagName 新窗口顯示TagName標簽,光標跳到標簽處
Ctrl+W + ] 新窗口顯示當前光標下單詞的標簽,光標跳到標簽處
當一個標簽有多個匹配項時(函數 (或類中的方法) 被多次定義),":tags" 命令會跳轉到第一處。如果在當前文件中存在匹配,那它將會被首先使用。
可以用這些命令在各匹配的標簽間移動:
:tfirst 到第一個匹配
:[count]tprevious 向前 [count] 個匹配
:[count]tnext 向後 [count] 個匹配
:tlast 到最後一個匹配
或者使用以下命令選擇要跳轉到哪一個
:tselect TagName
輸入以上命令後,vim會為你展示一個選擇列表。然後你可以輸入要跳轉到的匹配代號 (在第一列)。其它列的信息可以讓你知道標簽在何處被定義過。
以下命令將在預覽窗口顯示標簽
:ptag TagName 預覽窗口顯示TagName標簽,光標跳到標簽處
Ctrl+W + } 預覽窗口顯示當前光標下單詞的標簽,光標跳到標簽處
:pclose 關閉預覽窗口
:pedit file.h 在預覽窗口中編輯文件file.h(在編輯頭文件時很有用)
:psearch atoi 查找當前文件和任何包含文件中的單詞並在預覽窗口中顯示匹配,在使用沒有標簽文件的庫函數時十分有用。

最簡單的使用方法舉例
用vi在任意目錄寫一個Test.c文件,內容如下:

int main(void)
{
printf("Hello World!\n");
return 0;
}

寫好後末行模式輸入w保存好(不要退出vi),按Esc回到指令模式,把光標停留在printf上
然後按 Ctrl+W + ],vi會自動跳到Linux系統函數printf()處,這時我們能查看printf()的原形
查看完了,按Ctrl+o(回到上一個標簽) 就回到原來的地方

4、管理vim插件——vim-addons
通過vim-addons,我們可以管理vim插件。我們在sudo apt-get install vim vim-scripts vim-doc時,一般會自動安裝上vim-addons。若未安裝可通過sudo apt-get install vim-addon-manager手動安裝。安裝完成後,就可以用vim-addons管理vim插件了。
# 系統中已有的vim-scripts中包含的插件及其狀態:
lingd@ubuntu:~$ vim-addons status
# Name User Status System Status
align removed removed
alternate removed removed
bufexplorer removed removed
calendar removed removed
closetag removed removed
colors sampler pack removed removed
cvsmenu removed removed
debPlugin removed removed
detectindent removed removed
doxygen-toolkit removed removed
editexisting removed removed
enhanced-commentify removed removed
gnupg removed removed
info removed removed
justify removed removed
lbdbq removed removed
markdown-syntax removed removed
matchit removed removed
minibufexplorer installed removed
nerd-commenter removed removed
omnicppcomplete installed removed
po removed removed
project installed removed
python-indent removed removed
secure-modelines removed removed
snippetsEmu removed removed
sokoban removed removed
supertab removed removed
surround removed removed
taglist installed removed
tetris removed removed
utl removed removed
vcscommand removed removed
vimplate removed removed
whatdomain removed removed
winmanager removed removed
xmledit removed removed
上面我們介紹了如何獨立於系統配置文件之外,建立自己的Vim配置文件。當我們自己下載安裝Vim插件的時候,也可以另外建立目錄,放置我們自己的插件。這個目錄一般為/home/user/.vim,另外還需要建立一個插件子目錄,一個插件文檔子目錄,以上的可以進入/home/user目錄下通過下面的命令執行:
lingd@ubuntu:~$ mkdir .vim
lingd@ubuntu:~$ cd .vim
lingd@ubuntu:~/.vim$ mkdir plugin
lingd@ubuntu:~/.vim$ mkdir doc
# vim官方插件的安裝,xxxx是要安裝的插件名,以status中顯示的名稱為准。安裝插件xxxx時使用以下命令(前提是在目錄/home/user/.vim/下建立好了plugin和doc兩個文件夾)
vim-addons install xxxx
關於vim-addons命令的詳細用法,可以通過“man vim-addons”查看其幫助文檔

5、vim自動補全——OmniCppComplete
vim的自動補全功能可通過其插件OmniCppComplete實現。
安裝OmniCppComplete
lingd@ubuntu:~$ vim-addons install omnicppcomplete
配置OmniCppComplete
在vim配置文件/home/user/.vimrc中加入如下的配置:

"-- omnicppcomplete setting --
" 按下F3自動補全代碼,注意該映射語句後不能有其他字符,包括tab;否則按下F3會自動補全一些亂碼
imap <F3> <C-X><C-O>
" 按下F2根據頭文件內關鍵字補全
imap <F2> <C-X><C-I>
set completeopt=menu,menuone " 關掉智能補全時的預覽窗口
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1 " enable the global scope search
let OmniCpp_DisplayMode=1 " Class scope completion mode: always show all members
"let OmniCpp_DefaultNamespaces=["std"]
let OmniCpp_ShowScopeInAbbr=1 " show scope in abbreviation and remove the last column
let OmniCpp_ShowAccess=1

(前幾行就是提供了C++中的./->/::等操作符的提示和自動完成)。
OmniCppComplete是基於ctags數據庫即tags文件實現的(基於ctags生成的索引信息來實現自動補全的),所以在ctags -R生成tags時還需要一些額外的選項,這樣生成的tags文件才能與OmniCppComplete配合運作。使用下列命令生成tags文件,就可以與OmniCppComplete配合運作:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
--c++-kinds=+p : 為C++文件增加函數原型的標簽
--fields=+iaS : 在標簽文件中加入繼承信息(i)、類成員的訪問控制信息(a)、以及函數的指紋(S)
--extra=+q : 為標簽增加類修飾符。注意,如果沒有此選項,將不能對類成員補全
# vim自動補全功能的測試
# 為了測試自動補全功能,我們先下載C++一份C++標准庫的源代碼。
lingd@ubuntu:~$ sudo apt-get install build-essential
# 然後在/usr/include/c++下就可以找到標准庫的頭文件了。
lingd@ubuntu:~$ cd /usr/include/c++
lingd@ubuntu:/usr/include/c++$ ls
4.4 4.4.3
# 在此文件夾下生成能與OmniCppComplete配合運作的tags文件
lingd@ubuntu:/usr/include/c++$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
lingd@ubuntu:/usr/include/c++$ ls
4.4 4.4.3 tags
# 在vim的配置文件中如下內容,然後在編程的時候就可以使用自動補全功能了。
lingd@ubuntu:/usr/include/c++$ vi ~/.vimrc

set tags+=/usr/include/c++/tags

# 用vi打開前面的Test.c文件,在printf("Hello World!\n")下一行中,輸入pri,然後按下Ctrl+X Ctrl+O,此時vi會彈出一個窗口,所有以pri開頭的tag都會出現在這個窗口中,printf就出現在第6行中
lingd@ubuntu:~$ cd ~
lingd@ubuntu:~$ vim Test.c

注意:在自動補全的點,Vim必須知道可能補全的定義。比如說,在namespace std命名空間下的變量和函數,必須要用using namespace std;暴露出來,否則是不能補全的。在.cpp文件中還可以,在.h文件中這樣就不是好的做法了。暫時不知道這個問題是由於我自己配置錯誤還是程序沒有實現。
當自動補全下拉窗口彈出後,一些可用的快捷鍵:
Ctrl+P 向前切換成員
Ctrl+N 向後切換成員
Ctrl+E 表示退出下拉窗口, 並退回到原來錄入的文字
Ctrl+Y 表示退出下拉窗口, 並接受當前選項
其他補全方式:
Ctrl+X Ctrl+L 整行補全
Ctrl+X Ctrl+N 根據當前文件裡關鍵字補全
Ctrl+X Ctrl+K 根據字典補全
Ctrl+X Ctrl+T 根據同義詞字典補全
Ctrl+X Ctrl+I 根據頭文件內關鍵字補全
Ctrl+X Ctrl+] 根據標簽補全
Ctrl+X Ctrl+F 補全文件名
Ctrl+X Ctrl+D 補全宏定義
Ctrl+X Ctrl+V 補全vim命令
Ctrl+X Ctrl+U 用戶自定義補全方式
Ctrl+X Ctrl+S 拼寫建議
幫助文檔
:help omnicppcomplete

6、提示函數原型echofunc
echofunc可以在命令行中提示當前輸入函數的原型。
echofunc下載地址:http://www.vim.org/scripts/script.php?script_id=1735
下載完成後,把echofunc.vim文件放到 ~/.vim/plugin文件夾中
當你在vim插入(insert)模式下緊接著函數名後輸入一個"("的時候, 這個函數的聲明就會自動顯示在命令行中。如果這個函數有多個聲明, 則可以通過按鍵"Alt+-"和"Alt+="向前和向後翻頁, 這個兩個鍵可以通過設置g:EchoFuncKeyNext和g:EchoFuncKeyPrev參數來修改。這個插件需要tags文件的支持, 並且在創建tags文件的時候要加選項"--fields=+lS"(OmniCppComplete創建的tag文件也能用), 整個創建tags文件的命令如下:
$ ctags -R --fields=+lS
其他插件說明詳見echofunc.vim

如果你在編譯vim時加上了"+balloon_eval"特性,那麼當你把鼠標放在函數名上的時候會有一個tip窗口彈出, 該窗口中也會有函數的聲明

Copyright © Linux教程網 All Rights Reserved