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

来源:互联网 发布:淘宝买家恶意申请退款 编辑:程序博客网 时间:2024/06/10 01:12
使用vim脚本来实现的,使用了模板,几行代码就能实现很实用。

~/.vimrc 中的代码
"auto add pyhton header --startautocmd BufNewFile *.py 0r ~/.vim/vim_template/vim_pyhton_headerautocmd BufNewFile *.py ks|call FileName()|'sautocmd BufNewFile *.py ks|call CreatedTime()|'sfun FileName()if line("$") > 10let l = 10  "这里是字母L 不是数字1 elselet l = line("$")endif exe "1," . l . "g/File Name:.*/s/File Name:.*/File Name: " .expand("%")         "最前面是数字1,这里的File Name: 要和模板中一致endfun fun CreatedTime()if line("$") > 10let l = 10elselet 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.pylzz@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############################

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


参考:https://gist.github.com/zxkletters/6521114

本文出自 “orangleliu笔记本” 博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/41902851

作者orangleliu 采用署名-非商业性使用-相同方式共享协议

0 0
原创粉丝点击