vim 自动生成文件头的配制

" 使用F4键调用函数AddAuthor
" map <F4> ms:call AddAuthor()<cr>'S

function AddAuthor()
    let n=1
    while n < 11
        let line = getline(n)
        if line=~'[#]*\s*\*\s*\S*Last\s*modified\s*:\s*\S*.*$'
        call UpdateTitle()
        return
    endif
    let n = n + 1
    endwhile
    if &filetype == 'sh'
        call AddTitleForShell()
    elseif &filetype == 'python'
        call AddTitleForPython()
    elseif &filetype == 'h'
	call AddTitleForH()
    else
        call AddTitleForC()
    endif

endfunction

autocmd BufNewFile *.h,*.H exec ":call AddTitleForH()"
autocmd BufNewFile *.c,*.C exec ":call AddTitleForC()"
autocmd BufNewFile *.sh exec ":call AddTitleForShell()"

"" 表示非.sh或.py结尾的文件添加此函数注释
function AddTitleForC()
    call append(0,"/***********************************************************")
    call append(1,"File name     : ".expand("%:t"))
    call append(2,"Author name   : lancz")
    call append(3,"Email address : xx@xx.com")
    call append(4,"Create time   : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(5,"***********************************************************/")
    call append(6,"#include <stdio.h>")
endfunction

"" 表示.py添加此函数注释
function AddTitleForPython()
    call append(0,"#!/usr/bin/env python")
    call append(1,"# coding:utf-8")
    call append(2,"")
    call append(3,"#/***********************************************************")
    call append(4,"#File name     : ".expand("%:t"))
    call append(5,"#Author name   : lancz")
    call append(6,"#Email address : xxx@xx.xx")
    call append(7,"#Create time   : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(8,"#Description   : ")
    call append(9,"#***********************************************************/")
    echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endfunction

"" 表示.sh文件添加此行数注释
function AddTitleForShell()
    call append(0,"#!/bin/bash")
    call append(1,"# **********************************************************")
    call append(2,"# Author        : lancz")
    call append(3,"# Email         : xxx@xx.xx")
    call append(4,"# Create time   : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(5,"# Last modified : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(6,"# Filename      : ".expand("%:t"))
    call append(7,"# Description   : ")
    call append(8,"# **********************************************************")
endfunction


function AddTitleForH()
    call append(0,"/***********************************************************")
    call append(1,"File name     : ".expand("%:t"))
    call append(2,"Author name   : lancz")
    call append(3,"Email address : xxx@xxx.xx")
    call append(4,"Create time   : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(5,"***********************************************************/")
    call append(6,"#ifndef __".toupper("".expand("%:r"))."_H__")
    call append(7,"#define __".toupper("".expand("%:r"))."_H__")
    call append(8, "")
    call append(9, "")
    call append(10,"#endif")
endfunction

"小声点拿走就是了,我也是根据网上的资料修改来的。

You may also like...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据