linux下vim自动在c文件插入文件头

我们希望在新建c文件时,自动在文件头部加入一些代码,比如预处理命令,和编码设置,可以将以下配置放到/etc/vimrc或者 ~/.vimrc 文件底部,然后退出vim在进入vim即可生效。
"新建.c,.h,.sh,.java文件,自动插入文件头 autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" """定义函数SetTitle,自动插入文件头 func SetTitle() "如果文件类型为.sh文件 if &filetype == 'sh' call setline(1,"\#########################################################################") call append(line("."), "\# File Name: ".expand("%")) call append(line(".")+1, "\# Author: pengzhen") call append(line(".")+2, "\# mail: pz9042@163.com") 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: pengzhen") call append(line(".")+2, " > Mail: pz9042@163.com ") call append(line(".")+3, " > Created Time: ".strftime("%c")) call append(line(".")+4, " ************************************************************************/") call append(line(".")+5, "") 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

  

posted @ 2014-10-21 15:18  木偶人  阅读(1636)  评论(0编辑  收藏  举报