一、背景
由於之前配置錯誤,導致我的YouCompleteMe這個插件不能用了,一直提示:
ERROR:Required vim compiled with +python.
YouCompleteMe unavailable: Required vim compiled with Python2.7.1+ or 3.4+ support.
然而我通過vim --version(或者vim --version | grep python)查看到我是有python支持的啊,+python/dyn和+python3/dyn。但是就是提示python不可用,也嘗試了echo has('python')和echo has('python3'),但return的結果都是0,不是1。
最終通過一番查閱資料發現了問題關鍵,進入vim並輸入:h python-2-and-3,回車結果如下:
意思就是說當使用全局符號時,會導致第二個版本的python崩潰,所有當使用全局符號時只能一根版本的python是有效的。
然后這里是debian系統維護者曾經寫的一封解釋信(Google到的),附上鏈接:(https://bugs.debian.org/cgi-bin/bugreport.cgi?msg=10;att=0;bug=729924)
On Mon, Nov 18, 2013 at 05:59:02PM -0500, Barry Warsaw wrote:
> I'm not a vim user but I'm am very interested in porting the world to
> Python 3. To that end, I see that the current version of upstream vim
> supports Python 3, but building that is not currently enabled in the
> Debian package.
This is due to two things.
First, the way Debian builds Python prevents loading both libpython2 and
libpython3 in the same process, since Debian's builds necessitate
passing RTLD_GLOBAL to dlopen(). This means that when Vim is built to
support both Python 2 & 3, one has to choose between using plugins that
target Python 2 or Python 3 instead of the user being able to use both.
At the time, this meant I had to choose one and I went with Python 2 due
to the much higher prevalence of plugins targeting it. While Python 3
has gained a lot of ground since then and it's much more easy to write
code that works in both versions, my understanding is that the
RTLD_GLOBAL issue still exists. This means I still need to choose one
version and, while it's more of a toss up now, I think Python 2 is still
the right call here.
Second, I'm not keen on the state of dependency tracking with software
that dynamically loads libraries instead of linking against them. If
Vim dynamically loaded its own code that provides the language bindings,
and those were linked against the proper language libraries, then I
could easily provide vim-{lua,perl,python,ruby,tcl}-bindings packages
which expressed proper relationships on the corresponding language
libraries.
Since that isn't the case, we can then run into situations where bad
things happen during a library transition due to nothing telling the
transition trackers that Vim should actually be rebuilt or forcing the
users to upgrade Vim in sync with the library. See #611573 for some
previous discussion about this related to Vim's Perl support.
Cheers,
--
James
GPG Key: 4096R/331BA3DB 2011-12-05 James McCoy <jamessan@debian.org>
This means that when Vim is built to support both Python 2 & 3, one has to choose between using plugins that target Python 2 or Python 3 instead of the user being able to use both.
意思就是說debian系統中python2和python3是運行在同一進程的,也就意味着你只能選擇其中一個版本來使用,用python2就不能同時用python3。
綜上來看,解決辦法只能是重新編譯vim啦,舍去其中一個python版本。
二、vim的卸載:
- 1、使用apt-get的方式:
1、sudo apt-get autoremove vim vim-common vim-tiny vim-runtime
- 2、然后dpkg -l | grep vim看一下是否還存在有vim,如果有,請到dpkg -l中可看到你所有安裝的軟件,若有vim,那么卸載的時候可以用這兩種方式;如果沒有,那就全部卸載完成:
(PS:我的遇見了這種情況,apt-get autoremove 方式並沒有把所有的vim環境給我刪除掉,當時郁悶了好久。)
1、sudo dpkg --purge vim vim-common vim-addon-manager # 此種方式使用--purge參數,可將配置文件與安裝文件一同刪除
2、sudo dpkg -r vim vim-common vim-addon-manager # 此種方式只刪除安裝文件、不能刪除配置文件
三、重新安裝vim以及配置
1. vim安裝:
以我的為例,全程在root環境下:
step1: mkdir /opt/vim
step2: git clone https://github.com/vim/vim.git
step3: unzip vim-master.zip
step4: cd vim-master/
step5: make clean make distclean
step6: 安裝依賴庫:apt-get install libncurses5-dev python-dev python3-dev libgtk3.0-dev libatk1.0-dev libbonoboui2-dev libcairo2-dev libx11-dev libxpm-dev libxt-dev
step7: ./configure --with-features=huge --enable-multibyte --enable-rubyinterp --enable-python3interp --enable-luainterp --enable-cscope --enable-gui=gtk3 --enable-perlinterp --with-python3-config-dir=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/ --prefix=/usr/local/vim
step8: make
step9: make install
在上面step7中,我選擇了使用python3,如果使用python2的,請對應更改:--enable-python3interp為--enable-pythoninterp,--with-python3-config-dir=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/為--with-python-config-dir=/usr/lib/python2.7/config-x86_64-linux-gnu/
相關參數說明
--with-features=huge:支持最大特性
--enable-rubyinterp:打開對 ruby 編寫的插件的支持
--enable-pythoninterp:打開對 python 編寫的插件的支持
--enable-python3interp:打開對 python3 編寫的插件的支持
--enable-luainterp:打開對 lua 編寫的插件的支持
--enable-perlinterp:打開對 perl 編寫的插件的支持
--enable-multibyte:打開多字節支持,可以在 Vim 中輸入中文
--enable-cscope:打開對cscope的支持
--enable-gui=gtk3 表示生成采用 GNOME3 風格的 gvim
--with-python-config-dir=/usr/lib/python2.7/config-x86_64-linux-gnu/ 指定 python 路徑
--with-python3-config-dir=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/ 指定 python3路徑
--prefix=/usr/local/vim:指定將要安裝到的路徑
2. vim配置
- 安裝 vim 的插件管理器 vundle,退出root環境,為普通用戶:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
- vim ~/.vimrc,配置如下:
" vundle 環境設置
set nocompatible " be IMproved, required
filetype off
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必須位於 vundle#begin() 和 vundle#end() 之間
call vundle#begin()
" Let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
...... " 你自己需要的Plugin插件放在這里,例如:Plugin "Valloric/YouCompleteMe"
" All of your plugins must be added before the following line!
call vundle#end()
filetype plugin indent on
配置完后,:wq保存退出,並重新進入~/.vimrc,執行PluginInstall。
相關配置如下,參考:
"開啟真彩色支持
set termguicolors
"開啟256色支持
set t_Co=256
" 定義快捷鍵前綴 <Leader>
let mapleader=";"
" 開啟文件類型偵測
filetype on
" 根據不同文件類型加載不同插件
filetype plugin on
" 定義快捷鍵到行首和行尾
nmap LB 0
nmap LE $
" 設置快捷鍵將選中文本塊復制至系統剪貼板
vnoremap <Leader>y "+y
" 設置快捷鍵將系統剪貼板內容粘貼至 vim
nmap <Leader>p "+p
" 定義快捷鍵關閉當前分割窗口
nmap <Leader>q :q<CR>
" 定義快捷鍵保存當前窗口內容
nmap <Leader>w :w<CR>
" 定義快捷鍵保存所有窗口內容並退出 vim
nmap <Leader>WQ :wa<CR>:q<CR>
" 不做任何保存,直接退出 vim
nmap <Leader>Q :qa!<CR>
" 依次遍歷子窗口
nnoremap nw <C-W><C-W>
" 跳轉至右方的窗口
nnoremap <Leader>lw <C-W>l
" 跳轉至左方的窗口
nnoremap <Leader>hw <C-W>h
" 跳轉至上方的子窗口
nnoremap <Leader>kw <C-W>k
" 跳轉至下方的子窗口
nnoremap <Leader>jw <C-W>j
" 定義快捷鍵在結對符之間跳轉
nmap <Leader>M %
" 開啟實時搜索功能
set incsearch
" 搜索時大小寫不敏感
set ignorecase
" 關閉兼容模式
set nocompatible
" vim 自身命令行模式智能補全
set wildmenu
" vundle 環境設置
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必須位於 vundle#begin() 和 vundle#end() 之間
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'altercation/vim-colors-solarized'
Plugin 'tomasr/molokai'
Plugin 'vim-scripts/phd'
Plugin 'Lokaltog/vim-powerline'
Plugin 'octol/vim-cpp-enhanced-highlight'
Plugin 'nathanaelkane/vim-indent-guides'
Plugin 'derekwyatt/vim-fswitch'
Plugin 'kshenoy/vim-signature'
Plugin 'vim-scripts/BOOKMARKS--Mark-and-Highlight-Full-Lines'
Plugin 'majutsushi/tagbar'
Plugin 'vim-scripts/indexer.tar.gz'
Plugin 'vim-scripts/DfrankUtil'
Plugin 'vim-scripts/vimprj'
Plugin 'dyng/ctrlsf.vim'
Plugin 'terryma/vim-multiple-cursors'
Plugin 'scrooloose/nerdcommenter'
Plugin 'vim-scripts/DrawIt'
Plugin 'SirVer/ultisnips'
Plugin 'davidhalter/jedi-vim' " jedi-vim提供python語法、自動補全等支持
Plugin 'Valloric/YouCompleteMe'
Plugin 'derekwyatt/vim-protodef'
Plugin 'scrooloose/nerdtree'
Plugin 'fholgado/minibufexpl.vim'
Plugin 'gcmt/wildfire.vim'
Plugin 'sjl/gundo.vim'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'suan/vim-instant-markdown'
Plugin 'lilydjwg/fcitx.vim'
Plugin 'iCyMind/NeoSolarized'
" 插件列表結束
call vundle#end()
filetype plugin indent on
" 配色方案
set background=dark
colorscheme NeoSolarized
" 總是顯示狀態欄
set laststatus=2
" 顯示光標當前位置
set ruler
" 開啟行號顯示
set number
" 使用 NERDTree 插件查看工程文件。設置快捷鍵,速記:file list
nmap <Leader>fl :NERDTreeToggle<CR>
" 設置NERDTree子窗口寬度
let NERDTreeWinSize=32
" 設置NERDTree子窗口位置
let NERDTreeWinPos="rigth"
" 顯示隱藏文件
let NERDTreeShowHidden=1
" NERDTree 子窗口中不顯示冗余幫助信息
let NERDTreeMinimalUI=1
" 刪除文件時自動刪除文件對應 buffer
let NERDTreeAutoDeleteBuffer=1
" 高亮顯示當前行/列
set cursorline
"set cursorcolumn
" 高亮顯示搜索結果
set hlsearch
" 設置 gvim 顯示字體
set guifont=YaHei\ Consolas\ Hybrid\ 11.5
" 禁止折行
set nowrap
" 設置狀態欄主題風格
let g:Powerline_colorscheme='solarized256'
" 開啟語法高亮功能
syntax enable
" 允許用指定語法高亮配色方案替換默認方案
syntax on
" 自適應不同語言的智能縮進
filetype indent on
" 將制表符擴展為空格
set expandtab
" 設置編輯時制表符占用空格數
set tabstop=4
" 設置格式化時制表符占用空格數
set shiftwidth=4
" 讓 vim 把連續數量的空格視為一個制表符
set softtabstop=4
" 隨 vim 自啟動
let g:indent_guides_enable_on_vim_startup=1
" 從第二層開始可視化顯示縮進
let g:indent_guides_start_level=2
" 色塊寬度
let g:indent_guides_guide_size=1
" 快捷鍵 i 開/關縮進可視化
:nmap <silent> <Leader>i <Plug>IndentGuidesToggle
至此,vim基本的安裝配置就完成了,之前的問題也就解決了,效果如下:
補充:vim8支持vim編輯欄中分出一個shell窗口,可一邊編輯代碼,一邊運行。命令:“:terminal”