这是Lakshmanan G写的客座帖子。
本文是正在进行中的一部分 Vi / Vim提示& Tricks 系列。使用Vi / Vim中的自动命令功能,您可以指定一些在读取或写入文件,进入/离开缓冲区/窗口或退出Vim时自动执行的Vim命令。
在本文中,通过3个简单的步骤,让我们回顾一下如何使用Vim的这一强大的autocmd功能在文件内创建标头部分(例如,C编程代码中的标头),其文件名,创建日期,上次修改日期在vi中打开文件时,/ time自动填充。
Vim autocmd语法:
autocmd {event} {pattern} {cmd}
大事记: 有40多个autocmd事件。以下是一些示例autocmd事件。
BufNewFile - Starting 至 edit a file that doesn't exist. FileReadPre - Before reading a file with a ":read" command. BufWritePre - Starting 至 write the whole buffer 至 a file. FileWritePre - Starting 至 write part of a buffer 至 a file. BufDelete - Before deleting a buffer from the buffer list. BufWipeout - Before completely deleting a buffer. BufNew - Just after creating a new buffer. BufEnter - After entering a buffer. BufLeave - Before leaving 至 another buffer. SwapExists - Detected an existing swap file.
大多数开发人员都希望为其程序提供一些默认头。让我们举个例子。当打开“ .c”文件时,您需要一个包含作者,文件名等的文件头。考虑到我需要在打开新的“ .c”文件时自动加载以下模板。您可以按照以下三个步骤来实现此目的。
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. * File 名称 : 1.c * Purpose : * Creation Date : 22-12-2008 * Last Modified : Mon 22 Dec 2008 10:36:49 PM PST * Created By : _._._._._._._._._._._._._._._._._._._._._.*/
步骤1:创建模板文件
将上面的模板保存在文本文件中“:insert”在第一行中,然后是模板,在最后一行中是“。”(点),如下所示。
$ cat c_header.txt :insert /* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. * File 名称 : * Purpose : * Creation Date : * Last Modified : * Created By : _._._._._._._._._._._._._._._._._._._._._.*/ .
步骤2:将autocmd命令添加到〜/ .vimrc
在〜/ .vimrc文件中添加以下行。
$ cat 〜/ .vimrc autocmd bufnewfile *.c so /home/jsmith/c_header.txt autocmd bufnewfile *.c exe "1," . 10 . "g/File 名称 :.*/s//File 名称 : " .expand("%") autocmd bufnewfile *.c exe "1," . 10 . "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y") autocmd Bufwritepre,filewritepre *.c execute "normal ma" autocmd Bufwritepre,filewritepre *.c exe "1," . 10 . "g/Last Modified :.*/s/Last Modified :.*/Last Modified : " .strftime("%c") autocmd bufwritepost,filewritepost *.c execute "normal `a"
步骤3:创建一个带有自动标题的新* .c文件
现在,当您使用vim创建一个新的* .c文件时,它将自动添加在Step1中定义的标题,并自动填充文件名和创建日期,如下所示。
$ vi myfile.c /* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. * File 名称 : myfile.c * Purpose : * Creation Date : 20-12-2008 * Last Modified : * Created By : _._._._._._._._._._._._._._._._._._._._._.*/
保存myfile.c文件时,它将相应地自动更新Last Modified字段,如下所示。
$ vi myfile.c /* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. * File 名称 : myfile.c * Purpose : * Creation Date : 20-12-2008 * Last Modified : Sat 20 Dec 2008 09:37:30 AM PST * Created By : _._._._._._._._._._._._._._._._._._._._._.*/
〜/ .vimrc中的autocmd命令说明
$ cat -n 〜/ .vimrc 1 autocmd bufnewfile *.c so /home/jsmith/c_header.txt 2 autocmd bufnewfile *.c exe "1," . 10 . "g/File 名称 :.*/s//File 名称 : " .expand("%") 3 autocmd bufnewfile *.c exe "1," . 10 . "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y") 4 autocmd Bufwritepre,filewritepre *.c execute "normal ma" 5 autocmd Bufwritepre,filewritepre *.c exe "1," . 10 . "g/Last Modified :.*/s/Last Modified :.*/Last Modified : " .strftime("%c") 6 autocmd bufwritepost,filewritepost *.c execute "normal `a"
- 第1行定义了模板文件。这表示对于* .c文件,应使用/home/jsmith/c_header.txt模板文件。
- 第2行将从第一行到第十行搜索模式“文件名:”。如果找到,它将在该行中写入当前文件名。
- 第3行将更新“创建日期”字段。
- 保存文件时,第5行将使用当前日期和时间更新“上次修改时间”字段。
- 4号线&6:在保存文件时,光标将移至“Last modified :” (because of last write operation). If you want the cursor back 至 the previous position then, you need 至 add 4号线and 6 至 the .vimrc file.
- 4号线will mark the current cursor position before updating.
- 第6行将光标位置恢复到之前的位置。
最后说明:
- 验证是否在Vi / Vim中启用了autocmd –从vi / vim执行:version如果启用了自动命令功能,它将显示+ autocmd。
- 自动命令帮助 –从vi / vim中执行:help au,以获得有关vim autocmd功能的快速帮助。
推荐读物
学习Vi和Vim编辑器,由Arnold Robbins撰写。一世’一个命令行迷。所以我自然’我是Vi和Vim编辑器的忠实拥护者。几年前,当我在Linux上编写大量C代码时,我一直随身携带Vi编辑器的参考资料。即使你’我已经使用Vi和Vim编辑器好几年了,还没有读过这本书,请帮个忙,阅读这本书。您’Vim编辑器的功能会让您惊讶。
本文是正在进行中的一部分 Vi / Vim提示和技巧 系列。 请 订阅 至 怪胎 和唐’不要错过任何未来的Vi和Vim编辑器提示和技巧。
本文由Lakshmanan G写。他正在从事 bk Systems(p)Ltd,并有兴趣为开源做贡献。 极客的东西welcomes your tips and 客座文章 .
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
这篇文章既有趣又很有用
循序渐进的方法是可观的。
它将避免在最小c使用量的地方使用插件。