如何编辑vim配置文件使新建文件自动写入文件头

本篇文章为大家展示了如何编辑vim配置文件使新建文件自动写入文件头,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

因工作需要经常写shell脚本,每次都要写脚本的头,就想偷个懒,在每次写脚本的时候可以自动生成想要的信息,编辑/etc/vimrc该文件,在新增.sh文件的时候会出现一些信息 

autocmd BufNewFile *.sh exec ":call Setcomment()"

func Setcomment()

        call append(0,"#!/bin/bash")

        call append(1,"#*********************************** ")

        call append(2,"#*       copyleft test " .strftime("%Y-%m-%d"))

        call append(3,"#*       scriptname: " .expand("%"))

        call append(4,"#*       email:  sb@localhost")

        call append(5,"#*       version: v0.1 ")

        call append(6,"#*********************************** ")

endfunc

=================================================================================================================================================================================================================================

编辑/etc/vimrc该文件,在新增.sh以及.py文件的时候会出现一些信息

autocmd BufNewFile *.py,*.sh, exec ":call SetTitle()"

let $author_name = "xxx"

let $author_email = "xxx@xxx.xxx"

func SetTitle()

if &filetype == 'sh'

        call setline(1,"\###################################################################")

        call append(line("."), "\# File Name: ".expand("%"))

        call append(line(".")+1, "\# Author: ".$author_name)

        call append(line(".")+2, "\# mail: ".$author_email)

        call append(line(".")+3, "\# Created Time: ".strftime("%c"))

        call append(line(".")+4, "\#=============================================================")

        call append(line(".")+5, "\#!/bin/bash")

        call append(line(".")+6, "")

else

        call setline(1,"\###################################################################")

        call append(line("."), "\# File Name: ".expand("%"))

        call append(line(".")+1, "\# Author: ".$author_name)

        call append(line(".")+2, "\# mail: ".$author_email)

        call append(line(".")+3, "\# Created Time: ".strftime("%c"))

        call append(line(".")+4, "\#=============================================================")

        call append(line(".")+5, "\#!/usr/bin/python")

        call append(line(".")+6, "")

endif

endfunc

上述内容就是如何编辑vim配置文件使新建文件自动写入文件头,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。