1. 程式人生 > 其它 >編輯shell指令碼時,自動添加註釋

編輯shell指令碼時,自動添加註釋

在家目錄下建立 .vimrc檔案(任意目錄均可)

vim.vimrc

內容如下

set si
autocmd BufNewFile *.sh exec ":call SeTitle()"
func SeTitle()
        if expand("%:e")  == 'sh'
        call setline(1,"#!/bin/bash")
        call setline(2,"########################################")
        call setline(3,    "#Author:lee_yanyi")
        call setline(4,    "#time:"        .strftime("%c"))
        call setline(5,"#filename:"        .expand("%"))
        call setline(6,"#Script description:")
        call setline(7,"########################################")
endif
endfunc

建立測試指令碼

vim test.sh

自動出現一下內容表示成功

#!/bin/bash
########################################
#Author:lee_yanyi
#time:2021年04月24日 星期六 10時15分09秒
#filename:test.sh
#Script description:
########################################

在此基礎上輸入指令碼內容 echo 'i love linux'

直接敲G到檔案最後一行,再輸入o 進行檔案編輯

#!/bin/bash
########################################
#Author:lee_yanyi
#time:2021年04月24日 星期六 10時15分09秒
#filename:test.sh
#Script description:
########################################
echo 'i love linux'
                  

賦權並執行測試指令碼自動新增內容正確性

chmod +x test.sh

./test.sh