首頁 > 軟體

基於Ubuntu 16.04 設定 Vim IDE

2020-06-16 16:57:57

1.序言        

本文是本人在Ubuntu 16.04上Vim IDE搭建過程的總結。在Windows環境開發的小夥伴也許都知道sourceinsight這款編輯器,為了提高Linux下開發效率,本人搭建了類sourceinsight的IDE開發環境(勉強稱作VIM IDE吧:))。

廢話不多,先上圖展示本人的VIM IDE。 一個視窗同時顯示了4個子終端視窗(當然也可以有更多),每一個子終端視窗類似sourceinsight又切分成了3欄(左下Taglist欄-顯示函數,宏等標籤;左上filelist欄-顯式程式碼檔案目錄檔案結構; 右側欄-程式碼編輯區)。 如果我們看著視窗字太小,沒關係, 當編輯程式時候你可以選擇4個子終端視窗中要編輯的那個終端單獨放大全螢幕顯示(terminator安裝後Ctrl+shit+X),編輯完成之後仍然可以切換回來(terminator安裝後Ctrl+shift+Z)。

2. 設定過程

    如果要完成以上顯示效果,我們需要安裝一些必要的工具和外掛,下面將逐步展開。

2.1 Terminator 安裝

      工具Terminator可以在一個視窗中顯示多個終端視窗,並且可以按照使用者的要求對視窗進行任意分割。

      Ubuntu環境安裝terminator工具命令為:sudo apt-get install terminator

      Terminator常用快捷按鍵有

    1)Ctrl+Shift+E垂直分割視窗

      2)Ctrl+Shift+O 水平分割視窗

      3)Ctrl+Tab  在分割的視窗間切換

      4)Ctrl+Shift+C/V 複製/貼上

      5)Ctrl+Shift+X 放大視窗到全螢幕

      6)Ctrl+Shift+Z 從放大視窗回到多視窗

      注意:如果您對視窗風格不滿意,可以右鍵->preferences進行必要設定和修改。比如修改快捷按鍵等等。

2.2 vim 編輯器安裝

      Ubuntu環境安裝terminator工具命令為: sudo apt-get install vim vim-scripts vim-doc

     其中vim-scripts是vim的一些基本外掛 ,vim安裝之後緊接著應該個性化設定。Vim本身的系統組態檔夾是在/usr/share/vim/和/etc/vim/兩個資料夾下。但是通常我們不會去改變這兩個資料夾下的組態檔,而是在使用者資料夾/home/user下建立自己的組態檔.vimrc然後對其設定,這裡不再解釋,後面我將附上我的vim設定。

 

2.3 vim外掛安裝

    vim常用外掛有:1)vim-addons 2)ctags 3)cscope 4)winmanager 5)minibufexplorer 6)omnicppcomplete 7)AutoComplPop  8)echofunc 9)taglist

2.3.1 vim-addons

  通過vim-addons,我們可以管理vim外掛。可通過命令sudo apt-get install vim-addon-manager手動安裝。

  檢視外掛狀態 命令: 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       
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                  installed    removed       
xmledit                    removed      removed

    安裝某個外掛X命令(前提是在目錄/home/user/.vim/下建立好了plugin和doc兩個資料夾):vim-addons install X

2.3.2 ctags

    ctags用來建立原始碼樹的標籤索引(標籤就是一個識別符號被定義的地方,如函數定義),在程式設計時能迅速定位函數、變數、宏定義等位置去檢視原形。
    1)ctags安裝命令

          sudo apt-get install ctags

    2)ctags設定

        要正確使用ctags功能,我們也需要設定vimrc檔案,我將在附件列舉。

    3)建立專案project的原始碼索引命令(通常在專案project資料夾根目錄執行)

          ctags -R *

          執行以上命令之後,你會發現多了一個tags檔案,這個就是ctags索引檔案。

    4)ctags常用方法

          Ctrl+]  跳到當前游標下單詞的標籤
          Ctrl+O  返回上一個標籤
          Ctrl+T  返回上一個標籤
        Ctrl+W + ]  新視窗顯示當前游標下單詞的標籤,游標跳到標籤處

