python自动生成文件头

"auto add pyhton header --start
autocmd BufNewFile *.py 0r ~/.vim/vim_template/vim_python_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

在~/.vimrc中添加如上内容。

另外,还需要在创建~/.vim/vim_template/vim_python_header

1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 ############################
4 #File Name:
5 #Author: frank
6 #Mail: frank0903@aliyun.com
7 #Created Time:
8 ############################

配置完成后,再vi 1.py

这种在vim中创建文件头的方式也可以用于其他语言。

参考:http://blog.csdn.net/orangleliu/article/details/41902851

原文地址:https://www.cnblogs.com/black-mamba/p/7061333.html