1. 程式人生 > >vim的解除安裝以及環境的配置小記

vim的解除安裝以及環境的配置小記

一、背景

由於之前配置錯誤,導致我的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 <
[email protected]
>

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”