1. 程式人生 > >將Vim打造成伺服器端IDE

將Vim打造成伺服器端IDE

仔細想了下,伺服器端程式設計由於伺服器(一般都是Linux)並不會裝Eclipse,Visual Studio之類的IDE,所以用Vim還是比較好的選擇,還能提高Coding(敲鍵盤)能力,固化程式設計式思維。

愛上Coding!

  • .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# ls color
export CLICOLOR=1;
export LSCOLORS=gxfxaxdxcxegedabagacad;

# command display
# export PS1="\
[email protected]
\H \w\n\$ " export PS1='\[\033[01;32m\]\[email protected]\H\[\033[01;33m\] \w\[\033[34;40m\]\n> \[\033[00m\] ' # change open files -> 10000 ulimit -n 10000 # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH
  • ctags

brew install ctags

(Centos: yum install ctags)

  • vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
  • .vimrc.bundles

這個是vim vundle的配置檔案。

set nocompatible              " be iMproved, required
filetype off                  " required

set rtp+=~/.vim/bundle/vundle/
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
Bundle 'taglist.vim'
Bundle 'winmanager'
Bundle 'The-NERD-tree'
Bundle 'The-NERD-Commenter'
" Bundle 'fholgado/minibufexpl.vim'
Bundle 'minibufexplorerpp'
Bundle 'Yggdroot/indentLine'
Bundle 'scrooloose/syntastic'
Bundle 'vim-scripts/pylint.vim'
call vundle#end()            " required
filetype plugin indent on    " required
  • .vimrc

這個是vim的配置檔案,每次vim啟動時都會逐條執行裡面的配置選項。貼下我的通用配置 (還沒有包含外掛,按照下面的步驟增加外掛會增加.vimrc中的內容):

" vundle bundles
if filereadable(expand("~/.vimrc.bundles"))
	source ~/.vimrc.bundles
endif

" my own settings
syn on "語法支援
set laststatus=2 "始終顯示狀態列
set tabstop=4 "一個製表符的長度
set softtabstop=4 "一個製表符的長度(可以大於tabstop)
set shiftwidth=4 "一個縮排的長度
set expandtab "使用空格替代製表符
set smarttab "智慧製表符
set autoindent "自動縮排
set smartindent "智慧縮排
set number "顯示行號
set ruler "顯示位置指示器
set backupdir=/tmp "設定備份檔案目錄
set directory=/tmp "設定臨時檔案目錄
set ignorecase "檢索時忽略大小寫
set hls "檢索時高亮顯示匹配項
set helplang=cn "幫助系統設定為中文
set foldmethod=syntax "程式碼摺疊
set nobackup "不生成備份檔案
set guifont=Courier\ New:h15 "設定字型
set nowrap "長句在一行顯示
set encoding=utf-8

colorscheme pablo

" Tlist
let Tlist_Ctags_Cmd='/usr/local/bin/ctags'
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Use_Right_Window=1
let Tlist_Sort_Type = "name"
let Tlist_Auto_Open=1
nmap wt :Tlist<CR>

 " MiniBufExplorer
let g:miniBufExplMapWindowNavVim = 1   
let g:miniBufExplMapWindowNavArrows = 1   
let g:miniBufExplMapCTabSwitchBufs = 1   
let g:miniBufExplModSelTarget = 1  
let g:miniBufExplMoreThanOne=0

nmap be :MiniBufExplorer<CR>

" NERDTree
let g:NERDTree_title="[NERDTree]"

function! NERDTree_Start()
    exec 'NERDTree'
endfunction

function! NERDTree_IsValid()
    return 1
endfunction

" WinManager
let g:winManagerWindowLayout="NERDTree"

nmap wm :WMToggle<CR>

" Cscope
:set cscopequickfix=s-,c-,d-,i-,t-,e-

 

  • YouCompleteMe

安裝最新vim:brew install vim

安裝vundle: https://github.com/VundleVim/Vundle.vim#quick-start

cd ~/.vim/bundle/

git clone--recursive https://github.com/Valloric/YouCompleteMe.git 獲取最新的倉庫,而後使用git submodule update --init --recursive確認倉庫的完整性。

