HANDBOOK
Vim与Python真乃天作之合
Vim与Python真乃天作之合 我注意到,有人在realpython.com宣扬Sublime Text 3。作为公司的资深开发人员(呃,也就是老古董),我觉得有义务介绍一个真正的Python开发环境给大家——我要推荐的当然就是Vim了。不错,Vim编辑器无处不在,速度快,从来不会崩溃。并且,它能做任何事情! 不过,不利之处也有,就是Vim配置起来很让人头疼。但是,别担心,本文将告诉你如何配置一个强大的Vim环境,专门用于天天捣鼓Python开发。 下面是最终效果预览。 如果想充分地利用好本文,你应该对如何使用Vim和它的命令模式至少有一个基本的了解。如果你是初学者,你可以通过vim-adventure或者openvim网站学习。在继续阅读本文之前,请花点时间浏览那两个网站的内容。 安装 因为许多Unix衍生系统已经预装了Vim,我们首先要确认编辑器是否成功安装:
1 |
vim --version |
如果已经安装了,你应该看到类似下面的文字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="x">VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Nov 5 2014 21:00:28)</span> <span class="x">Compiled by root@apple.com</span> <span class="x">Normal version without GUI. Features included (+) or not (-):</span> <span class="x">-arabic +autocmd -balloon_eval -browse +builtin_terms +byte_offset +cindent</span> <span class="x">-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments</span> <span class="x">-conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs</span> <span class="x">-dnd -ebcdic -emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path</span> <span class="x">+find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv</span> <span class="x">+insert_expand +jumplist -keymap -langmap +libcall +linebreak +lispindent</span> <span class="x">+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape</span> <span class="x">-mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm -mouse_sysmouse</span> <span class="x">+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype</span> <span class="x">+path_extra -perl +persistent_undo +postscript +printer -profile +python/dyn</span> <span class="x">-python3 +quickfix +reltime -rightleft +ruby/dyn +scrollbind +signs</span> <span class="x">+smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary</span> <span class="x">+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title</span> <span class="x"> -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo</span> <span class="x">+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp</span> <span class="x"> -xterm_clipboard -xterm_save</span> <span class="x"> system vimrc file: "</span><span class="p">$</span><span class="nv">VIM</span><span class="x">/vimrc"</span> <span class="x"> user vimrc file: "</span><span class="p">$</span><span class="nv">HOME</span><span class="x">/.vimrc"</span> <span class="x"> user exrc file: "</span><span class="p">$</span><span class="nv">HOME</span><span class="x">/.exrc"</span> <span class="x"> fall-back for </span><span class="p">$</span><span class="nv">VIM</span><span class="x">: "/usr/share/vim"</span> <span class="x">Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe</span> <span class="x">Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses</span> |
在这一步,你要确保已经满足以下两点要求: Vim编辑版本应该大于7.3。 支持Python语言。在所选编辑器的功能中,确保你看到了+python。 如果满足上述要求,接下来可以安装Vim扩展了。如果不满足,则需要安装/升级。 OS X 如果没有Homebrew,建议马上安装,并运行:
1 2 |
brew update brew install vim |
Unix衍生系统 Debian或Ubuntu系统,可以使用下面的代码:
1 2 3 |
sudo apt-get remove vim-tiny apt-get update apt-get install vim |
如果是其他版本的Linux系统,请查阅相应版本包管理器的文档。不清楚的话,可以先阅读这篇文章:安装Vim Windows Windows系统下安装Vim有很多种方法。请查阅官方文档。 验证安装 确保你已经安装了7.3版本以上、支持Python的Vim编辑器。你可以再次运行vim –version进行确认。如果你想知道Vim中使用的Python版本,你可以在编辑器中运行:python import sys; print(sys.version)。
1 2 |
2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] |
这行命令会输出你的编辑器当前的Python版本。如果报错,那么你的编辑器就不支持Python语言,需要重装或重新编译。 Vim编辑器安装完成后,我们来看看如何将其设置为Python开发的强大环境。 Vim扩展 Vim本身能够满足开发人员的很多需求,但是它的可扩展性也极强,并且已经有一些杀手级的扩展,可以让Vim拥有“现代”集成开发环境的特性。所以,你所需要的第一件东西就是一个好用的扩展管理器。 Vim的扩展通常也被成为bundle或插件。 Vundle Vim有多个扩展管理器,但是我们强烈推荐Vundle。你可以把它想象成Vim的pip。有了Vundle,安装和更新包这种事情不费吹灰之力。 我们现在来安装Vundle:
1 |
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim |
该命令将下载Vundle插件管理器,并将它放置在你的Vim编辑器bundles文件夹中。现在,你可以通过.vimrc配置文件来管理所有扩展了。 将配置文件添加到你的用户的home文件夹中:
1 |
touch ~/.vimrc |
接下来,把下来的Vundle配置添加到配置文件的顶部:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 |
这样,你就完成了使用Vundle前的设置。之后,你就可以在配置文件中添加希望安装的插件,然后打开Vim编辑器,运行下面的命令:
1 |
:PluginInstall |
这个命令告诉Vundle施展它的魔法——自动下载所有的插件,并为你进行安装和更新。 Read more…