参考vim设置新建文件自动填入的文件头,以及设置编辑时间_Waleking的专栏-CSDN博客
新建.c,.h,.sh,.java,.py文件,自动插入文件头,以及一些个人信息,不用每次都手动输入。
方法
修改~/.vimrc文件(该文件是用户自己vim的配置文件,可随意修改)。在 .vimrc 中添加如下内容:
1.新建文件自动填入文件头
"新建.c,.h,.sh,.java,.py文件,自动插入文件头
autocmd BufNewFile *.py,*.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#!/bin/bash")
call append(line("."), "")
call append(line(".")+1, "\"\"\"")
call append(line(".")+2, "\# File Name: ".expand("%"))
call append(line(".")+3, "\# Author: thc")
"call append(line(".")+2, "\# mail: zhnlion@126.com")
call append(line(".")+4, "\# Created Time: ".strftime("%c"))
call append(line(".")+5, "")
call append(line(".")+6, "\"\"\"")
endif
if &filetype == 'python'
call setline(1,"\#!/usr/bin/python")
call append(line("."), "\#coding:utf-8")
call append(line(".")+1, "\"\"\"")
call append(line(".")+2, "\# File Name: ".expand("%"))
call append(line(".")+3, "\# Author: thc")
call append(line(".")+4, "\# Created Time: ".strftime("%H:%M %Y-%m-%d"))
call append(line(".")+5, "")
call append(line(".")+6, "\"\"\"")
else
call setline(1,
"/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: thc")
"call append(line(".")+2, " > Mail: xxx@163.com ")
call append(line(".")+2, " > Created Time: ".strftime("%c"))
call append(line(".")+3, " ************************************************************************/")
call append(line(".")+4, "")
endif
if &filetype == 'cpp'
call append(line(".")+5, "#include<iostream>")
call append(line(".")+6, "using namespace std;")
call append(line(".")+7, "")
endif
if &filetype == 'c'
call append(line(".")+5, "#include<stdio.h>")
call append(line(".")+6, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
可以根据自己需要加入自定义信息。
注意!!
这里&filetype == 'python',这里如果设置为&filetype == 'py',不能正常识别python文件。
保存.vimrc文件后,新建一个.py文件可以看到文件头内容会自动生成。
2.文件头显示文件最后的修改时间
" modify the last modified time of a file
func SetLastModifiedTime(lineno)
let modif_time = strftime("%c")
if a:lineno == "-1"
let line = getline(4) "此处行号要和上面create time行号对应
else
let line = getline(a:lineno)
endif
if line =~ '^\sLast Modified'
let line = substitute( line,':\s\+.*\d\{4\}', ':'.modif_time, "" )
else
let line = ' Last Modified: '.modif_time
endif
if a:lineno == "-1"
call setline(7, line) "此处行号为要显示行号(绝对行号)
else
call append(a:lineno, line)
endif
endfunc
" map the SetLastModifiedTime command automatically
au BufWrite *.py,*.java call SetLastModifiedTime(-1)
注意其中两处行号所以要和实际对应!
值得注意的是,最后一句"au BufWrite *.v call SetLastModifiedTime(-1)"的作用是制定在文件类型是*.v的文件中当进行文件保存时调用该函数。au是Vim中的自动命令,一般格式为:
一般格式:autocmd 事件 文件类型 命令
例子:au BufNewFile,BufRead *.pl setf perl
解释:当事件 BufNewFile 和 BufRead 发生在 *.pl 文件上的时候,执行命令:setf perl
对文件更改后,可以看到会生成上次保存时间。
需要注意的是,此方法只有在编辑相同文件头的文件时没问题,如果编辑外部导入代码,文件头不一致,生成的last modified会覆盖原有内容,需要留意!!
3.鼠标直接定位
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key