2.3.3 cscope

      cscope是類似於ctags一樣的工具,但??比ctags更強大。

     1)cscope安裝命令

          sudo apt-get install cscope

    2)cscope設定

        要正確使用cscope功能,我們也需要設定vimrc檔案,我將在附件列舉。

    3)建立專案project的原始碼cscope資料庫命令(通常在專案project資料夾根目錄執行)

          cscope -Rbq cscope.out

          執行以上命令之後,你會發現多了3個檔案,cscope.in.out,cscope.po.out, cscope.out, 它們就是cscope索引資料庫。

        注意:以上命令收集的是project目錄下cscope預設型別檔案索引,有時候我們自定義檔案不能索引,所以我們可以用如下命令替換以上命令:

        find  .  -name "*.h" -o -name "*.cpp" -o -name "*.c" > cscope.file

        cscope -bqk -i cscope.file

    4)新增cscope庫到vim

        對於簡單1個檔案,用vim開啟某個原始碼檔案,末行模式下,輸入“:cs add cscope.out"。對於整個project工程檔案,我們需要借助vim自己讀取組態檔。

        我是在使用者目錄~/.vim/plugin/下放置了cscope_map.vim組態檔,後面會附上。

    5)cscope常用方法

      Ctrl- s 查詢所有當前游標所在符號出現過位置。
      Ctrl- c 查詢所有呼叫當前游標所在函數的函數。

      當然還有其它按鍵,請閱讀組態檔或者在vim命令列執行:help cscope

    注意:通過快捷鍵查詢某個符號後,會立即跳轉到第一個找到的該符號出現的位置。如果你對這次預設跳轉的位置不滿意,在Vim命令列下執行cw命令,就能在編輯區下面quickfix視窗看到所有查詢結果的列表,點選相應列表項就能跳轉到相應位置。

2.3.4  winmanager

    WinManager用於管理檔案瀏覽器和緩衝區(buffer)。

    安裝命令為:vim-addons install winmanager

    設定見附錄vimrc,進入vim之後開啟或者關閉命令是輸入:WMToggle

2.3.5 minibufexplorer

    安裝命令為:vim-addons install minibufexplorer

    設定見附錄vimrc

2.3.6 omnicppcomplete(自動補齊)

  安裝命令為:vim-addons install omnicppcomplete

  組態檔見附錄vimrc

