OS:CentOS Linux release 7.6.1810 (Core)
python版本:Python 3.9.6
需求:配置vim使能支持python程序开发的类似IDE环境,实现代码不全、语法高亮等功能。
1.检查vim版本,如果没有“+python3”关键字,则需要升级vim
# 可见vim版本为7.4
[root@drp-monitor-20210426165633-mojh ~]# vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 15 2020 16:44:08)
Included patches: 1-207, 209-629
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
……
+cryptv +linebreak +python/dyn +viminfo
+cscope +lispindent -python3 +vreplace
……
[root@drp-monitor-20210426165633-mojh ~]# rpm -qa | grep vim
vim-filesystem-7.4.160-5.el7.x86_64
vim-minimal-7.4.160-5.el7.x86_64
vim-enhanced-7.4.629-8.el7_9.x86_64
vim-common-7.4.629-8.el7_9.x86_64
cd /home/service/packages/vim
mkdir vim8.2
cd vim8.2
git clone git://github.com/vim/vim.git
cd vim/src
#make clean仅仅是清除之前编译的可执行文件及配置文件
make clean
./configure --prefix=/usr/local/vim/ --with-features=huge --enable-python3interp --enable-luainterp --enable-perlinterp --enable-multibyte --enable-cscope --with-python-config-dir=/usr/local/lib/python3.9/
make
# make distclean要清除所有生成的文件
make distclean #如果make出错时执行,调整后再重新make。
make install
#删除老vim版本,如下是RPM安装的删除方式。如果是编译安装的直接删除安装目录即可。如果是yum安装的,则可以使用yum remove来删除老版本
# rpm -e vim-enhanced-2:7.4.629-8.el7_9.x86_64
# rpm -e vim-common-2:7.4.629-8.el7_9.x86_64
# rpm -e vim-filesystem-7.4.160-5.el7.x86_64
#最后在创建一个软连接
ln -s /usr/local/vim/bin/vim /usr/bin/vim
# 检查vim版本
[root@drp-monitor-20210426165633-mojh bin]# vim --version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Aug 24 2021 19:05:47)
Included patches: 1-3370
Compiled by root@drp-monitor-20210426165633-mojh
Huge version without GUI. Features included (+) or not (-):
……
+cmdline_hist +langmap +python/dyn +visual
+cmdline_info +libcall +python3/dyn +visualextra
……
#configure参数说明:
–with-features=huge:支持最大特性
–enable-rubyinterp:打开对 ruby 编写的插件的支持
–enable-pythoninterp:打开对 python 编写的插件的支持
–enable-python3interp:打开对 python3 编写的插件的支持
–enable-luainterp:打开对 lua 编写的插件的支持
–enable-perlinterp:打开对 perl 编写的插件的支持
–enable-multibyte:打开多字节支持,可以在 Vim 中输入中文
–enable-cscope:打开对 cscope 的支持
–with-python-config-dir=/usr/lib64/python2.7/ 指定 python 路径
–with-python-config-dir=/usr/local/lib/python3.9/ 指定 python3 路径
–prefix=/usr/local/vim:指定VIM将要安装到的路径 (自行创建)
#python3/dyn 含义如下,表示同时支持python2和python3
可使用如下方法:
#如下/usr/lib64/python2.7 就是我们需要的python2路径
[root@drp-monitor-20210426165633-mojh bin]# python
Python 2.7.5 (default, Apr 2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
#如下/usr/local/lib/python3.9 就是我们需要的python3路径
[root@drp-monitor-20210426165633-mojh bin]# python
Python 3.9.6 (default, Jul 21 2021, 17:15:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/root/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages']
Vundle是Vim bundle的缩写,是一个Vim插件管理器。git hub地址为:https://github.com/VundleVim/Vundle.vim
# mkdir /usr/local/vim/share/vim/bundle
cd /usr/local/vim/share/vim/bundle
# 下载vundle插件管理器到/usr/local/vim/share/vim/bundle/vundle.vim目录下
git clone git://github.com/VundleVim/Vundle.vim.git /usr/local/vim/share/vim/bundle/vundle.vim
# 拷贝一份vimrc样本配置文件
cp /usr/local/vim/share/vim/vim82/vimrc_example.vim /usr/local/vim/share/vim/vimrc
vim /usr/local/vim/share/vim/vimrc
#加入如下内容:
"去除VI一致性,必须
set nocompatible
"必须
filetype off
"设置Vundle的运行路径
set rtp+=/usr/local/vim/share/vim/bundle/vundle.vim
"设置插件的安装路径,vundle插件起始标志,
"#begin …… #end 之间就是要安装的插件,插件文件放在rtp+对应路径下面
call vundle#begin('/usr/local/vim/share/vim/bundle')
"让vundle管理插件版本
Plugin 'VundleVim/Vundle.vim'
"你的所有插件需要在下面这行之前
call vundle#end()
"加载vim自带和插件相应的语法和文件类型相关脚本
filetype plugin indent on
# 1)vim命令进入vim程序
# vim
# 2)命令模式下执行:PluginList列出你所有的插件
:PluginList
# 3) 命令模式下执行:PluginInstall安装插件
:PluginInstall
#删除插件只需要在vimrc配置文件中注释掉对应的插件,在vim中用:PluginClean命令就会清理掉注释掉的插件
注意:
删除插件从磁盘删除对应的插件相关文件
功能:erdtree是一个在vim中新窗口显示的文件浏览器
cd /usr/local/vim/share/vim/bundle
git clone git://github.com/preservim/nerdtree.git
vim ../vimrc
#编辑vimrc配置文件,在#begin……#end之间,添加如下内容:
"添加nerdtree插件
Plugin 'preservim/nerdtree'
"设置按F2启动NerdTree
map <F2> :NERDTreeToggle<CR>
"隐藏目录树中的.pyc文件
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree
在PluginList,PluginInstall来安装该插件
代码自动补全插件,支持多种语言(如java、python、C等),github详细介绍
cd /usr/local/vim/share/vim/bundle
git clone git://github.com/ycm-core/YouCompleteMe.git
cd /usr/local/vim/share/vim/bundle/YouCompleteMe
git submodule update --init --recursive #可能报错见“报错1”
python install.py --clang-completer #可能报错见“报错2”
#Q1: 报错1--git submodule报错
[root@drp-monitor-20210426165633-mojh YouCompleteMe]# git submodule update --init --recursive
Cloning into 'third_party/bottle'...
fatal: unable to access 'https://github.com/defnull/bottle/': Failed connect to 127.0.0.1:1080; Connection refused
Clone of 'https://github.com/defnull/bottle' into submodule path 'third_party/bottle' failed
Failed to recurse into submodule path 'third_party/ycmd'
#A1: 需要修改访问github的协议
1) 需要将报错的模块对应的https:// 协议改成 git://
cd /usr/local/vim/share/vim/bundle/YouCompleteMe
如上报错中提示‘https://github.com/defnull/bottle’ 找不到,
grep -ri "https://github.com/defnull/bottle" 查看哪些文件配置了这个,如下:
sed -i s#https://#git://# third_party/ycmd/.gitmodules
sed -i s#https://#git://# .git/modules/third_party/ycmd/config
注意:非github地址,可以可能需要保持使用https://协议
#补充:
查看git配置:
git config --global -l
配置代理:
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy http://127.0.0.1:1080
取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
#Q2:报错2--正式安装YouCompleteMe插件时报错
[root@drp-monitor-20210426165633-mojh YouCompleteMe]# python install.py --clang-completer
File /usr/local/vim/share/vim/bundle/YouCompleteMe/third_party/ycmd/build.py does not exist; you probably forgot to run:
git submodule update --init --recursive
#A2:
修改.gitmodules 将“https://" 改成 ”git://" # 因为可能https协议可能无法访问。
git submodule sync
git submodule update --init --recursive
#Q3:报错3--缺少动态链接库
#A3:下载python源码包,重新编译,并复制到指定目录
1)下载(www.python.org)
2)./configure --prefix=/usr/local --enable-shared CFLAGS=-fPIC
3)make
4)makeinstall
5)复制到指定目录
cp libpython3.6m.so.1.0 /usr/local/lib64/
cp libpython3.6m.so.1.0 /usr/lib/
cp libpython3.6m.so.1.0 /usr/lib64/
#Q4:报错4:缺少devtoolset-8
-- NOTE: You appear to be on CentOS. In order to use this application, you require a more modern compiler than the default compiler on this platform. Please install the devtoolset-8 or greater. For example, see this link: https://www.softwarecollections.org/en/scls/rhscl/devtoolset-8/
CMake Error at CMakeLists.txt:232 (message):
Your C++ compiler does NOT fully support C++17.
-- Configuring incomplete, errors occurred!
See also "/tmp/ycm_build_s84e_jbz/CMakeFiles/CMakeOutput.log".
ERROR: the build failed.
#A4:参考如下:
1). Install a package with repository for your system:
# On CentOS, install package centos-release-scl available in CentOS repository:
$ sudo yum install centos-release-scl
# On RHEL, enable RHSCL repository for you system:
$ sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
2). Install the collection:
$ sudo yum install devtoolset-8
3). Start using software collections:
$ scl enable devtoolset-8 bash
#Q5:报错5:
[root@drp-monitor-20210426165633-mojh vim]# vim
YouCompleteMe unavailable: unable to load Python.
Press ENTER or type command to continue
#A5:原因:vim8不能同时python和python3,需要关闭python2的配置开关--enable-pythoninterp
#重新编译vim
cd vim源码包
make uninstall
./configure --prefix=/usr/local/vim/ --with-features=huge --enable-python3interp --enable-luainterp --enable-perlinterp --enable-multibyte --enable-cscope --with-python-config-dir=/usr/local/lib/python3.9
make
make install
#查看日志
:YcmToggleLogs ycmd_49353_stderr_hfxb354_.log
#重启ycmd server
:YcmRestartServer
#ycmd命令自动补全
:Ycm(按tab键选择,按enter确定)