使用vim打造自己的python编辑器

基础配置
vim的配置是在用户主目录下的 ~/.vimrc 文件中完成的,如果没有的话,需要自己新建一下:

1
2
cd ~
touch .vimrc
首先做些简单的配置:

1
2
3
4
5
6
7
8
9
10
set nocompatible "关闭与vi的兼容模式
set number "显示行号
set nowrap "不自动折行
set showmatch "显示匹配的括号
set scrolloff=3 "距离顶部和底部3行"
set encoding=utf-8 "编码
set fenc=utf-8 "编码
set mouse=a "启用鼠标
set hlsearch "搜索高亮
syntax on "语法高亮
为py文件添加下支持pep8风格的配置:

1
2
3
4
5
6
7
8
au BufNewFile,BufRead *.py
set tabstop=4 "tab宽度
set softtabstop=4
set shiftwidth=4
set textwidth=79 "行最大宽度
set expandtab "tab替换为空格键
set autoindent "自动缩进
set fileformat=unix "保存文件格式
分割窗口
vim在编辑的时候就可以打开多个文件:

:vs 或者 :vsplit 将当前窗口竖直分割,并在上面新窗口中显示当前文件

:vs filename 将当前窗口竖直分割,新文件在新窗口中显示

:sp 或者:sv或者:split 将当前窗口水平分割,并在左边新窗口中显示当前文件

:sp filename 将当前窗口竖直分割,新文件在左边新窗口中显示

:new 新建文件并竖直分割

:vnew 新建文件并水平分割

如果想让新窗口在右边或者下方打开,添加配置:

1
2
set splitbelow
set splitright
在窗口之间切换可以用鼠标,如果不想用鼠标,切换按键如下:

Ctrl-w-j 切换到下方的分割窗口
Ctrl-w-k 切换到上方的分割窗口
Ctrl-w-l 切换到右侧的分割窗口
Ctrl-w-h 切换到左侧的分割窗口
觉得三个按键多的话可以设置快捷键:

1
2
3
4
nnoremap
nnoremap
nnoremap
nnoremap
这样就不用按w键了。

代码折叠
当代码行数很多的时候,代码折叠是很必须的:

1
2
set foldmethod=indent
set foldlevel=99
使用zc按键来创建折叠,使用za来打开或者关闭折叠。

za经常会误输入,可以用空格键来替代za:

