[Vim]新建python文件自动添加python header

时间:2023-03-09 22:10:52
[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: orangleliu@gmail.com
#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: orangleliu@gmail.com
#Created Time: 2014-12-11 20:16:33
############################

其他语言也可以类似的编程,这样就不用每次都在代码开头写各种头文件啊,协议啊,作者,日期啥的了。