Vim 作为一个经典的编辑器,如果配置合适,可以成为一个编辑 Python 脚本非常给力的工具。这篇文章主要目的是介绍如何打造一个强大的 Vim 编程环境。

第一部分:软件安装:

在终端中执行:

vim --version

会看到如下输出:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Oct 5 2017 04:42:50)
MacOS X (unix) version
Included patches: 1-1175
Compiled by travis@Traviss-Mac-592.local
Huge version with MacVim GUI. Features included (+) or not (-):
+acl +find_in_path -mouse_sysmouse -tag_any_white
+arabic +float +mouse_urxvt -tcl
+autocmd +folding +mouse_xterm +termguicolors
+balloon_eval -footer +multi_byte +terminal
+browse +fork() +multi_lang +terminfo
++builtin_terms +fullscreen -mzscheme +termresponse
+byte_offset -gettext +netbeans_intg +textobjects
+channel -hangul_input +num64 +timers
+cindent +iconv +odbeditor +title
+clientserver +insert_expand +packages +toolbar
+clipboard +job +path_extra +transparency
+cmdline_compl +jumplist +perl/dyn +user_commands
+cmdline_hist +keymap +persistent_undo +vertsplit
+cmdline_info +lambda +postscript +virtualedit
+comments +langmap +printer +visual
+conceal +libcall +profile +visualextra
+cryptv +linebreak +python/dyn +viminfo
+cscope +lispindent +python3/dyn +vreplace
+cursorbind +listcmds +quickfix +wildignore
+cursorshape +localmap +reltime +wildmenu
+dialog_con_gui +lua/dyn +rightleft +windows
+diff +menu +ruby/dyn +writebackup
+digraphs +mksession +scrollbind -X11
+dnd +modify_fname +signs -xfontset
-ebcdic +mouse +smartindent +xim
+emacs_tags +mouseshape +startuptime -xpm
+eval +mouse_dec +statusline -xsmp
+ex_extra -mouse_gpm -sun_workshop -xterm_clipboard
+extra_search -mouse_jsbterm +syntax -xterm_save
+farsi +mouse_netterm +tag_binary
+file_in_path +mouse_sgr +tag_old_static
...

这里要确认两件事:

Vim 的版本要求不低于 7.3

要求支持 python,确保列表中有 +python

不同系统下 Vim 的安装方式如下:

MacOS:

安装 Homebrew,然后在终端中执行:

brew update

brew install vim

*NIX:

在 Debian 或者 Ubuntu 上,执行:

sudo apt-get remove vim-tiny

sudo apt-get update

sudo apt-get install vim

Windows:

第二部分:配置文件

Vim 的配置文件是

~/.vimrc

你可以讲需要的配置加入配置文件中,重新启动 Vim 或者在 normal 环境中执行

:source %

便可加载配置文件

编辑 .vimrc 文件,可以执行:

vim ~/.vimrc

有用的配置内容有:

set nocompatible
syntax on
filetype plugin indent on
set ic
set hlsearch
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,GB2312,big5
set cursorline
set autoindent
set smartindent
set scrolloff=4
set showmatch
set nu

窗口移动:

Vim 的默认窗口移动方式是 Ctrl+w Ctrl+hjkl,但是这样太过复杂,对此进行重新映射:

nnoremap 
nnoremap 
nnoremap 
nnoremap

针对 Python 的特殊配置有:

let python_highlight_all=1
au Filetype python set tabstop=4
au Filetype python set softtabstop=4
au Filetype python set shiftwidth=4
au Filetype python set textwidth=79
au Filetype python set expandtab
au Filetype python set autoindent
au Filetype python set fileformat=unix
autocmd Filetype python set foldmethod=indent
autocmd Filetype python set foldlevel=99

自动执行:按一下 F5,自动执行代码:

map :call CompileRunGcc()
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %
exec "!time ./%
elseif &filetype == 'cpp'
exec "!g++ % -o %
exec "!time ./%
elseif &filetype == 'java'
exec "!javac %"
exec "!time java %
elseif &filetype == 'sh'
:!time bash %
elseif &filetype == 'python'
exec "!clear"
exec "!time python3 %"
elseif &filetype == 'html'
exec "!firefox % &"
elseif &filetype == 'go'
" exec "!go build %
exec "!time go run %"
elseif &filetype == 'mkd'
exec "!~/.vim/markdown.pl % > %.html &"
exec "!firefox %.html &"
endif
endfunc

第三部分:插件安装和管理工具

安装插件强烈建议使用插件管理工具,这里推荐的是 vundle

首先安装 vundle:

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

然后在 .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

关闭后重新打开 Vim,在 normal 模式下执行:

:PluginInstall

插件管理工具会自动安装配置文件中写到的插件。

在 .vimrc 中加入

Plugin 'Chiel92/vim-autoformat'

关闭后重新打开 Vim,在 normal 模式下执行下述代码即可:

Plugin 'Chiel92/vim-autoformat'

第四部分:推荐的插件

1、YouCompleteMe

作为 Python 开发环境,必须要有自动补全工具,这里强烈推荐 YouCompleteMe

vim写python慢 vim配置python开发环境_配置文件

建议按照官方说明一步一步进行安装,史上最难安装的vim插件并非名不副实

2、Autoformat

这个插件 vim-autoformat 能够自动的一键格式化代码

Before:

vim写python慢 vim配置python开发环境_vim_02

After:

vim写python慢 vim配置python开发环境_vim写python慢_03

安装方式:

这款插件需要已经安装有代码格式化工具,安装方式是在终端下执行:

pip install autopep8

推荐的配置是:

Plugin 'Chiel92/vim-autoformat'
nnoremap :Autoformat
let g:autoformat_autoindent = 0
let g:autoformat_retab = 0
let g:autoformat_remove_trailing_spaces = 0

每次在 normal 环境下按 F6 便可以格式化代码。

3、文件树

在 Vim中浏览文件夹:nerdtree

vim写python慢 vim配置python开发环境_python vim配置_04

推荐配置是:

Plugin 'https://github.com/scrooloose/nerdtree'

nnoremap :NERDTreeToggle

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

通过按 开关文件树。

4、强大的状态栏

这款插件提供了加强版的状态栏:vim-airline

vim写python慢 vim配置python开发环境_配置文件_05

推荐配置:

Plugin 'https://github.com/bling/vim-airline'

5、代码检查工具

syntastic 提供了自动代码检查功能,再也不用担心变量写错了。

vim写python慢 vim配置python开发环境_vim写python慢_06

安装方式:

这款插件需要已经安装好代码检测工具,Python 代码检查工具的安装方式是在终端下执行:

pip install flake8

推荐配置是:

Plugin 'scrooloose/syntastic'
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

尝试了 ale,确实很不错,它利用了 Vim8 的异步处理功能,用起来不会有 syntastic 的卡顿现象。

确保你的vim版本不低于8.0

安装方式:

和 syntastic 一样,它需要代码检测工具支持:

pip install flake8

推荐配置是:

Plugin 'w0rp/ale'
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
let g:ale_sign_column_always = 1
let g:airline#extensions#ale#enabled = 1