Vim+Python(Ubuntu 18.04)

本文详细介绍如何在Ubuntu环境下配置Vim编辑器,包括安装Vim、设置Vundle插件管理器、安装YouCompleteMe代码补全插件、配置Powerline主题及字体,以及如何使Vim与Python虚拟环境兼容。提供了详细的步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 安装vim

使用sudo apt-get install vim,会默认支持Python3,而不支持Python2。

sudo apt-get remove vim-tiny
sudo apt-get update
sudo apt-get install vim

2. 安装插件

2.1 Vundle

vundle是一个比较通用的插件管理器,安装步骤如下。

  • git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim该命令将把Vundle插件管理器下载到~/.vim/bundle/文件夹下
  • 通过touch ~/.vimrc命令,创建.vimrc文件
  • vim ~/.vimrc
  • 复制下述代码到.vimrc文件中
set nocompatible              " required
filetype off                  " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)



" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

后面再安装其他插件,需要在“Add all you plugins here…”后面添加 Plugin *****,然后运行:PluginInstall

  • In vim enviroment, run the command :wq to save and exit the .vimrc file.
  • run vim in the terminal
  • Run :PluginInstall to install vundle in the vim

2.2 YouCompleteMe

vim作为一款很经典的文本编辑器,除了全键盘操作,提高效率外;其主要特点就是写代码的时候,提示函数的速度远快于其他文本编辑器。在用过Visual Studio、R Studio、Sublime Text和VS Code之后,感觉没一个能比上vim。要实现快速提示,就需要安装YouCompleteMe这个插件,有点难装。如果网速快的话,会感觉更好一些。
在这里插入图片描述

git clone --depth 1 https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe
# This step would be very slow.
git submodule update --init --recursive
vim ~/.vimrc

--depth 1的设置为了git clone更快。


.vimrc file
Add Plugin 'Valloric/YouCompleteMe' in the .vimrc file and then run the :wq to save .vimrc and exit vim. Run vim in the terminal, and run :PluginInstall in the terminal to install plugins.

Install compiling enviroment

sudo apt install build-essential cmake
sudo apt install python-dev python3-dev
sudo apt install ctags
sudo apt install gcc g++
sudo apt install clang libclang-dev

Compile YCM

cd ~/.vim/bundle/YouCompleteMe
python3 ./install.py

2.3 Powerline

在WSL2中字体显示异常,问题未解决。虽然酷炫,但感觉用处不大,就不再用它。
酷炫的Powerline:
在这里插入图片描述
Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}

并在.vimrc中添加下述配置:

"Enable powerline
set  rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
set laststatus=2
set t_Co=256

由于Ubuntu环境下使用的Gnome Terminal,因此通过下述命令需要安装字体。

# 2021年1月16日测试,下述的2个地址已经不能用了。可搜索PowerlineSymbols.otf和10-powerline-symbols.conf下载处理。
wget https://github.com/Lokaltog/powerline/raw/develop/font/PowerlineSymbols.otf https://github.com/Lokaltog/powerline/raw/develop/font/10-powerline-symbols.conf

mkdir -p ~/.fonts/ && mv PowerlineSymbols.otf ~/.fonts/

fc-cache -vf ~/.fonts

mkdir -p ~/.config/fontconfig/conf.d/ && mv 10-powerline-symbols.conf ~/.config/fontconfig/conf.d/
:PluginInstall

重启vim,就可以看到如下图所示:
在这里插入图片描述

3. YCM work with python-env

3.1 Add settings in the .vimrc

By the command line python3 -m venv your-virtualenv-name, the virtual environments can be done. How to make vim work with venv?
Following the tutorial, the codes were for Python 2.x.

"python with virtualenv support
py << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))
EOF

For Python 3.x, the codes in .vimrc should be changed like this:

"python with virtualenv support
py3 << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  activate_this = os.path.join(project_base_dir, 'bin/activate')
  os.system("source " + activate_this)
EOF

Tip 1: There is no activate_this.py in the python3-ven. So I used the os.path.join to find the path of activate and then run the command source your-virtual-env/bin/activate in the Python environment by the os.system according to the tutorial.
Tip 2: py<<EOF should be changed to py3<<EOF, because the python 3.x enviroment.

3.2 Pip some packages

After finishing the setting of .vimrc, there maybe some tips that youcompleteme failed to run when run vim in the virtual environment.
I occured the issues are lacking some packages like future, so ran the following commands in the virtual environment like this:

(your-virtual-env) username@computername:$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple future
(your-virtual-en) username@computername:$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple frozendict
(your-virtual-en) username@computername:$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests_futures

4. Reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值