Vim 脚本自动生成文件头

本文介绍了一个使用Vim脚本自动为多种文件类型(包括.cc,.sh,.java等)创建标准文件头部注释的方法,并提供了自动更新文件最后修改时间的功能。该脚本适用于希望标准化代码注释格式的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/lyz2015lyz/article/details/79039802 

 

此脚本目前支持这些后缀:.cc, .sh, .java, .cpp, .h, .hpp, .py, .lua

自动创建文件头部注释的脚本

1. 把脚本保存为file_format.vim, 并存放在~/.vim/macros/目录下;

2. 编辑~/.vimrc文件,在文件末尾加入run macros/file_format.vim,即可
 

autocmd BufNewFile *.cc,*.sh,*.java,*.cpp,*.h,*.hpp,*.py,*.lua exec ":call SetTitle()"
"新建.cc,.java,.sh,.cpp,.h, .hpp,
"""定义函数SetTitle,自动插入文件头
func SetTitle()
    let filetype_name = strpart(expand("%"), stridx(expand("%"), "."))
    let file_name = strpart(expand("%"), 0, stridx(expand("%"), "."))
    if file_name =~ "\/"
        let file_name = strpart(file_name, strridx(file_name, "/") + 1)
    endif
    let time_value = strftime("%Y-%m-%d %H:%M:%S")
    if filetype_name == ".sh"
        call setline(1, "\#!/bin/bash")
        call append(line("."), "")
        call append(line(".")+1, "\#########################################################################")
        call append(line(".")+2, "\# File Name: ". file_name . filetype_name)
        call append(line(".")+3, "\# Created on: ".time_value)
        call append(line(".")+4, "\# Author: glendy")
        call append(line(".")+5, "\# Last Modified: ".time_value)
        call append(line(".")+6, "\# Description: ")
        call append(line(".")+7, "\#########################################################################")
        call append(line(".")+8, "")
    else
        if filetype_name == ".lua"
            call setline(1, "\--lua")
            call append(line("."), "") 
            call append(line(".")+1, "\--#########################################################################")
            call append(line(".")+2, "\--# File Name: ". file_name . filetype_name)
            call append(line(".")+3, "\--# Created on: ".time_value)
            call append(line(".")+4, "\--# Author: glendy")
            call append(line(".")+5, "\--# Last Modified: ".time_value)
            call append(line(".")+6, "\--# Description: ")
            call append(line(".")+7, "\--#########################################################################")
            call append(line(".")+8, "") 
            call append(line(".")+9, file_name . " = {}")
            call append(line(".")+10, file_name .".__index = ". file_name)
            call append(line(".")+11, "function ". file_name .":new()")
            call append(line(".")+12, "    local o = {}")
            call append(line(".")+13, "    self.__index = self")
            call append(line(".")+14, "    setmetatable(o, self)")
            call append(line(".")+15, "    \-- construct function code here")
            call append(line(".")+16, "    return o")
            call append(line(".")+17, "end")
            call append(line(".")+18, "function ". file_name .":hotfix()")
            call append(line(".")+19, "    setmetatable(self, ". file_name .")")
            call append(line(".")+20, "end")
            call append(line(".")+21, "") 
            call append(line(".")+22, "return ". file_name)
        else
            if filetype_name == ".py"
                call setline(1, "\# -*- coding: utf-8 -*-")
                call append(line("."), "") 
                call append(line(".")+1, "\#########################################################################") 
                call append(line(".")+2, "\# File Name: ". file_name . filetype_name)  
                call append(line(".")+3, "\# Created on : ".time_value)  
                call append(line(".")+4, "\# Author: glendy")
                call append(line(".")+5, "\# Last Modified: ".time_value)
                call append(line(".")+6, "\# Description:")  
                call append(line(".")+7, "\#########################################################################")
                call append(line(".")+8, "")
            else
                call setline(1, "\/*")
                call append(line("."), " * File Name: ". file_name . filetype_name)
                call append(line(".")+1, " * ")
                call append(line(".")+2, " * Created on: ".time_value)
                call append(line(".")+3, " * Author: glendy")
                call append(line(".")+4, " * ")
                call append(line(".")+5, " * Last Modified: ".time_value)
                call append(line(".")+6, " * Description: ")
                call append(line(".")+7, " */")
                call append(line(".")+8, "")
                if filetype_name == ".h"
                    call append(line(".")+9, "#ifndef _". toupper(file_name) . substitute(toupper(filetype_name), ".", "_", "") ."_")
                    call append(line(".")+10, "#define _". toupper(file_name) . substitute(toupper(filetype_name), ".", "_", "") ."_")
                    call append(line(".")+11, "")
                    call append(line(".")+12, "class " . file_name)
                    call append(line(".")+13, "{")
                    call append(line(".")+14, "public:")
                    call append(line(".")+15, "")
                    call append(line(".")+16, "protected:")
                    call append(line(".")+17, "")
                    call append(line(".")+18, "};")
                    call append(line(".")+19, "")
                    call append(line(".")+20, "#endif //". toupper(file_name) . substitute(toupper(filetype_name), ".", "_", "") ."_")
                endif
            endif
        endif
    endif
endfunc

自动更新文件的修改时间

  1. 把脚本保存为file_modify.vim, 并存放在~/.vim/macros/目录下;
  2. 编辑~/.vimrc文件,在文件末尾加入run macros/file_modify.vim,即可
" modify the last modified time of a file  
function SetLastModifiedTime(lineno)  
    let modif_time = strftime("%Y-%m-%d %H:%M:%S")
    if a:lineno == "-1"  
            let line = getline(7)  
    else  
            let line = getline(a:lineno)  
    endif
    if line =~ '\sLast Modified:'
            let line = strpart(line, 0, stridx(line, ":")) . ": " . modif_time
    endif  
    if a:lineno == "-1"  
            call setline(7, line)  
    else  
            call append(a:lineno, line)  
    endif  
endfunc

" map the SetLastModifiedTime command automatically  
autocmd BufWrite *.cc,*.sh,*.java,*.cpp,*.h,*.hpp,*.py,*.lua call SetLastModifiedTime(-1)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值