vim模板插件vim-template的使用
之前使用IDE編程,模板是最基本的功能,現在切換到vim,用慣了模板的我,對于每次寫代碼都來上
# -*- coding: utf-8 -*-
#
# 日期 作者
#
# License
這么一段,感覺有點恐怖,Google了一下,vim竟然可以實現比IDE更強大的功能,真是不得不佩服vim的強大!
安裝
由于不同人的vim配置不一樣,安裝方法也不盡相同,請參考官方的安裝方法來安裝。
對于使用spf13-vim的同學,在主目錄下的.vimrc.bundles.local添加一行:
Bundle 'git@github.com:aperezdc/vim-template.git'
# 不知道"Bundle 'vim-template'"為什么老要我輸入用戶名密碼,
# 輸入之后也安裝不成功,知道的同學可以留言告知我。
然后在shell里面運行:
vim +BundleInstall
或者打開vim,運行:
:BundleInstall
都行。
基本使用
如果你對自帶的模板沒有很多意見,你甚至什么都不用配置,你用vim打開一個新的python腳本,vim-template會自動檢測
文件后綴,然后自己根據模板填充文件,一切都那么理所當然。你甚至可以打開沒有后綴的文件,然后運行:Template <pattern>(比如:Template *.py)在文件
開頭插入模板,:TemplateHere <pattern>(比如: TemplateHere *.py)在光標所在的位置插入模板。
配置
安裝好vim-template之后,可以根據自己的需要來配置:
1. 自定義模板變量
vim-template可以自定義模板變量, 在主目錄的.vimrc(如果你使用spf13-vim的話就修改.vimrc.local)中添加:
let g:templates_user_variables = [['EMAIL', 'GetEmail'],]
function GetEamil()
return 'pylego@hotmail.com'
endfunction
注意:上面都是VimScript的語法,有興趣的可以學習下。
這樣你就可以在模板里面寫%EMAIL%,然后模板引擎會自動調用GetEmail函數,將返回值代替之。
2. 自定義模板
有時候你想根據自己的需要定義模板,在vim-template里面是很容易實現的
1. 修改g:templates_directory到你自定義模板所在的文件夾
# 在.vimrc(如果你使用spf13-vim的話就是.vimrc.local)
let g:templates_directory = '/home/pylego/.vim/templates'
2. 在/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.