Vim 8 支援 Python 3 的一些坑
最近寫了一些 Python3 程式,還是習慣在 Linux 下用 Vim 編碼,可自己 Vim 也沒有用什麼外掛,總感覺效率不好,就想著搗鼓下 Vim,弄個 Python IDE。
遇到的第一個問題讓 Vim 支援 Python3,這樣就能支援一些 Python3 程式或軟體,看上去挺簡單,沒想到最後花了長時間,簡單和大家分享下。
心得就是越折騰,理解的就越透徹,使用 Linux 解決問題不能死板硬套,一定要注意特定版本的 Linux 發行版,特定的環境,特定的軟體版本。
我使用的作業系統是 Ubuntu 16。預設安裝的 Vim 是 7.4 版本,本次打算升級到 8.1 版本。如何知道 Vim 預設支援的 Python 版本?輸入下列命令就能知道:
$ vim --version -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7
初步的打算就是安裝一個新的 Vim(不是替代 Vim 舊版本),新版本支援 Python3 即可。如何讓 Python2 和 Python3 共存,可以見《是時候配置一個Python3的開發環境了》
首先安裝各類依賴包,不同的發行版各不相同,隨便 Google 就能找到:
$ apt-get install libcairo2-dev libx11-dev libxpm-dev libxt-dev\ python3-dev ruby-devlibperl-dev git
接下去很多文章告訴你這樣配置:
$ git clone https://github.com/vim/vim.git $ cd vim $ ./configure --with-features=huge \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-pythoninterp=yes \ --with-python-config-dir=/usr/lib/python2.7/config \ --enable-python3interp=yes \ --with-python3-config-dir=/usr/lib/python3.5/config \ --enable-perlinterp=yes \ --prefix=/opt/vim8
首先需要注意的是 Vim 不能同時支援 Python2 和 Python3,所以 —enable-pythoninterp=yes 引數要去除。
—enable-python3interp 引數表示啟用 Python3 解析器。
最重要的就是 —with-python3-config-dir 引數,那麼 Python3 的配置檔案在哪兒呢?可以輸入下列命令找到:
$ /usr/local/bin/python3-config --configdir /usr/local/lib/python3.7/config-3.7m-x86_64-linux-gnu
其實高版本的 Vim 比較智慧,根本無需 —with-python3-config-dir 引數:
$ ./configure --help | grep python --enable-pythoninterp=OPTSInclude Python interpreter. default=no OPTS=no/yes/dynamic --enable-python3interp=OPTSInclude Python3 interpreter. default=no OPTS=no/yes/dynamic --with-python-command=NAMEname of the Python 2 command (default: python2 or python) --with-python-config-dir=PATHPython's config directory (deprecated) --with-python3-command=NAMEname of the Python 3 command (default: python3 or python) --with-python3-config-dir=PATHPython's config directory (deprecated)
可以看出 —with-python3-config-dir 引數已經廢棄了,所以只要 —enable-python3interp 引數就可以,安裝指令碼能夠自行找到 —with-python-config-dir。
最後,我使用下令指令碼完成安裝:
$ git clone https://github.com/vim/vim.git $ cd vim $ ./configure --with-features=huge \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-python3interp=yes \ --enable-perlinterp=yes \ --prefix=/opt/vim8
執行:
$ ln -s /opt/vim8/bin/vim /sbin/vim8
這樣輸入 vim8 命令,就能開啟 Vim 8.1。
在本次折騰過程中,我開始沒有找到 Python3.7 python3-config,所以用的是 Python3.4,可始終記不起來,自己安裝過 Python3.4,實際上 Python3.4 是 Ubuntu 預設的最小化安裝,輸入下列命令就會知道:
$ dpkg-query -L python3.4 python3-minimal/trusty,trusty,now 3.4.0-0ubuntu2 amd64 [installed] python3.4-minimal/now 3.4.3-1ubuntu1~14.04.5 amd64 [installed,upgradable to: 3.4.3-1ubuntu1~14.04.7]
從中可以看出,我從 ubuntu 14 升級到 ubuntu 16,預設使用 python3.4-minimal 包安裝的,為了讓作業系統保持乾淨,我將 Python3.4 刪除了:
$ apt-get purge --auto-remove python3.4
另外一個問題是出現 PyThread_start_new_thread 報錯(配置 python3.4),即使執行make clean
重新安裝也不行,最後全部刪除再 git clone 後解決(配置 python3.7)。
安裝完成後,開啟檔案發現二個問題,一個就是 Vim 配色全沒了,另外一個就是 Backspace 鍵失效,最後編輯 ~/.vimrc 解決:
syntax on set backspace=indent,eol,start
歡迎關注我的公眾號(ID:yudadanwx,虞大膽的嘰嘰喳喳),一直在用心寫。