在.vimrc中新增:

call vundle#begin()
. . . 
Plugin 'Valloric/YouCompleteMe'
. . .
call vundle#end()

編譯YCM:

brew install CMake

cd ~/.vim/bundle/YouCompleteMe

./install.sh --clang-completer

 

vim

:BundleInstall

 

cp  ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py ~/.vim/

vim ~/.vim/.ycm_extra_conf.py

加上這些:

'-isystem',                                                                                                 
'/usr/include',                                                                                             
'-isystem',                                                                                                 
'/usr/local/include',                                                                                       
'-isystem',                                                                                                 
'/Library/Developer/CommandLineTools/usr/include',                                                          
'-isystem',                                                                                                 
'/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',

註釋掉:

#    try:

#      final_flags.remove( '-stdlib=libc++' )

#    except ValueError:

#      pass

return { 'flags': final_flags, 'do_cache': True }

 

vim .vimrc

" YouCompleteMe

let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

set completeopt=longest,menu    "讓Vim的補全選單行為與一般IDE一致(參考VimTip1228) 

autocmd InsertLeave * if pumvisible() == 0|pclose|endif "離開插入模式後自動關閉預覽視窗 

inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"    "回車即選中當前項

"youcompleteme  預設tab  s-tab 和自動補全衝突
"let g:ycm_key_list_select_completion=['<c-n>']
let g:ycm_key_list_select_completion = ['<Down>']
"let g:ycm_key_list_previous_completion=['<c-p>']
let g:ycm_key_list_previous_completion = ['<Up>']
let g:ycm_confirm_extra_conf=0 "關閉載入.ycm_extra_conf.py提示
let g:ycm_collect_identifiers_from_tags_files=1 " 開啟 YCM 基於標籤引擎
let g:ycm_min_num_of_chars_for_completion=2 " 從第2個鍵入字元就開始羅列匹配項
let g:ycm_cache_omnifunc=0  " 禁止快取匹配項,每次都重新生成匹配項
let g:ycm_seed_identifiers_with_syntax=1    " 語法關鍵字補全
nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>    "force recomile with syntastic
"nnoremap <leader>lo :lopen<CR> "open locationlist
"nnoremap <leader>lc :lclose<CR>    "close locationlist
inoremap <leader><leader> <C-x><C-o>
"在註釋輸入中也能補全
let g:ycm_complete_in_comments = 1
"在字串輸入中也能補全
let g:ycm_complete_in_strings = 1
"註釋和字串中的文字也會被收入補全
let g:ycm_collect_identifiers_from_comments_and_strings = 0

let g:clang_user_options='|| exit 0' 

nnoremap <C-g> :YcmCompleter GoToDefinitionElseDeclaration<CR> " (Ctrl + g)跳轉到定義處

" 另外:YCM提供的跳躍功能採用了vim的jumplist,往前跳和往後跳的快捷鍵為Ctrl+O以及Ctrl+I。

 

解決標頭檔案找不到的問題:

在~/.vim/.ycm_extra_conf.py檔案中加入:

# nginx                                                                                                   
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/core',
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/http',
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/mail',
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/os/unix',
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/event',
'-isystem',
'/Users/zhangyaodong/Documents/Projects/Open/nginx-1.12.1/src/stream'

 

  • echofunc

http://www.vim.org/scripts/script.php?script_id=1735

下下來後直接扔 ~/.vim/plugin/, 需配合ctags,ctags -R --fields=+lS

MAC includes: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/

 

  • Syntastic

" syntastic,vundle是從git上下載外掛的,配置中scrooloose是作者名,後面是repo
Bundle  'scrooloose/syntastic'

vim ~/.vimrc

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

" 關閉Syntastic視窗:    :lclose  (在程式碼視窗,不是在Syntastic執行)

" 開啟Syntastic視窗:    :lopen

 

  • pylint

sudo easy_install pylint

vim ~/.vimrc

Bundle 'vim-scripts/pylint.vim'

let g:syntastic_python_checkers=['pylint']
let g:syntastic_python_pylint_args='--disable=C0111,R0903,C0301'

  • 待研究

cscope (需要嗎?)

easymotion