參照文章【1】【2】的辦法,將vim打造成一個Python開發環境。文章中使用的是 pathogen + git 來管理 Vim 插件的。對這種方式還不太明白的同學可以參考【3】中的介紹。pathogen 改變了原先 Vim 只能把插件全部扔到 .vim 目錄下的操作方式,使得各個插件可以以一個獨立的文件夾存在於 .vim/bundle 目錄中,添加和刪除插件都變的非常清爽。使用 git 強大的子模塊管理功能,可以實現方便的插件安裝和自動升級。
1. 准備工作
創建.vim目錄,以及其下的autoload和bundle目錄。(如果先前已有.vim目錄,可以先備份一下)
$ mkdir ~/.vim/ $ mkdir ~/.vim/{autoload,bundle} $ cd ~/.vim/ $ git init
如果Ubuntu中沒有安裝Git,需要首先安裝Git【4】。
2. 安裝pathogen
下載vim-pathogen【5】,將zip包中autoload下的pathogen.vim文件copy到 ~/.vim/autoload文件夾下。
然后添加下面的命令到~/.vimrc文件中(.vimrc文件不存在的話,先創建.vimrc文件)。
filetype off
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
當運行pathogen命令的時候文件格式檢測必須關閉,所以把filetype off放到最前面(UPTATED(20141022))。
這樣,pathogen插件就安裝好了。
UPDATED(20141017):
(1) 當按照本文設置完成后,使用vim時,會報錯誤:
Error detected while processing /home/maxime/.vim/bundle/tasklist/plugin/tasklist.vim: line 369: E227: mapping already exists for \t
錯誤原因是: tasklist will not map to <leader>t if a mapping to <Plug>TaskList is found.So you just need to create a mapping to <Plug>TaskList in your vimrc.
解決方法是:在~/.vimrc文件中添加mapping【7】:
nnoremap <leader>v <Plug>TaskList
(2) 添加完mapping后,使用vim時,仍然會彈出以下命令行:
Change pathogen#runtime_append_all_bundles() to pathogen#infect()
Press ENTER or type command to continue
原因:call pathogen#infect() which can be used in the latest version of pathogen now deals with this filetype problem (and is much easier to remember and type than runtime_append_all_bundles)【8】
這是提示你 pathogen#runtime_append_all_bundles() 太老了,現在用 pathogen#infect() 替代它,而且比pathogen#runtime_append_all_bundles() 更容易記,還能少打好多字呢。
所以你需要把上面設定中的 call pathogen#runtime_append_all_bundles()改為 pathogen#infect() 。
UPTATED(20141022)
當在vimrc中設置 設置語法高亮時,發現不起作用,如果將 filetype off 注掉,或者將其提至syntax on之前,語法高亮就會起作用。
所以需要將filetype off 提至vimrc文件的最頂端。
UPDATED(20141024)
(1)關於pathogen為何要關閉filetype detection的原因,請參見【12】,同時也提供了解決辦法,在 filetype off后,為不影響其他功能,在設置完pathogen后,再將filetype detection打開。
(2)必須設置 filetype off,是因為Vim's file detection的一個bug,這個bug在 >Vim 7.3.430的版本中已修復了,因此如果vim的版本>Vim 7.3.430,則不必設置filetype off【13】。
(3)為避免不必要的麻煩,我們將pathogen插件設置提前到vimrc的最開始。
3. Git Submodule添加Plugin到Git Repository中
Git Submodule的作用,簡單來說就是可以將別人的 git 掛入到你目前 git 的任何位置。添加所有的vim plugins作為我們當前Repository的submodule。
關於Git Submodule的詳細教程,參考【6】。
在~/.vim目錄下執行以下命令
git submodule add http://github.com/tpope/vim-fugitive.git bundle/vim-fugitive git submodule add https://github.com/msanders/snipmate.vim.git bundle/snipmate git submodule add https://github.com/tpope/vim-surround.git bundle/vim-surround git submodule add https://github.com/tpope/vim-git.git bundle/vim-git git submodule add https://github.com/ervandew/supertab.git bundle/supertab git submodule add https://github.com/sontek/minibufexpl.vim.git bundle/minibufexpl git submodule add https://github.com/wincent/Command-T.git bundle/command-t git submodule add https://github.com/mitechie/pyflakes-pathogen.git bundle/pyflakes-pathogen git submodule add https://github.com/mileszs/ack.vim.git bundle/ack git submodule add https://github.com/sjl/gundo.vim.git bundle/gundo git submodule add https://github.com/fs111/pydoc.vim.git bundle/pydoc git submodule add https://github.com/vim-scripts/pep8.git bundle/pep8 git submodule add https://github.com/alfredodeza/pytest.vim.git bundle/pytest git submodule add https://github.com/reinh/vim-makegreen bundle/vim-makegreen git submodule add https://github.com/vim-scripts/TaskList.vim.git bundle/tasklist git submodule add https://github.com/vim-scripts/The-NERD-tree.git bundle/the-nerd-tree git submodule add https://github.com/sontek/rope-vim.git bundle/rope-vim git submodule init git submodule update git submodule foreach git submodule init git submodule foreach git submodule update
NOTE:在安裝的過程中,minibufexpl.vim.git和rope-vim.git沒有安裝成功,需要用戶名和密碼。
現在,我們已經將vim配置放到了git倉庫中。執行完上面的操作后,在.vim目錄下會生成一個隱藏文件:.gitmodules。
UPDATED(20141020):
添加了這些插件作為submodule后,這些插件中會產生一些untracked文件(比如bundle/snipmate/doc/tags 文件)。在.gitignore中過濾是不起作用的。
解決方法是(【9】是stackoverflow中所提問題,【10】是解決方案的出處):
By adding the ignore = dirty option to each one of the entries in the .gitmodules file.
[submodule "zen-coding-gedit3"] path = zen-coding-gedit3 url = git://github.com/leafac/zen-coding-gedit3.git ignore = dirty
4. 將其提交到GitHub
由於.vimrc 文件不在.vim目錄下,所以將其從~目錄copy到.vim目錄,然后add。(Please use method in UPDATED2)
git status git commit -m "add .vim plugins" cp ~/.vimrc . git add .vimrc git status git commit -m "add .vimrc file" git status git remote add origin https://github.com/zhchnchn/VimConfig.git git push -u origin master
UPDATED(20141020):
git push -u origin master:Push到遠程倉庫,同時設置跟蹤分支,下次push的時候,直接輸入git push即可,系統會自動用本地master分支跟蹤遠程master分支 。
UPDATED2(20141020)
為了將所有的文件都置於版本控制下,我們需要將~/.vimrc 移到~/.vim下(有gvimrc的話,將其一並移動),為了讓他不再是一個隱藏文件,我們去掉了前面的點(.)【11】。
mv ~/.vimrc ~/.vim/vimrc mv ~/.gvimrc ~/.vim/gvimrc
當啟動vim時,仍然會試圖尋找~下的.vimrc文件,所以我們創建一個symbolic link。將~/.vim/vimrc文件link 到 ~/.vimrc文件。
ln -s ~/.vim/vimrc ~/.vimrc ln -s ~/.vim/gvimrc ~/.gvimrc
將以上修改commit到github。
5. 如何將vim插件同步到別的機器上
將.vim配置提交到Github之后,如何同步到別的機器上使用呢?
NOTE:下面所涉及的“本地”都是指別的機器的“本地”目錄。
(1)將.vim配置clone到別的機器的~/.vim目錄下(如果沒有.vim則新建一個)
git clone git@github.com:zhchnchn/VimConfig.git ~/.vim
(2)創建symbolic link,將~/.vim/vimrc文件link 到 ~/.vimrc文件。
ln -s ~/.vim/vimrc ~/.vimrc
(3)如果修改了本地的vimrc文件,在 git add和git commit之后,可以直接git push提交到github上。
(4)查看clone到本地的bundle目錄下的各個插件目錄,發現它們都是空的,那是因為還沒同步到本地來。將他們同步到本地【11】:
~/.vim$ git submodule init
~/.vim$ git submodule update
前一條命令作用是“registers the submodule”。
后一條的作用是“Checks out the version of each plugin's repository which was committed to the .vim repository before”.,它會將各個插件下載到本地。
6. 如何在pathogen下安裝主題風格插件呢?
pathogen 無法安裝配色主題風格,因為它會將所有插件都安裝在bundle目錄下,而vim只認~/.vim/colors/下的主題風格。因此只能將主題插件手工放置於 ~/.vim/colors/下【14】。
比如要安裝下面的2個主題插件,需要將下載的插件中的 *.vim 文件(即solarized.vim、molokai.vim文件,而非包含*.vim 文件的目錄)拷貝至 ~/.vim/colors/目錄下(如果沒有則手動創建)。
- solarized(https://github.com/altercation/vim-colors-solarized )
- molokai(https://github.com/tomasr/molokai )
然后在 .vimrc文件中設定主題:
" 配色方案 set background=dark colorscheme solarized "colorscheme molokai
Refer
【1】Turning Vim into a modern Python IDE(http://www.sontek.net/blog/2011/05/07/turning_vim_into_a_modern_python_ide.html)
【2】Turning Vim into a modern Python IDE(中譯文,其中有好多錯誤,請對照引文參考。http://python.42qu.com/11180003)
【3】教程:使用 pathogen + git 管理 Vim 插件(http://lostjs.com/2012/02/04/use-pathogen-and-git-to-manage-vimfiles/)
【4】Ubuntu12.04安裝Git(http://www.cnblogs.com/zhcncn/p/4030078.html)
【5】vim-pathogen(https://github.com/tpope/vim-pathogen)
【6】Git Submodule使用完整教程(http://www.kafeitu.me/git/2012/03/27/git-submodule.html)
【7】Resolving a vim plugin mapping conflict - mapping already exists for \t (http://stackoverflow.com/questions/18346350/resolving-a-vim-plugin-mapping-conflict-mapping-already-exists-for-t)
【8】Pathogen does not load plugins(http://stackoverflow.com/questions/3383502/pathogen-does-not-load-plugins/6365667)
【9】How to get rid of git submodules untracked status?(http://stackoverflow.com/questions/5126765/how-to-get-rid-of-git-submodules-untracked-status)
【10】How to ignore changes in git submodules(http://www.nils-haldenwang.de/frameworks-and-tools/git/how-to-ignore-changes-in-git-submodules)
【11】Synchronizing plugins with git submodules and pathogen (http://vimcasts.org/transcripts/27/en/)
【12】A Brief Note On Pathogen For Vim(http://blog.darevay.com/2010/10/a-brief-note-on-pathogen-for-vim/)
【13】https://github.com/gmarik/Vundle.vim/issues/176
【14】所需即所獲:像 IDE 一樣使用 vim(https://github.com/yangyangwithgnu/use_vim_as_ide)