用vim打开.py和.sh文件自动添加头

在~/.vimrc文件最后一行添加

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

autocmd BufNewFile *.sh 0r ~/.vim/template/sh.clp
autocmd BufNewFile *.sh ks|call CreatedTime()|'s

"auto add bash header --end

然后在~/.vim/template/文件夹(template自己创建)下添加 py.clp文件

#!/usr/bin/python
#-*- coding:utf-8 -*-
############################
#File Name:
#Author: wudi
#Mail: programmerwudi@gmail.com
#Created Time:
############################

~/.vim/template/文件下添加 sh.clp文件

#!/bin/bash

vim test.py 如下: (我把py.clp姓名和时间头都删了, 不太喜欢那个)

原文地址:https://www.cnblogs.com/douzujun/p/10739109.html