1
nnoremap za
一键执行python代码
如果想直接在vim中执行python代码,可以添加(来自https://www.zhihu.com/question/20271508):

1
2
3
4
5
6
7
map :call RunPython()
func! RunPython()
exec "W"
if &filetype == 'python'
exec "!time python2.7 %"
endif
endfunc
这样,按F5键python代码就可以自动执行了

插件
vim插件中最主要的就是vundle了,vundle用来管理vim的其它插件

Vundle
Vundle 是 Vim bundle 的简称,使用git来管理vim插件,有了它,安装其它插件就方便很多。

项目地址https://github.com/VundleVim/Vundle.vim。

首先下载源码:

1
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
如果~/.vim/bundle目录不存在,则新建目录:

1
2
3
4
cd ~
mkdir .vim
cd .vim
mkdir bundle
然后将下列配置放在.vimrc文件的开头:

1
2
3
4
5
6
7
8
9
10
11
12
13
set nocompatible " be iMproved, required
filetype off " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
如果想下载某个插件,比如自动缩进indentpython.vim插件,需要将

1
Plugin 'vim-scripts/indentpython.vim'
置于call vundle#begin()和call vundle#end()之间,保存配置后在vim中执行

1
:PluginInstall
即可以自动下载indentpython.vim插件了。

bundle可以管理下载几种不同的插件,方式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
github上的插件
Plugin 'tpope/vim-fugitive'
来自于http://vim-scripts.org/vim/scripts.html的插件
Plugin 'L9'
非github上的git插件
Plugin 'git://git.wincent.com/command-t.git'
本地插件
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
有旧插件的情况下,下载新的插件并重命名以避免冲突
Plugin 'ascenator/L9', {'name': 'newL9'}
下载方式除了在vim中运行:PluginInstall外,还可以在命令行中运行:

1
vim +PluginInstall +qall
其它常用的命令:

1
2
3
4
:PluginList - lists configured plugins
:PluginInstall - installs plugins; append ! to update or just :PluginUpdate
:PluginSearch foo - searches for foo; append ! to refresh local cache
:PluginClean - confirms removal of unused plugins; append ! to auto-approve removal
YouCompleteMe
非常好用的自动补全插件,就是比较重。

官网地址:http://valloric.github.io/YouCompleteMe/

github地址:https://github.com/Valloric/YouCompleteMe

YouCompleteMe安装后还需要手动编译,然后再在.vimrc中配置。

在ubuntu中使用,首先准备一些工具:

1
sudo apt-get install build-essential cmake
1
sudo apt-get install python-dev python3-dev
使用vundle安装:

1
Plugin 'Valloric/YouCompleteMe'
编译:

1
2
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
参数 --clang-completer是为了加上C系列语言的自动补全,也可以不加:

1
2
cd ~/.vim/bundle/YouCompleteMe
./install.py
耐心等待吧,要花很长时间...

复制一下默认配置文件到用户主目录:

1
cp third_party/ycmd/examples/.ycm_extra_conf.py ~/
YCM常用的一些选项,可根据个人喜好调整:

1
2
3
4
let g:ycm_min_num_of_chars_for_completion = 2 "开始补全的字符数"
let g:ycm_python_binary_path = 'python' "jedi模块所在python解释器路径"
let g:ycm_seed_identifiers_with_syntax = 1 "开启使用语言的一些关键字查询"
let g:ycm_autoclose_preview_window_after_completion=1 "补全后自动关闭预览窗口"
代码跳转:

1
nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration
开关YCM:

1
2
let g:ycm_auto_trigger = 0 "turn off
let g:ycm_auto_trigger = 1 "turn on
支持vim8的补全插件
YouCompleteMe实际上是使用jedi-vim来补全python代码的,如果觉得YCM实在太重,可以使用支持vim8的maralla/completor.vim来补全代码:

下载:

1
Plugin 'maralla/completor.vim'
下载jedi:

1
pip install jedi
配置:

1
let g:completor_python_binary = '/path/to/python/with/jedi/installed'
设置起来比YCM简单很多了。

自动缩进插件
写python代码,自动缩进是必须的,可以使用indentpython.vim插件:

1
Plugin 'vim-scripts/indentpython.vim'
语法检查
安装syntastic插件,每次保存文件时Vim都会检查代码的语法:

1
Plugin 'vim-syntastic/syntastic'
添加flake8代码风格检查:

Plugin 'nvie/vim-flake8'
运行F7就可以进行flake8检查了。

配色方案
solarized配色方案已经流行很久了,github地址https://github.com/altercation/vim-colors-solarized。

手动下载:

$ cd ~/.vim/bundle
$ git clone git://github.com/altercation/vim-colors-solarized.git
$ mv vim-colors-solarized ~/.vim/bundle/
或者vundle下载:

Plugin 'altercation/vim-colors-solarized'
solarized有dark和light两种配色,配置:

syntax enable
set background=light or dark
colorscheme solarized
也可以根据gui模式和终端模式进行切换:

if has('gui_running')
set background=light
else
set background=dark
endif
另外一种配色Zenburn方案:

Plugin 'jnurmine/Zenburn'
两种配色切换:

if has('gui_running')
set background=dark
colorscheme solarized
else
colorscheme Zenburn
endif
nerdtree
给vim添加一个树形目录,地址https://github.com/scrooloose/nerdtree。

下载:

Plugin 'scrooloose/nerdtree'
添加开关树形目录的快捷键:

map :NERDTreeToggle
Ctrl+n就可以开启目录了。

设置忽略.pyc文件:

let NERDTreeIgnore=['~$', '.pyc$', '.swp$']
为nerdtree添加git支持:

Plugin 'Xuyuanp/nerdtree-git-plugin'
如果你想用tab键:

Plugin 'jistr/vim-nerdtree-tabs'
vim-powerline
美化状态栏,可以显示当前的虚拟环境、Git分支、正在编辑的文件等信息。

Plugin 'Lokaltog/vim-powerline'

indentLine
缩进指示线,地址https://github.com/Yggdroot/indentLine。

安装:

Plugin 'Yggdroot/indentLine'
开关:

:IndentLinesToggle
python是靠代码缩进来判断代码块的,缩进指示线还是很方便的。

vim-autopep8
自动格式化工具,安装后运行:Autopep8就可以自动依照pep8的标准自动格式化代码。

地址https://github.com/Yggdroot/indentLine。

首先安装autopep8:

$ pip install autopep8

Plugin 'tell-k/vim-autopep8'
可以设置快捷键F8代替:Autopep8:

autocmd FileType python noremap :call Autopep8()
auto-pairs
自动补全括号和引号等,地址https://github.com/jiangmiao/auto-pairs。

Plugin 'jiangmiao/auto-pairs'
ctrlp.vim
搜索插件,在vim normal模式下,按下ctrl+p,然后输入你要寻找的文件就行了。

地址https://github.com/kien/ctrlp.vim。

Plugin 'kien/ctrlp.vim'
ag.vim
搜索引擎使用了the_silver_searcher

apt-get install silversearcher-ag
or
brew install the_silver_searcher
插件

Plugin 'rking/ag.vim'
使用

:Ag [options] {pattern} [{directory}]
vim-fugitive
git集成插件,可以在vim中运行git命令,https://github.com/tpope/vim-fugitive。

Plugin 'tpope/vim-fugitive'

原文地址:https://www.cnblogs.com/enumx/p/12333852.html