將vim打造成php的IDE開發環境


將vim打造成IDE開發環境

本文主要介紹將vim打造成IDE開發環境,如代碼補全,高亮顯示,函數跳轉,函數自動注釋等

首先介紹2款VIM插件管理器:Vbundle,Pathogen

本文中的vim插件,都可以通過這2款插件管理軟件進行安裝。具體使用方法,請參考官方說明
下面主要介紹通過手動安裝的方式,安裝VIM插件。

一.高亮顯示

具體請參考vim配置說明

二.代碼自動補全插件neocomplete

使用該插件,需要vim+lua支持,具體配置方法請參考lua安裝

neocomplete安裝,以下是github地址

https://github.com/Shougo/neocomplete

步驟1:下載插件

git clone https://github.com/Shougo/neocomplete

步驟2:安插neocomplete
將下載的源碼復制到vim目錄

cd neocomplete
cp -r autoload doc plugin /usr/share/vim/vim74/

步驟3:配置vimrc,使其支持自動補全
neocomplete配置,在/etc/vimrc (/usr/share/vim/vimrc)中加入如下配置

"Note: This option must set it in .vimrc(_vimrc).  NOT IN .gvimrc(_gvimrc)!
" Disable AutoComplPop.
let g:acp_enableAtStartup = 0
" Use neocomplete.
let g:neocomplete#enable_at_startup = 1
" Use smartcase.
let g:neocomplete#enable_smart_case = 1
" Set minimum syntax keyword length.
let g:neocomplete#sources#syntax#min_keyword_length = 3
let g:neocomplete#lock_buffer_name_pattern = '\*ku\*'

" Define dictionary.
let g:neocomplete#sources#dictionary#dictionaries = {
    \ 'default' : '',
    \ 'vimshell' : $HOME.'/.vimshell_hist',
    \ 'scheme' : $HOME.'/.gosh_completions'
        \ }

" Define keyword.
if !exists('g:neocomplete#keyword_patterns')
    let g:neocomplete#keyword_patterns = {}
endif
let g:neocomplete#keyword_patterns['default'] = '\h\w*'

" Plugin key-mappings.
inoremap <expr><C-g>     neocomplete#undo_completion()
inoremap <expr><C-l>     neocomplete#complete_common_string()

" Recommended key-mappings.
" <CR>: close popup and save indent.
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return (pumvisible() ? "\<C-y>" : "" ) . "\<CR>"
  " For no inserting <CR> key.
  "return pumvisible() ? "\<C-y>" : "\<CR>"
endfunction
" <TAB>: completion.
inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
" <C-h>, <BS>: close popup and delete backword char.
inoremap <expr><C-h> neocomplete#smart_close_popup()."\<C-h>"
inoremap <expr><BS> neocomplete#smart_close_popup()."\<C-h>"
" Close popup by <Space>.
"inoremap <expr><Space> pumvisible() ? "\<C-y>" : "\<Space>"

" AutoComplPop like behavior.
"let g:neocomplete#enable_auto_select = 1

" Shell like behavior(not recommended).
"set completeopt+=longest
"let g:neocomplete#enable_auto_select = 1
"let g:neocomplete#disable_auto_complete = 1
"inoremap <expr><TAB>  pumvisible() ? "\<Down>" : "\<C-x>\<C-u>"

" Enable omni completion.
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags

" Enable heavy omni completion.
if !exists('g:neocomplete#sources#omni#input_patterns')
  let g:neocomplete#sources#omni#input_patterns = {}
endif
"let g:neocomplete#sources#omni#input_patterns.php = '[^. \t]->\h\w*\|\h\w*::'
"let g:neocomplete#sources#omni#input_patterns.c = '[^.[:digit:] *\t]\%(\.\|->\)'
"let g:neocomplete#sources#omni#input_patterns.cpp = '[^.[:digit:] *\t]\%(\.\|->\)\|\h\w*::'

" For perlomni.vim setting.
" https://github.com/c9s/perlomni.vim
let g:neocomplete#sources#omni#input_patterns.perl = '\h\w*->\h\w*\|\h\w*::'

