Vi 和 Vim 自动命令:自动向文件添加自定义标题的 3 个步骤
【摘要】 使用 Vi/Vim 中的自动命令功能,您可以指定一些 Vim 命令在读取或写入文件、进入/离开缓冲区/窗口或退出 Vim 时自动执行。
使用 Vi/Vim 中的自动命令功能,您可以指定一些 Vim 命令在读取或写入文件、进入/离开缓冲区/窗口或退出 Vim 时自动执行。
在本文中,通过 3 个简单的步骤,让我们了解一下如何使用 Vim 强大的 autocmd 功能在文件中创建一个包含文件名、创建日期、最后修改日期的文件头部分(例如,C 编程代码中的头文件)在 vi 中打开文件时会自动填充 /time。
Vim 自动命令语法:
autocmd {event} {pattern} {cmd}
事件:有超过 40 个 autocmd 事件。以下是一些示例 autocmd 事件。
BufNewFile - Starting to edit a file that doesn't exist.
FileReadPre - Before reading a file with a ":read" command.
BufWritePre - Starting to write the whole buffer to a file.
FileWritePre - Starting to write part of a buffer to 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 to another buffer.
SwapExists - Detected an existing swap file.
大多数开发人员都希望为他们的程序提供一些默认标头。让我们举个例子。打开“.c”文件时,您需要一个包含作者、文件名等的文件头。考虑到我需要在打开新的“.c”文件时自动加载以下模板。您可以通过下面提到的三个步骤来实现这一点。
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : 1.c
* Purpose :
* Creation Date : 22-12-2012
* Last Modified : Mon 22 Dec 2012 10:36:49 PM PST
* Created By :
_._._._._._._._._._._._._._._._._._._._._.*/
第 1 步:创建模板文件
将上述模板保存在文本文件中,第一行带有“:insert”,然后是模板,最后一行带有“.”(点),如下所示。
$ cat c_header.txt
:insert
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name :
* Purpose :
* Creation Date :
* Last Modified :
* Created By :
_._._._._._._._._._._._._._._._._._._._._.*/
.
第 2 步:将 autocmd 命令添加到 ~/.vimrc
在 ~/.vimrc 文件中添加以下行。
$ cat ~/.vimrc
autocmd bufnewfile *.c so /home/tiamo/c_header.txt
autocmd bufnewfile *.c exe "1," . 10 . "g/File Name :.*/s//File Name : " .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 Name : myfile.c
* Purpose :
* Creation Date : 20-12-2012
* Last Modified :
* Created By :
_._._._._._._._._._._._._._._._._._._._._.*/
当您保存 myfile.c 文件时,它会相应地自动更新 Last Modified 字段,如下所示。
$ vi myfile.c
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : myfile.c
* Purpose :
* Creation Date : 20-12-2012
* Last Modified : Sat 20 Dec 2012 09:37:30 AM PST
* Created By :
_._._._._._._._._._._._._._._._._._._._._.*/
~/.vimrc 中 autocmd 命令的解释
$ cat -n ~/.vimrc
1 autocmd bufnewfile *.c so /home/tiamo/c_header.txt
2 autocmd bufnewfile *.c exe "1," . 10 . "g/File Name :.*/s//File Name : " .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 行将从第 1 行到第 10 行搜索模式“文件名:”。如果找到,它将在该行中写入当前文件名。
- 第 3 行将更新创建日期字段。
- 第 5 行将使用您保存文件时的当前日期和时间更新上次修改字段。
- 第 4 & 6 行:保存文件时,光标会移动到“Last modified:”(因为最后一次写操作)。如果你想让光标回到之前的位置,你需要将第 4 行和第 6 行添加到 .vimrc 文件中。
- 第 4 行将在更新前标记当前光标位置。
- 第 6 行将光标位置恢复到之前的位置。
最后说明:
- Verify whether autocmd is enabled in Vi / Vim - 从 vi / vim 执行 :version。如果启用了自动命令功能,它将显示+autocmd。
- Autocommand help– 从 vi / vim 执行 :help au,以获取有关 vim 自动命令功能的快速帮助。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)