" 自动设置备注
function! InsertHeaderComment()
" 检查是否是一个新文件
if line('$') == 1 && getline(1) == ''
" 插入注释
call append(0, "/**********************************************************")
call append(1, " > Author: LCL <lcl20001210@icloud.com>")
call append(2, " > Repository: https://github.com")
call append(3, " > Create Date: " . strftime("%Y-%m-%d"))
call append(4, " > License: MIT")
call append(5, "************************************************************/")
endif
endfunction
" 在打开特定类型的文件时插入头部注释 绑定F2键一键插入
autocmd BufNewFile *.c,*.py,*.cpp,*.h,*.cc call InsertHeaderComment()
function! InsertCustomCommentAtCursor()
let l:filename = expand('%:t') " 获取当前文件名
let l:author = "LCL <lcl20001210@icloud.com>"
let l:create_date = strftime("%Y-%m-%d %H:%M") " 获取当前日期和时间
let l:license = "MIT"
let l:comment = [
\ '//************************************************************',
\ '//>Filename: ' . l:filename,
\ '//>Author: ' . l:author,
\ '//>Create Date: ' . l:create_date,
\ '//>Function name: ' ,
\ '//>Function introduction: ' ,
\ '//************************************************************',
\ ''
\ ]
" 在光标当前位置插入注释
let l:current_line = line('.')
call append(l:current_line, l:comment)
" 将光标移动到注释的下方
execute "normal! " . (len(l:comment) + 1) . "j"
endfunction
nnoremap <F2> :call InsertCustomCommentAtCursor()<CR>