步驟4:php自動補全配置

經過以上配置,默認情況下已經支持大多數語言的代碼補全
官方已經禁用了php和ruby的代碼補全,因為運行效率太慢
要使用php補全,參考https://github.com/shawncplus/phpcomplete.vim說明


Q: I want to use PHP omni completion.

A:
Note: You should use this omni completion for PHP.
https://github.com/shawncplus/phpcomplete.vim
>
    let g:neocomplete#sources#omni#input_patterns.php =
    \ '\h\w*\|[^. \t]->\%(\h\w*\)\?\|\h\w*::\%(\h\w*\)\?'
<

在這里,直接在vimrc中加入如下配置,就可以php自動補全了

let g:neocomplete#sources#omni#input_patterns.php =
    \ '\h\w*\|[^. \t]->\%(\h\w*\)\?\|\h\w*::\%(\h\w*\)\?'

如果要使用neocomplete推薦的php補全,https://github.com/shawncplus/phpcomplete.vim,請參考如下配置

需要安裝Vbundle,它是一個vim軟件集,且是一個vim插件管理器

https://github.com/VundleVim/Vundle.vim
步驟1
在vim路徑,/usr/share/vim/vim74/ 下建立bundle目錄
 cd ~/.vim/bundle
 git clone git://github.com/shawncplus/phpcomplete.vim.git
 
 步驟2:
git clone https://github.com/VundleVim/Vundle.vim.git

在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'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
Plugin 'ascenator/L9', {'name': 'newL9'}

" 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

輸入如下命令運行安裝插件
:PluginInstall
或者
sh執行vim +PluginInstall +qall

.vimrc加入如下配置 
Plugin 'shawncplus/phpcomplete.vim'
Source your .vimrc with :so % or otherwise reload your vim
Run the :PluginInstall commmand


三.函數,文件自動注釋插件 DoxygenToolkit.vim

http://www.vim.org/scripts/script.php?script_id=987
https://github.com/vim-scripts/DoxygenToolkit.vim

安裝插件 
Copy to your '~/.vim/plugin' directory
cp -r plugin /usr/shar/vim/vim74/


let g:DoxygenToolkit_commentType = "php"
let g:DoxygenToolkit_authorName="coolbaby"
let s:licenseTag = "Copyright(C)\<enter>"
let s:licenseTag = s:licenseTag . "For free\<enter>"
let s:licenseTag = s:licenseTag . "All right reserved\<enter>"
let g:DoxygenToolkit_licenseTag = s:licenseTag
let g:DoxygenToolkit_briefTag_funcName="yes"
let g:doxygen_enhanced_color=1

let g:DoxygenToolkit_briefTag_pre="@desc function  "
let g:DoxygenToolkit_paramTag_pre="@params [type] "
let g:DoxygenToolkit_returnTag="@return "
"let g:DoxygenToolkit_blockHeader="--------------------------------------------------------------------------"
"let g:DoxygenToolkit_blockFooter="----------------------------------------------------------------------------"
"let g:DoxygenToolkit_authorName="Mathias Lorente"
"let g:DoxygenToolkit_licenseTag="My own license"

doxygen使用

注釋操作都在vim命令行模式下操作:

