简介
实现写一个脚本的时候添加头部注释,方法可自动注释也可手动注释,方法如下:
一、添加配置文件
[root@testOS ~]# vim ~/.vimrc
配置内容如下:(示例包含shell、Python脚本)
" Adding header comments when creating *. sh or *. py scripts in vim "" Press the shortcut key F8 to automatically add Shell and header comments "" Press the shortcut key F9 to automatically add Python and header comments map <F8> ms:call AddTitleForShell()<cr> ' s " This shell script automatic annotation autocmd BufNewFile *.sh exec ":call AddTitleForShell()" function AddTitleForShell() call append(0,"#!/bin/bash") call append(1,"# *********************************************************#") call append(2,"# * Author : aRUA #") call append(3,"# * Email : xxxxxxxxxxxxxxxxxxxxxx #") call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M:%S")." #") call append(5,"# * Filename : ".expand("%:t")." #") call append(6,"# * Description : This is Shell scripts #") call append(7,"# #") call append(8,"# #") call append(9,"# *********************************************************#") endfunction map <F9> ms:call AddTitleForPython()<cr> ' s " This python script automatic annotation autocmd BufNewFile *.py exec ":call AddTitleForPython()" function AddTitleForPython() call append(0,"#!/usr/bin/python3") call append(1,"# *********************************************************#") call append(2,"# * Author : aRUA #") call append(3,"# * Email : xxxxxxxxxxxxxxxxxxxxxx #") call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M:%S")." #") call append(5,"# * Filename : ".expand("%:t")." #") call append(6,"# * Description : This is Python scripts #") call append(7,"# #") call append(8,"# #") call append(9,"# *********************************************************#") endfunction
通过删除下面两行可实现F8/F9手动添加注释
autocmd BufNewFile *.sh exec ":call AddTitleForShell()" 或 autocmd BufNewFile *.py exec ":call AddTitleForPython()"
二、测试
示例如下(shell示例):
[root@testOS ~]# vim hello.sh #!/bin/bash # *********************************************************# # * Author : aRUA # # * Email : xxxxxxxxxxxxxxxxxxxxxx # # * Create time : 2023-06-29 23:32:41 # # * Filename : hello.sh # # * Description : This is Shell scripts # # # # # # *********************************************************#
示例如下(python示例):
[root@testOS ~]# vim hello.py #!/usr/bin/python3 # *********************************************************# # * Author : aRUA # # * Email : xxxxxxxxxxxxxxxxxxxxxx # # * Create time : 2023-06-29 23:33:20 # # * Filename : hello.py # # * Description : This is Python scripts # # # # # # *********************************************************#
至此
已经实现成功。