1. 程式人生 > >[Vim]新建python檔案自動新增python header

[Vim]新建python檔案自動新增python header

使用vim指令碼來實現的,使用了模板,幾行程式碼就能實現很實用。 ~/.vimrc 中的程式碼
"auto add pyhton header --start
autocmd BufNewFile *.py 0r ~/.vim/vim_template/vim_pyhton_header
autocmd BufNewFile *.py ks|call FileName()|'s
autocmd BufNewFile *.py ks|call CreatedTime()|'s

fun FileName()
	if line("$") > 10
		let l = 10  "這裡是字母L 不是數字1 
	else
		let l = line("$")
	endif 
	exe "1," . l . "g/File Name:.*/s/File Name:.*/File Name: " .expand("%")  
       "最前面是數字1,這裡的File Name: 要和模板中一致
endfun 

fun CreatedTime()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif 
	exe "1," . l . "g/Created Time:.*/s/Created Time:.*/Created Time: " .strftime("%Y-%m-%d %T") 
        "這裡Create Time: 要和模板中一致
endfun 
"auto add python header --end

模板程式碼 檔案為~/.vim/vim_template/vim_pyhton_header
#!/usr/bin/python
#-*- coding:utf-8 -*-
############################
#File Name:
#Author: orangleliu
#Mail: [email protected]
#Created Time:
############################

說明 1 模板路徑要一致 2 不要忘了保持 .vimrc中替換標籤名稱和模板中一致 例如  Create Time: 測試結果
lzz@ubuntu:~$ vim c.py
lzz@ubuntu:~$ cat c.py 
#!/usr/bin/python
#-*- coding:utf-8 -*-
############################
#File Name: c.py
#Author: orangleliu
#Mail: [email protected]
#Created Time: 2014-12-11 20:16:33
############################

其他語言也可以類似的程式設計,這樣就不用每次都在程式碼開頭寫各種標頭檔案啊,協議啊,作者,日期啥的了。