1 注釋類型(C/C++/// 或者, Python:##和#):
在vim中,默認C++注釋為,但是如果你更喜歡使用///,只需要在你的配置文件.
vimrc中添加如下語句:
let g:DoxygenToolkit_commentType="C++"。

2 許可:
在vim中,將光標放在將要寫doxygen許可注釋的那一行,然后,執行命令:DoxLic。這將會生成許可注釋並將光標放置在剛才那一行之后。

3 作者:

在vim中,將光標放在想要添加doxygen作者注釋的地方。然后執行命令:DoxAuthor。這將會生成一個框架,如果沒有為其設置變量則將光標放置在@author標簽之后,或者放在在框架之后。

4 函數/類注釋:
在vim中,將光標放置在函數頭部那一行(或者函數的返回變量)或者類。然后執行命令:Dox。這將生成框架並且將光標放置在@brief標簽后。

5 忽略代碼片段(只有C/C++):
在vim中,如果你想要忽略所有在塊中的代碼片段,類似: #ifdef DEBUG ... #endif你只需要執行以下命令:DoxUndoc(DEBUG)!

6 組:
在vim中,執行命令:DoxBlock在后面的行中插入一個doxygen塊。

四.ctags,標簽Taglist

taglist是一個用於顯示定位程序中各種符號的插件,例如宏定義、變量名、結構名、函數名這些東西 我們將其稱之為符號(symbols),而在taglist中將其稱之為tag。

顯然,要想將程序文件中的tag顯示出來,需要事先了解全部tag的信息,並將其保存在一個文件中,然后去解析對應的tag文件。

taglist做的僅僅是將tag文件中的內容解析完后顯示在Vim上而已。

tag掃描以及數 據文件的生成則是由ctags(Exuberant Ctags)這一工具完成的

所以在使用taglist之前,你的電腦需要裝有ctags。

1)、vim插件tags安裝與配置

ctags可以建立源碼樹的標簽索引(標簽就是一個標識符被定義的地方,如函數定義),使程序員在編程時能迅速定位函數、變量、宏定義等位置去查看原形

Ctags工具是用來遍歷源代碼文件生成tags文件,這些tags文件能被編輯器或其它工具用來快速查找定位源代碼中的符號(tag/symbol),如變量名,函數名等。比如,tags文件就是Taglist和OmniCppComplete工作的基礎。

以下是在centos6.7下ctags的下載安裝和配置過程:

參考資料

https://github.com/shawncplus/phpcomplete.vim/wiki/Patched-ctags
http://blog.csdn.net/duguteng/article/details/7412652

ctags相關網站

http://ctags.sourceforge.net/
當前的PHP支持ctags發布(5.8)不支持當前的PHP版本的新功能名稱空間、特征或舊功能接口。
有一個改進版的https://ctags.io/
改進版ctags源碼下載
https://github.com/shawncplus/phpcomplete.vim/raw/master/misc/ctags-5.8_better_php_parser.tar.gz

ctags安裝

wget "https://github.com/shawncplus/phpcomplete.vim/raw/master/misc/ctags-5.8_better_php_parser.tar.gz" -O ctags-5.8_better_php_parser.tar.gz
tar xvf ctags-5.8_better_php_parser.tar.gz
./configure
make
make install

運行
ctags --version 將會看到 Exuberant Ctags Development表示安裝成功

使用ctags

cd /path/to/your/projects/root
運行
ctags -R --fields=+aimS --languages=php
參數說明

    -a
    Access (or export) of class members; Adds the access field like: access: public

    -i
    Inheritance information; Adds the inherits field like: inherits:RuntimeException,ExceptionInterface

    -m
    Implementation information; Adds the implementation field like: implementation:abstract

    -S
    Signature of routine; Adds the signature field like: signature:($a, $b)

These extra fields will show up in the tag stack like this:
使用額外的參數字段將會看到類似下面的堆棧調試信息

6 F   f    handle            vendor/monolog/monolog/src/Monolog/Handler/NullHandler.php
             class:Monolog\Handler::NullHandler access:public signature:(array $record)
             public function handle(array $record)


2)、taglist

Taglist用於列出了當前文件中的所有標簽(宏, 全局變量, 函數名等)

安裝Taglist

方式1:vim-addons install taglist
方式2:
官網http://vim-taglist.sourceforge.net/
https://sourceforge.net/projects/vim-taglist/files/
https://sourceforge.net/projects/vim-taglist/files/vim-taglist/4.6/taglist_46.zip
taglist的github地址
https://github.com/vim-scripts/taglist.vim
目前最新版本是4.6
下載之后解壓,拷備目錄中的2個目錄到vim目錄
cp -r doc plugin /usr/share/vim/vim74/

配置Taglist

在Vim配置文件中,可通過"let"語句設定以下變量控制taglist:

Tlist_GainFocus_On_ToggleOpen : 為1則使用TlistToggle打開標簽列表窗口后會獲焦點至於標簽列表窗口;為0則taglist打開后焦點仍保持在代碼窗口
Tlist_Auto_Open : 為1則Vim啟動后自動打開標簽列表窗口
Tlist_Close_On_Select : 選擇標簽或文件后是否自動關閉標簽列表窗口
Tlist_Exit_OnlyWindow : Vim當前僅打開標簽列表窗口時,是否自動退出Vim
Tlist_Use_SingleClick : 是否將默認雙擊標答打開定義的方式更改為單擊后打開標簽
Tlist_Auto_Highlight_Tag : 是否高亮顯示當前標簽。命令":TlistHighlightTag"也可達到同樣效果
Tlist_Highlight_Tag_On_BufEnter : 默認情況下,Vim打開/切換至一個新的緩沖區/文件后,標簽列表窗口會自動將當前代碼窗口對應的標簽高亮顯示。TlistHighlight_Tag_On_BufEnter置為0可禁止以上行為
Tlist_Process_File_Always : 為1則即使標簽列表窗口未打開,taglist仍然會在后台處理vim所打開文件的標簽
Tlist_Auto_Update : 打開/禁止taglist在打開新文件或修改文件后自動更新標簽。禁止自動更新后,taglist僅在使用:TlistUpdate,:TlistAddFiles,或:TlistAddFilesRecursive命令后更新標簽
Tlist_File_Fold_Auto_Close : 自動關閉標簽列表窗口中非激活文件/緩沖區所在文檔標簽樹,僅顯示當前緩沖區標簽樹
Tlist_Sort_Type : 標簽排序依據,可以為"name"(按標簽名排序)或"order"(按標簽在文件中出現的順序,默認值)
Tlist_Use_Horiz_Window : 標簽列表窗口使用水平分割樣式
Tlist_Use_Right_Window : 標簽列表窗口顯示在右側(使用垂直分割樣式時)
Tlist_WinWidth : 設定水平分割時標簽列表窗口的寬度
Tlist_WinHeight : 設定垂直分割時標簽列表窗口的高度
Tlist_Inc_Winwidth : 顯示標簽列表窗口時允許/禁止擴展Vim窗口寬度
Tlist_Compact_Format : 減少標簽列表窗口中的空白行
Tlist_Enable_Fold_Column : 是否不顯示Vim目錄列
Tlist_Display_Prototype : 是否在標簽列表窗口用標簽原型替代標簽名
Tlist_Display_Tag_Scope : 在標簽名后是否顯示標簽有效范圍
Tlist_Show_Menu : 在圖型界面Vim中,是否以下拉菜單方式顯示當前文件中的標簽
Tlist_Max_Submenu_Item : 子菜單項上限值。如子菜單項超出此上限將會被分隔到多個子菜單中。缺省值為25
Tlist_Max_Tag_Length : 標簽菜單中標簽長度上限

在vim配置文件/home/user/.vimrc中加入如下的配置:

"-- Taglist setting --
"自動更新taglist
let Tlist_Auto_Update = 1
"設置taglist寬度
let Tlist_WinWidth=40       
"因為我們放在環境變量里,所以可以直接執行
let Tlist_Ctags_Cmd='ctags' 
"讓窗口顯示在右邊,0的話就是顯示在左邊
let Tlist_Use_Right_Window=1 
"讓taglist可以同時展示多個文件的函數列表
let Tlist_Show_One_File=0 
"非當前文件,函數列表折疊隱藏
let Tlist_File_Fold_Auto_Close=1 
 "當taglist是最后一個分割窗口時,自動推出vim
let Tlist_Exit_OnlyWindow=1
"為1則即使標簽列表窗口未打開,taglist仍然會在后台處理vim所打開文件的標簽
let Tlist_Process_File_Always=1 
let Tlist_Inc_Winwidth=0

標簽列表窗口快捷鍵

taglist快捷鍵:

