以前使用IDE编程,模板是最基本的功能,如今切换到vim,用惯了模板的我,对于每次写代码都来上python
# -*- coding: utf-8 -*- # # 日期 做者 # # License
这么一段,感受有点恐怖,Google了一下,vim居然能够实现比IDE更强大的功能,真是不得不佩服vim的强大!git
因为不一样人的vim配置不同,安装方法也不尽相同,请参考官方的安装方法来安装。
对于使用spf13-vim的同窗,在主目录下的.vimrc.bundles.local添加一行:github
Bundle 'git@github.com:aperezdc/vim-template.git' # 不知道"Bundle 'vim-template'"为何老要我输入用户名密码, # 输入以后也安装不成功,知道的同窗能够留言告知我。
而后在shell里面运行:shell
vim +BundleInstall
或者打开vim,运行:编程
:BundleInstall
都行。vim
若是你对自带的模板没有不少意见,你甚至什么都不用配置,你用vim打开一个新的python脚本,vim-template会自动检测
文件后缀,而后本身根据模板填充文件,一切都那么理所固然。你甚至能够打开没有后缀的文件,而后运行:Template <pattern>(好比:Template *.py)在文件
开头插入模板,:TemplateHere <pattern>(好比: TemplateHere *.py)在光标所在的位置插入模板。函数
安装好vim-template以后,能够根据本身的须要来配置:学习
vim-template能够自定义模板变量, 在主目录的.vimrc(若是你使用spf13-vim的话就修改.vimrc.local)中添加:插件
let g:templates_user_variables = [['EMAIL', 'GetEmail'],] function GetEamil() return 'pylego@hotmail.com' endfunction
注意:上面都是VimScript的语法,有兴趣的能够学习下。code
这样你就能够在模板里面写%EMAIL%,而后模板引擎会自动调用GetEmail函数,将返回值代替之。
有时候你想根据本身的须要定义模板,在vim-template里面是很容易实现的
# 在.vimrc(若是你使用spf13-vim的话就是.vimrc.local) let g:templates_directory = '/home/pylego/.vim/templates'
文件的命名模式是"=template=<pattern>", 好比我想让每一个以py结尾的文件都以这个模板开始就能够这样:
建立一个名为"=template=.py"的文件,向文件里面写入模板。
注意这个"=template="前缀是能够改动的,具体不在本文讨论范围内。
spf13-vim
.vimrc.local
" settings for vim-template bundle let g:templates_directory = ['/home/pylego/.vim/templates',] let g:templates_user_variables = [['EMAIL', 'GetMail'], ['FULLPATH', 'GetFullPath']] function GetMail() return 'pylego@hotmail.com' endfunction function GetFullPath() return expand('%:p') endfunction " vim-template settings end
/home/pylego/.vim/templates/=template=.py
# -*- coding: utf-8 -*- # # Copyright © PyleGo. # # %DATE% %TIME% <%EMAIL%> # # Distributed under terms of the %LICENSE% license.