2.3.7 AutoComplPop(普通變數和函數自動彈出補齊)

      下載連結:http://www.vim.org/scripts/script.php?script_id=1879 

        安裝方法:

               先解壓:unzip vim-autocomplpop.zip,將解壓後的檔案拷貝到~/.vim/ 下的相應目錄裡:

                     autoload/*    ->    ~/.vim/autoload/

                     doc/*    ->    ~/.vim/doc/

                     plugin/*    ->    ~/.vim/plugin/

      幫助使用:

              重新開啟vim即可使用。新增help檔案:helptags ~/.vim/doc/即可(開啟幫助檔案:help autocomplpop)

2.3.8 echofunc(函數原型提示)

    echofunc下載地址:http://www.vim.org/scripts/script.php?script_id=1735
    下載完成後,把echofunc.vim檔案放到 ~/.vim/plugin資料夾中
    當在vim插入(insert)模式下緊接著函數名後輸入一個"("的時候, 這個函數的宣告就會自動顯示在資訊提示欄。

    注意:這個外掛需要tags檔案的支援, 並且在建立tags檔案的時候要加選項"--fields=+lS"。  ctags -R --field=+IS

2.3.9 taglist

      taglist用於列出了當前檔案中的所有標籤(宏, 全域性變數, 函數名等)。
    安裝Taglist命令: vim-addons install taglist

      設定見附錄vimrc,進入vim之後關閉或者開啟taglist方法是末行命令模式輸入:Taglist。

2.3.10 quickfix

      在程式的開發過程中,很重要的一個迴圈是:編輯-編譯-編輯,vim中的quickfix功能就是為了提高這一回圈的效率。

    推薦使用Makefile的方式進行專案的編譯、管理,可以實現專案的自動化管理、有利於提高效率。通過make命令完成程式的編譯工作後,會得到編譯結果,一般會有一些編譯錯誤,quickfix功能使我們可以直接跳到檔案中的錯誤位置,直接進行修改,並通過使用quickfix的命令完成錯誤列表的跳轉。

    如果僅有1個檔案也懶得寫Makefile檔案,可以採用如下2條命令處理:

    :set makeprg=gcc test.c -o test
    :make
    變數的值為字串,當在其中有空格時需要用進行跳脫,同樣如果想輸入也要進行跳脫。
    注意:在vim執行時通過命令列設定的變數值均是臨時的,即當退出vim環境時,該變數值會恢復為組態檔中的值或者預設值。
   
   
    常用的quickfix命令為:
    :cc     顯示詳細錯誤資訊
    :cp     跳到上一個錯誤
    :cn     跳到下一個錯誤
    :cl      列出所有錯誤
    :cw     如果有錯誤列表,則開啟quickfix視窗,沒有則什麼也不錯

3. 附錄

3.1 vimrc設定

    下面是我的vimrc設定,這個檔案放在目錄/home/user/下。

runtime! 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  

" 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       
set autowrite       
set autoindent       
"set smartindent     
set tabstop=4       
set softtabstop=4  
set shiftwidth=4  
set cindent     
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s     
"set backspace=2   
set showmatch     
set linebreak     
set whichwrap=b,s,<,>,[,]
"set hidden " Hide buffers when they are abandoned
set mouse=a           
set number           
"set previewwindow   
set history=50     


set laststatus=2
set ruler           

set showcmd   
set showmode

"--find setting--
set incsearch       
set hlsearch   


"--ctags setting--
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+=~/test/tags


"-- omnicppcomplete setting --
imap <F3> <C-X><C-O>
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


"-- Taglist setting --
let Tlist_Ctags_Cmd='ctags'
let Tlist_Use_Right_Window=1
let Tlist_Show_One_File=0
let Tlist_File_Fold_Auto_Close=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Process_File_Always=1
let Tlist_Inc_Winwidth=0



"-- WinManager setting --
let g:winManagerWindowLayout='FileExplorer|TagList'
"let g:persistentBehaviour=0
nmap wm :WMToggle<cr>


" -- MiniBufferExplorer --
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1   


"--fold setting--
set foldmethod=syntax
set foldlevel=100
set foldcolumn=5


"-- QuickFix setting --
map <F6> :make clean<CR><CR><CR>
map <F7> :make<CR><CR><CR> :copen<CR><CR>
map <F8> :cp<CR>
map <F9> :cn<CR>
imap <F6> <ESC>:make clean<CR><CR><CR>
imap <F7> <ESC>:make<CR><CR><CR> :copen<CR><CR>
imap <F8> <ESC>:cp<CR>
imap <F9> <ESC>:cn<CR>


3.2 cscope組態檔

  cscope組態檔位於目錄(/home/user/.vim/plugin)名叫cscope_map.vim,內容為

 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6:    Stick this file in your ~/.vim/plugin directory (or in a
"              'plugin' directory in some other directory that is in your
"              'runtimepath'.
"
" -- vim 5:    Stick this file somewhere and 'source cscope.vim' it from
"              your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys).  If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell      jduell@alumni.princeton.edu    2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled.  If it wasn't, time to recompile vim...
if has("cscope")

   set csprg=/usr/bin/cscope "指定用來執行cscope的命令


    """"""""""""" Standard cscope/vim boilerplate

    " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
    set cscopetag
   set csto=0  "設定cstag命令查詢次序:0先找cscope資料庫再找標籤檔案;1先找標籤檔案再找cscope資料庫"
   set cst                            "同時搜尋cscope資料庫和標簽檔案
   set cscopequickfix=s-,c-,d-,i-,t-,e-    " 使用QuickFix視窗來顯示cscope查詢結果
   
   set nocsverb

    " add any cscope database in current directory
    if filereadable("cscope.out")
        cs add cscope.out  
    " else add the database pointed to by environment variable
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif

    " show msg when any other cscope db added
   set csverb
  " set cscopeverbose  


    """"""""""""" My cscope/vim key mappings
    "
    " The following maps all invoke one of the following cscope search types:
    "
    "  's'  symbol: find all references to the token under cursor
    "  'g'  global: find global definition(s) of the token under cursor
    "  'c'  calls:  find all calls to the function name under cursor
    "  't'  text:  find all instances of the text under cursor
    "  'e'  egrep:  egrep search for the word under cursor
    "  'f'  file:  open the filename under cursor
    "  'i'  includes: find files that include the filename under cursor
    "  'd'  called: find functions that function under cursor calls
    "
    " Below are three sets of the maps: one set that just jumps to your
    " search result, one that splits the existing vim window horizontally and
    " diplays your search result in the new window, and one that does the same
    " thing, but does a vertical split instead (vim 6 only).
    "
    " I've used CTRL- and CTRL-@ as the starting keys for these maps, as it's
    " unlikely that you need their default mappings (CTRL-'s default use is
    " as part of CTRL- CTRL-N typemap, which basically just does the same
    " thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
    " If you don't like using 'CTRL-@' or CTRL-, , you can change some or all
    " of these maps to use other keys.  One likely candidate is 'CTRL-_'
    " (which also maps to CTRL-/, which is easier to type).  By default it is
    " used to switch between Hebrew and English keyboard mode.
    "
    " All of the maps involving the <cfile> macro use '^<cfile>$': this is so
    " that searches over '#include <time.h>" return only references to
    " 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
    " files that contain 'time.h' as part of their name).


    " To do the first type of search, hit 'CTRL-', followed by one of the
    " cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope
    " search will be displayed in the current window.  You can use CTRL-T to
    " go back to where you were before the search.  
    "

    nmap <C->s :cs find s <C-R>=expand("<cword>")<CR><CR>   
    nmap <C->g :cs find g <C-R>=expand("<cword>")<CR><CR>   
    nmap <C->c :cs find c <C-R>=expand("<cword>")<CR><CR>   
    nmap <C->t :cs find t <C-R>=expand("<cword>")<CR><CR>   
    nmap <C->e :cs find e <C-R>=expand("<cword>")<CR><CR>   
    nmap <C->f :cs find f <C-R>=expand("<cfile>")<CR><CR>   
    nmap <C->i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
    nmap <C->d :cs find d <C-R>=expand("<cword>")<CR><CR>   


    " Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
    " makes the vim window split horizontally, with search result displayed in
    " the new window.
    "
    " (Note: earlier versions of vim may not have the :scs command, but it
    " can be simulated roughly via:
    "    nmap <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>   

    nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>   
    nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>   
    nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>   
    nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>   
    nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>   
    nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>   
    nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>   
    nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>   


    " Hitting CTRL-space *twice* before the search type does a vertical
    " split instead of a horizontal one (vim 6 and up only)
    "
    " (Note: you may wish to put a 'set splitright' in your .vimrc
    " if you prefer the new window on the right instead of the left

    nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>   
    nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>   
    nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>


    """"""""""""" key map timeouts
    "
    " By default Vim will only wait 1 second for each keystroke in a mapping.
    " You may find that too short with the above typemaps.  If so, you should
    " either turn off mapping timeouts via 'notimeout'.
    "
    "set notimeout
    "
    " Or, you can keep timeouts, by uncommenting the timeoutlen line below,
    " with your own personal favorite value (in milliseconds):
    "
    "set timeoutlen=4000
    "
    " Either way, since mapping timeout settings by default also set the
    " timeouts for multicharacter 'keys codes' (like <F1>), you should also
    " set ttimeout and ttimeoutlen: otherwise, you will experience strange
    " delays as vim waits for a keystroke after you hit ESC (it will be
    " waiting to see if the ESC is actually part of a key code like <F1>).
    "
    "set ttimeout
    "
    " personally, I find a tenth of a second to work well for key code
    " timeouts. If you experience problems and have a slow terminal or network
    " connection, set it higher.  If you don't set ttimeoutlen, the value for
    " timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
    "
    "set ttimeoutlen=100

endif

3.3 ctags和cscope指令碼

    為方便使用,如下寫到一個指令碼中,在建立工程project之後呼叫一下即可建立tags索引和cscope資料庫。

#!/bin/sh

ctags -R --fields=+lS

find . -name "*.h" -o -name "*.c" -o -name "*.cpp" > cscope.files
cscope -bkq -i cscope.files

Vim入門基礎知識集錦  http://www.linuxidc.com/Linux/2017-02/140903.htm

Vim入門基礎教學 http://www.linuxidc.com/Linux/2017-02/140279.htm

把Vim打造成優秀的C++ IDE  http://www.linuxidc.com/Linux/2016-06/132262.htm

Ubuntu 14.04升級Vim7.4到8.0  http://www.linuxidc.com/Linux/2016-11/136816.htm

Vim安裝youcompleteme自動補全外掛  http://www.linuxidc.com/Linux/2016-11/137665.htm

Linux Vim編輯器使用簡單講解  http://www.linuxidc.com/Linux/2016-12/138930.htm

Vim文字編輯器  http://www.linuxidc.com/Linux/2017-03/142275.htm

Vim安裝與設定進階版 http://www.linuxidc.com/Linux/2017-03/141724.htm

Vim編輯器使用教學  http://www.linuxidc.com/Linux/2017-07/145885.htm

Ubuntu 16.04 Vim YouCompleteMe自動補全的安裝設定與使用  http://www.linuxidc.com/Linux/2017-02/141088.htm

Linux文字編輯器Vim基礎教學  http://www.linuxidc.com/Linux/2017-09/146930.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-12/149870.htm


IT145.com E-mail:sddin#qq.com