<CR> : 代碼窗口跳轉到標簽列表窗口中光標所在標簽定義處
o : 在新建代碼窗口中跳轉到標簽列表窗口中光標所在標簽定義處
P : 跳轉至上一個窗口的標簽處
p : 代碼窗口中內容跳轉至標簽定義處,光標保持在標簽列表窗口中
t : 在Vim新標簽窗口中跳轉至標簽定義處。如文件已經在Vim標簽窗口中打開,則跳轉至此標簽窗口
Ctrl-t : 在Vim新標簽窗口處跳轉至標簽定義處
 : 顯示光標當前所在標簽原型。對文件標簽,顯示文件的全路徑名,文件類型和標簽數量。對標簽類型(指如variable/function等類別),顯示標簽類型和擁有標簽的數量;對函數/變量等普通標簽,顯示其定義的原型
u : 更新標簽列表窗口中的標簽信息
s : 切換標簽排序類型(按名稱序或出現順序)
d : 移除當前標簽所在文件的所有標簽
x : 擴展/收縮標簽列表窗口
+ : 展開折疊節點*
- : 折疊結點*
* : 展開所有結點
= : 折疊所有節點
[[ : 跳轉至上一個文件標簽的頭部
<Backspace> : 跳轉至上一個文件標簽頭部
]] : 跳轉至下一個文件標簽頭部
<Tab> : 跳轉至下一個文件標簽頭部
q : 關閉標簽列表窗口
F1 : 顯示幫助**

taglist命令

taglist在Vim中提供了以下擴展命令:

:TlistAddFiles {files(s)} [file(s)...] 添加一或多個指定文件(的標簽項)到標簽列表窗口中。文件名表達式中可使用通配符(*);如文件名中帶有空格,需要使用反斜杠對空格進行轉義("\ ")
:TlistAddFilesRecursive {directory} [{pattern}] 遍歷指定路徑{directory},將與模式{pattern}相匹配的文件加入標簽列表窗口。如未指定pattern,則使用缺省值'*'。如路徑中包含空格,需使用反斜杠'\'轉義("\ ")
:TlistClose 關閉標簽列表窗口
:TlistDebug [filename] 記錄taglist插件的調試信息。如指定了filename,則調試信息將被寫入此指定文件(如文件已存在,內容將被覆蓋);如未指定filename,則調試信息寫入腳本的局部變量中
:TlistLock 鎖定標簽列表,並且不處理新打開的文件
:TlistMessage 僅當調試信息寫入腳本局部變量時有效,顯示記錄的調試信息
:TlistOpen 打開並跳轉至標簽列表窗口
:TlistSessionSave {filename} 將當前打開文件及其標簽信息寫入指定文件
:TlistSessionLoad {filename} 從指定文件載入所保存的會話信息
:TlistShowPrototype [filename] [linenumber] 顯示指定文件中指定代碼行或之前的標簽的原型。如未指定文件名/行號,則使用當前文件名/當前行號
:TlistShowTag [filename] [linenumber] 顯示指定文件中指定代碼行或之前標簽名稱。如未指定文件名/行號,則使用當前文件名/當前行號
:TlistHighlightTag 加亮顯示標簽窗口中的當前標簽
:TlistToggle 在打開和關閉狀態間切換標簽窗口的狀態。標簽窗口切換至打開狀態后仍然光標保持在代碼窗口中
:TlistUndebug 停止記錄taglist插件調試信息
:TlistUnlock 解鎖標簽列表,並處理新打開的文件
:TlistUpdate 更新當前緩沖區的標簽信息

taglist全局函數

taglist插件為Vim提供了一些全局函數,可供其他插件使用:

Tlist_Update_File_Tags({filename}, {filetype}) 以指定文件類型更新指定文件的標簽信息。如taglist插件此前未處理過指定文件,則會調用ctags對文件進行分析
Tlist_Get_Tag_Prototype_By_Line([{filename}, {linenumber}]) 獲取指定文件中指定行號或之前標簽的原型信息。如未指定文件名/行號,則使用當前緩沖區對應文件/當前行號
Tlist_Get_Tagname_By_Line ([{filename}, {linenumber}]) 獲取指定文件中指定行號或之前標簽的名稱信息。如未指定文件名/行號,則使用當前緩沖區對應文件/當前行號
Tlist_Set_App({appname}) 設置正在控制taglist的插件名稱

