用vim打開.py和.sh文件自動添加頭


在~/.vimrc文件最后一行添加

"auto add pyhton header --start
autocmd BufNewFile *.py 0r ~/.vim/template/py.clp
autocmd BufNewFile *.py ks|call FileName()|'s
autocmd BufNewFile *.py ks|call CreatedTime()|'s

fun FileName()
    if line("$") > 10
        let l = 10  "這里是字母L 不是數字1
    else
        let l = line("$")
    endif
    exe "1," . l . "g/File Name:.*/s/File Name:.*/File Name: " .expand("%")
    "最前面是數字1,這里的File Name:要和模板中一致
endfun

fun CreatedTime()
    if line("$") > 10
        let l = 10
    else
        let l = line("$")
    endif
    exe "1," . l . "g/Created Time:.*/s/Created Time:.*/Created Time: ".strftime("%Y-%m-%d %T")
    "這里Create Time:要和模板中一致
endfun
"auto add python header --end
"
"auto add bash header --start

autocmd BufNewFile *.sh 0r ~/.vim/template/sh.clp
autocmd BufNewFile *.sh ks|call CreatedTime()|'s

"auto add bash header --end

然后在~/.vim/template/文件夾(template自己創建)下添加 py.clp文件

#!/usr/bin/python
#-*- coding:utf-8 -*-
############################
#File Name:
#Author: wudi
#Mail: programmerwudi@gmail.com
#Created Time:
############################

~/.vim/template/文件下添加 sh.clp文件

#!/bin/bash

vim test.py 如下: (我把py.clp姓名和時間頭都刪了, 不太喜歡那個)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM