vim创建程序文件自动添加头部注释

简介

实现写一个脚本的时候添加头部注释,方法可自动注释也可手动注释,方法如下:

一、添加配置文件

[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                 #
#                                                          #
#                                                          #
# *********************************************************#
复制代码
至此
已经实现成功。
posted @   阿rua  阅读(240)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)
点击右上角即可分享
微信分享提示