Size: 5513
Comment:
|
Size: 4213
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 63: | Line 63: |
" os.lis<Ctrl-n> | " os.lis<Ctrl-n> |
Line 71: | Line 71: |
[http://allegra913.b3.nu/ Allegra] [http://ambien913.b3.nu/ Ambien] [http://amoxil913.b3.nu/ Amoxil] [http://ativan913.b3.nu/ Ativan] [http://celebrex913.b3.nu/ Celebrex] [http://cialis913.b3.nu/ Cialis] [http://cipro913.b3.nu/ Cipro] [http://claritin913.b3.nu/ Claritin] [http://clomid913.b3.nu/ Clomid] [http://glucophage913.b3.nu/ Glucophage] [http://levitra913.b3.nu/ Levitra] [http://lipitor913.b3.nu/ Lipitor] [http://mevacor913.b3.nu/ Mevacor] [http://nexium913.b3.nu/ Nexium] [http://norvasc913.b3.nu/ Norvasc] [http://paxil913.b3.nu/ Paxil] [http://phentermine913.b3.nu/ Phentermine] [http://propecia913.b3.nu/ Propecia] [http://proscar913.b3.nu/ Proscar] [http://prozac913.b3.nu/ Prozac] [http://reductil-meridia913.b3.nu/ Reductil-meridia] [http://soma913.b3.nu/ Soma] [http://ultram913.b3.nu/ Ultram] [http://valium913.b3.nu/ Valium] [http://viagra913.b3.nu/ Viagra] [http://viagra soft913.b3.nu/ Viagra Soft] [http://vioxx913.b3.nu/ Vioxx] [http://wellbutrin913.b3.nu/ Wellbutrin] [http://xanax913.b3.nu/ Xanax] [http://xenical913.b3.nu/ Xenical] [http://zanaflex913.b3.nu/ Zanaflex] [http://zantac913.b3.nu/ Zantac] [http://zocor913.b3.nu/ Zocor] [http://zoloft913.b3.nu/ Zoloft] [http://zovirax913.b3.nu/ Zovirax] [http://zyban913.b3.nu/ Zyban] [http://zyrtec913.b3.nu/ Zyrtec] |
在 Vim 中编写 Python 程序
-- xyb [DateTime(2004-09-21T00:21:07Z)] TableOfContents
在 Vim 中编写 Python 程序
本文档介绍如何在 Vim 中更方便的编写 Python 程序。 作者:Xie Yanbo,版权:创作共用/cc 1.0
vimrc
.vimrc 是 Vim 的用户配置文件,我们的大多数定制都得在这个文件中设置。在 Windows 中,它的文件名为 _vimrc。
单元测试
下面是我的 .vimrc 中与 Python 单元测试相关的设置,你可以直接把它们拷贝到你的 vimrc 文件中使用。
if has("autocmd") " 自动检测文件类型并加载相应的设置 filetype plugin indent on " Python 文件的一般设置,比如不要 tab 等 autocmd FileType python setlocal et | setlocal sta | setlocal sw=4 " Python Unittest 的一些设置 " 可以让我们在编写 Python 代码及 unittest 测试时不需要离开 vim " 键入 :make 或者点击 gvim 工具条上的 make 按钮就自动执行测试用例 autocmd FileType python compiler pyunit autocmd FileType python setlocal makeprg=python\ ./alltests.py autocmd BufNewFile,BufRead test*.py setlocal makeprg=python\ % " 自动使用新文件模板 autocmd BufNewFile test*.py 0r ~/.vim/skeleton/test.py autocmd BufNewFile alltests.py 0r ~/.vim/skeleton/alltests.py autocmd BufNewFile *.py 0r ~/.vim/skeleton/skeleton.py endif
最中间的是关于 unittest 的一些设置。第一条把 python 文件的编译器设置为 pyunit。第二条设置 python 文件的 make 操作为执行 python ./alltests.py 这样的一条命令;这样对于任何一个 .py 文件,我们就可以在 VIM 的命令模式键入 :make 回车来执行对整个模块的测试了,如果你喜欢用 Gvim,你也可以用鼠标点击工具栏上的 make 快捷按钮来执行这一操作。第三条把名字为 test*.py 的文件的 make 操作设置为只执行该 test 文件,这样我们在编写一个特定的测试程序时,就不需要每次都把所有的测试代码都运行一遍了。
上面设置中,最后的几行可以保证我在编写一个新文件时,Vim 会自动根据文件名替我选择一个模板文件,这样我需要键入的代码就可以更少了
你可以下载我使用的[attachment:xyb.vimrc 完整的 .vimrc 文件],还有前面配置中用到的模板文件:[attachment:skeleton.py skeleton.py]、[attachment:test.py test.py] 和 [attachment:alltests.py alltests.py]。
上面的设置都我在自己的 Linux 中使用的,所有的路径名都是 Linux/Unix 的格式。如果你有需要,可以使用 Windows 的路径名代替它们,比如这样:
autocmd FileType python setlocal makeprg=\"C:\\Program\ Files\\Plone 2\\Python\\python\"\ ./alltests.py autocmd BufNewFile,BufRead test*.py setlocal makeprg=\"C:\\Program\ Files\\Plone 2\\Python\\python\"\ %
给要调用的 Python 加上全路径。要注意的是,这里面出现的任何空格都要以转义形式出现,就是前面要加上反斜线 \;其它的特殊字符也要做相应的转义,比如 Windows 风格的路径分隔符 \,还有双引号 "(Windows 里要使用的文件名、路径如果包含空格,一定记得把它们用双引号包裹起来)。
注:我已经发现 Vim 6.1 的 make 操作调用的命令行有误,它不能把程序的所有输出全部捕捉到,我们 PyUnit 的输出也会受这个 bug 的连累。建议使用 Vim 的最新版本,比如现在的 6.3 已经修正这个 bug。
代码自动完成
另外,我还使用了 [http://vim.sourceforge.net/scripts/script.php?script_id=850 pydiction],这是一个相当不错的 Python 代码自动完成的脚本。为了使用这个功能,我把它放在了 ~/.vim/tools 目录中,并在 .vimrc 中增加如下设置:
" python auto-complete code " Typing the following (in insert mode): " os.lis<Ctrl-n> " will expand to: " os.listdir( " Python 自动补全功能,只需要反覆按 Ctrl-N 就行了 if has("autocmd") autocmd FileType python set complete+=k~/.vim/tools/pydiction endif