欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

为vim编辑器添加默认的作者信息

程序员文章站 2022-05-26 14:01:48
...

修改/etc/vimrc配置文件,在文件末尾添加如下内容即可

" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新文件标题""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.c,.h,.sh,.java文件,自动插入文件头 
autocmd BufNewFile *.cpp,*.[ch] exec ":call SetTitleC()" 
autocmd BufNewFile *.sh exec ":call SetTitleSh()" 
autocmd BufNewFile *.php exec ":call SetTitlePhp()" 
autocmd BufNewFile *.py exec ":call SetTitlePy()" 
autocmd BufNewFile *.html,*.htm,*.phtml exec ":call SetTitleHtml()" 
autocmd BufNewFile *.css exec ":call SetTitleCss()" 
autocmd BufNewFile *.java exec ":call SetTitleJava()" 


""定义函数SetTitle,自动插入文件头
func SetTitle()
        call setline(1, "#/*************************************************************************") 
        call append(line("."), "#    > File Name: ".expand("%")) 
        call append(line(".")+1, "#    > Author: liyong") 
        call append(line(".")+2, "#    > Mail: 2550702985@qq.com") 
        call append(line(".")+3, "#    > Created Time: ".strftime("%Y-%m-%d %H:%M"))
        call append(line(".")+4, "#    > Modified Time: ".strftime("%Y-%m-%d %H:%M"))
        call append(line(".")+5, "# ************************************************************************/")
endfunc

func SetTitleC()
        call SetTitle()
        call append(line(".")+6, "#include<stdio.h>")
        call append(line(".")+7, "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc 

func SetTitleSh()
        call SetTitle()
        call append(line(".")+6, "\#!/bin/bash")
        call append(line(".")+7, "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc 

func SetTitlePhp()
        call SetTitle()
        call append(line(".")+6, "<?php")
        call append(line(".")+7, "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc 

func SetTitlePy()
        call SetTitle() 
        call append(line(".")+6, "#!/usr/bin/env python")
        call append(line(".")+7, "# -*- coding: utf-8 -*-") 
        call append(line(".")+8, "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc

func SetTitleHtml()
        call setline(1, "<!DOCTYPE HTML>")
        call append(line(".")+1, "<html lang="en-US">")
        call append(line(".")+2, "<head>")
        call append(line(".")+3, "  <meta charset="UTF-8">")
        call append(line(".")+4, "  <title></title>")
        call append(line(".")+5, "</head>")
        call append(line(".")+6, "<body>")
        call append(line(".")+7, "</body>")
        call append(line(".")+8, "</html>")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc


func SetTitleCss()
        call setline(1, "@charset 'utf-8'")
        call append(line("."), "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc

func SetTitleJava()
        call SetTitle()
        call append(line(".")+6, "")
        "新建文件后,自动定位到文件末尾
        autocmd BufNewFile * normal G
endfunc

func SetFileTitle() 
    "如果文件类型为.sh文件 
    if &filetype == 'sh' 
        call setline(1,"\#########################################################################") 
        call append(line("."), "\# File Name: ".expand("%")) 
        call append(line(".")+1, "\# Author: ma6174") 
        call append(line(".")+2, "\# mail: ma6174@163.com") 
        call append(line(".")+3, "\# Created Time: ".strftime("%Y-%m-%d %H:%M")) 
        call append(line(".")+4, "\# Description: "
        call append(line(".")+5, "\#########################################################################") 
        call append(line(".")+6, "\#!/bin/bash") 
        call append(line(".")+7, "") 
    else 
        call setline(1, "/*************************************************************************") 
        call append(line("."), "    > File Name: ".expand("%")) 
        call append(line(".")+1, "    > Author: liyong") 
        call append(line(".")+2, "    > Mail: 2550702985@qq") 
        call append(line(".")+3, "    > Created Time: ".strftime("%Y-%m-%d %H:%M")) 
        call append(line(".")+4, " ************************************************************************/") 
        call append(line(".")+5, "")
    endif

    "如果文件类型为.py文件
    if &filetype == 'py'
        call append(line(".")+6, "#!/usr/bin/env python")
        call append(line(".")+7, "# -*- coding: utf-8 -*-")
    endif

    "如果文件类型为.php文件
    if &filetype == 'php'
        call append(line(".")+6, "<?php")
    endif

    "如果文件类型为.html文件
    if &filetype == 'html'
        call append(line(".")+6, "<!DOCTYPE HTML>")
        call append(line(".")+7, "<html lang="en-US">")
        call append(line(".")+8, "<head>")
        call append(line(".")+9, "  <meta charset="UTF-8">")
        call append(line(".")+10, " <title></title>")
        call append(line(".")+11, "</head>")
        call append(line(".")+12, "<body>")
        call append(line(".")+13, "</body>")
        call append(line(".")+14, "</html>")
    endif

    "如果文件类型为.css文件
    if &filetype == 'css'
        call setline(1, "@charset 'utf-8'")
    endif

    if &filetype == 'cpp'
        call append(line(".")+6, "#include<iostream>")
        call append(line(".")+7, "using namespace std;")
        call append(line(".")+8, "")
    endif
    if &filetype == 'c'
        call append(line(".")+6, "#include<stdio.h>")
        call append(line(".")+7, "")
    endif
    "新建文件后,自动定位到文件末尾
    autocmd BufNewFile * normal G
endfunc 


"将键盘上的F4功能键映射为添加作者信息的快捷键  
map <F4> ms:call TitleDet()<cr>'s  
function AddTitle()  
        call append(0,"/*******************************************************************************")  
        call append(1," * Author     :liyong")  
        call append(2," * Email  : [email protected]")  
        call append(3," * Last modified : ".strftime("%Y-%m-%d %H:%M"))  
        call append(4," * Filename   : ".expand("%:t"))  
        call append(5," * Description    : ")  
        call append(6," * *****************************************************************************/")  
        echohl WarningMsg | echo "Successful in adding the copyright." | echohl None  
endfunction 

function UpdateTitle()  
        normal m'  
        execute '/# *Last modified:/[email protected]:.*[email protected]\=strftime(":\t%Y-%m-%d %H:%M")@'  
        normal "  
        normal mk  
        execute '/# *Filename:/s@:.*$@\=":\t\t".expand("%:t")@'  
        execute "noh"  
        normal 'k  
        echohl WarningMsg | echo "Successful in updating the copy right."| echohl None  
endfunction

function TitleDet()  
        let n=1  
        while n < 10  
                let line = getline(n)  
                if line =~'^\#\s*\S*Last\smodified:\S*.*$'  
                        call UpdateTitle()  
                        return  
                endif  
                let n = n + 1  
        endwhile  
        call AddTitle()  
endfunction
相关标签: 编辑器 vim