使用方法:
http://blog.csdn.net/skyflying2012/article/details/8112144參考

在源碼目錄下,執行ctags -R對各目錄遞歸創建生成tags文件

用Vim打開源碼文件,以命令模式執行Tlist,即可啟用Taglist插件, 可以查看taglist的幫助信息

:TlistToggle可以關閉,打開taglist
使用鍵映射關閉打開

nnoremap <silent> <F8> :TlistToggle<CR>

在Vim中加載代碼文件后,可以使用以下命令控制taglist

:TlistOpen 打開並將輸入焦點至於標簽列表窗口
:TlistClose 關閉標簽列表窗口
:TlistToggle 切換標簽列表窗口狀態(打開←→關閉),標簽列表窗口是否獲得焦點取決於其他配置
ctl-w + w 或ctl-w + 方向鍵 窗口切換(taglist本質上是一個vim分隔窗口,因此可以使用ctl-w系列快捷鍵對窗口進行切換操作)
使用鼠標單擊切換當前窗口,或在標簽列表窗口某標簽上雙擊使代碼窗口內窗跳轉至標簽定義
標簽列表窗口內,當光標停留在某個標簽之上時,回車鍵可切換代碼窗口內容至標簽定義; 'o',在分隔窗口中顯示;'t',在vim新標簽窗口中顯示

官方使用手冊
http://vim-taglist.sourceforge.net/manual.html

進入vim后用命令":Tlist"打開/關閉taglist窗口
幫助文檔
:help taglist.txt

五.代碼折疊

折疊用於把緩沖區內某一范圍內的文本行顯示為屏幕上的一行。就像一張紙,要它縮短
些,可以把它折疊起來:

 +------------------------+
 | 行 1                   |
 | 行 2                   |
 | 行 3                   |
 |_______________________ |
 \                        \
  \________________________\
  / 被折疊的行             /
 /________________________/
 | 行 12                  |
 | 行 13                  |
 | 行 14                  |
 +------------------------+

那些文本仍然在緩沖區內而沒有改變。受到折疊影響的只是文本行顯示的方式。

折疊的好處是,通過把多行的一節折疊成帶有折疊提示的一行,會使你更好地了解對文本
的宏觀結構。

折疊方式foldmethod

vim提供以下6種方法來選定折疊方式:

manual 手工定義折疊
indent 更多的縮進表示更高級別的折疊
expr 用表達式來定義折疊
syntax 用語法高亮來定義折疊
diff 對沒有更改的文本進行折疊
marker 對文中的標志折疊

折疊級別foldlevel

'foldlevel' 是個數值選項:數字越大則打開的折疊更多。

當 'foldlevel' 為 0 時,所有的折疊關閉。

當 'foldlevel' 為正數時,一些折疊關閉。

當 'foldlevel' 很大時,所有的折疊打開。

折疊欄foldcolumn

'foldcolumn' 是個數字,它設定了在窗口的邊上表示折疊的欄的寬度。當為0時,沒有折疊欄。最大是12。

一個打開的折疊由一欄來表示,頂端是 '-',其下方是 '|'。這欄在折疊結束的地方結束。當折疊嵌套時,嵌套的折疊出現在被包含的折疊右方一個字符位置。

一個關閉的折疊由 '+' 表示。

當折疊欄太窄而不能顯示所有折疊時,顯示一數字來表示嵌套的級別。

在折疊欄點擊鼠標,可以打開和關閉折疊:

點擊 '+' 打開在這行的關閉折疊

在任何其他非空字符上點擊,關閉這行上的打開折疊

在vim配置文件/home/user/.vimrc中加入如下的配置:

 "--fold setting vim折疊設置--

 " 用縮進來定義折疊
 set foldmethod=indext
 " 啟動vim時不要自動折疊代碼
 set foldlevel=100  
 " 設置折疊欄寬度
 set foldcolumn=5                            

常用命令

za  打開/關閉在光標下的折疊
zA  循環地打開/關閉光標下的折疊
zo  打開 (open) 在光標下的折疊
zO  循環打開 (Open) 光標下的折疊
zc  關閉 (close) 在光標下的折疊
zC  循環關閉 (Close) 在光標下的所有折疊
zM  關閉所有折疊
zR  打開所有的折疊
幫助文檔
:help usr_28.txt
:help fold.txt

參考資料
http://www.cnblogs.com/zhangsf/archive/2013/06/13/3134409.html

六、vim語法檢查插件syntastic

github地址

https://github.com/scrooloose/syntastic

環境需求

需要vim支持autocmd, eval, file_in_path, modify_fname, quickfix, reltime, and user_commands
檢查以上vim依賴是否安裝,出現+表示滿足要求

vim --version |grep autocmd

這個插件是用來做靜態語法檢查的,支持多語言,比如 Ruby,Python 和 JavaScript 等。實際上這個插件是個接口,背后的語法檢查是交給各個語言自己的檢查器,Ruby 實際使用ruby -c命令,JavaScript 使用 jshint,jslint 等。

注意安裝后jshint需要在系統環境變量中,否則需要指定path:
g:syntastic_jshint_exec
指定jshint的配置文件目錄:
g:syntastic_javascript_jshint_conf

最后但並非最不重要的:syntastic不知道如何自己做任何語法檢查。為了得到有意義的結果,你需要安裝相應的文件外部檢查你使用的類型

安裝syntastic with Pathogen

1.需要先安裝Pathogen

https://github.com/tpope/vim-pathogen
git clone https://github.com/tpope/vim-pathogen
cd vim-pathogen
cp -r autoload /usr/share/vim/vim74/

在vimrc中加入配置

execute pathogen#infect()

2.安裝syntastic

mkdir -p /usr/share/vim/vim74/bundle
cd /usr/share/vim/vim74/bundle
git clone https://github.com/scrooloose/syntastic.git

3.推薦配置syntastic

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

let g:syntastic_error_symbol = 'err!'
let g:syntastic_warning_symbol = '!'

nmap <A-up> :lprev<cr>
nmap <A-down> :lnext<cr>
nmap <A-right> :ll<cr>

4.安裝php語法分析器phpcs

方式1
pear install PHP_CodeSniffer
方式2
composer global require "squizlabs/php_codesniffer=*"

vim幫助文檔:help syntastic

七、目錄管理工具NERDtree

安裝方式
用插件管理器pathogen.vim或者Vundle安裝

cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git

啟動vim,自動打開NERDtree

autocmd vimenter * NERDTree

How can I open a NERDTree automatically when vim starts up if no files were specified?

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

映射快捷鍵

map <C-n> :NERDTreeToggle<CR>

如果只剩下NERDTree最后一個窗口,如何關閉NERDTree

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

Can I have different highlighting for different file extensions?
不同的擴展名,顯示不同的顏色

" NERDTress File highlighting
function! NERDTreeHighlightFile(extension, fg, bg, guifg, guibg)
 exec 'autocmd filetype nerdtree highlight ' . a:extension .' ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:guibg .' guifg='. a:guifg
 exec 'autocmd filetype nerdtree syn match ' . a:extension .' #^\s\+.*'. a:extension .'$#'
endfunction

call NERDTreeHighlightFile('jade', 'green', 'none', 'green', '#151515')
call NERDTreeHighlightFile('ini', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('md', 'blue', 'none', '#3366FF', '#151515')
call NERDTreeHighlightFile('yml', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('config', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('conf', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('json', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('html', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('styl', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('css', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('coffee', 'Red', 'none', 'red', '#151515')
call NERDTreeHighlightFile('js', 'Red', 'none', '#ffa500', '#151515')
call NERDTreeHighlightFile('php', 'Magenta', 'none', '#ff00ff', '#151515')

How can I change default arrows?
改變默認的箭頭

let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'

http://vim.wikia.com/wiki/Understanding_VIMRUNTIME


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM