Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
来源:互联网 发布:重置网络命令管理员 编辑:程序博客网 时间:2024/05/16 01:52
Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
In this article, using 3 simple steps, let us review how to use this powerful autocmd feature of Vim to create a header section inside a file (for example, header in a C programming code) with file name, creation date, last modified date/time automatically populated when you open a file in vi.
Vim autocmd syntax:autocmd {event} {pattern} {cmd}
Events: There are more than 40 autocmd events. Following are few sample autocmd events.
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.
help autocmd-events // get more information about autocmd events
Most of the developers want some default header for their programs. Lets take an example. When opening a “.c” file, you need a file header which has author, filename etc.. Consider that I need the following template to be loaded automatically while opening a new “.c” file. You can achieve this in three steps as mentioned below.
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.* File Name : 1.c* Purpose :* Creation Date : 22-12-2008* Last Modified : Mon 22 Dec 2008 10:36:49 PM PST* Created By : _._._._._._._._._._._._._._._._._._._._._.*/
Step 1: Create a template file
Save the above template in a text file with “:insert” in the first line, followed by the template and a “.”(dot) in the last line as shown below.
$ cat c_header.txt:insert/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.* File Name :* Purpose :* Creation Date :* Last Modified :* Created By : _._._._._._._._._._._._._._._._._._._._._.*/.
Step 2: Add autocmd commands to ~/.vimrc
Add the following lines in the ~/.vimrc file.
$ cat ~/.vimrcautocmd bufnewfile *.c so /home/jsmith/c_header.txtautocmd 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"
Step 3: Create a new *.c file with automatic header
Now, when you create a new *.c file using vim, this will automatically add the header defined in the Step1 and populate the File Name and Creation Date automatically as shown below.
$ vi myfile.c/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.* File Name : myfile.c* Purpose :* Creation Date : 20-12-2008* Last Modified :* Created By :_._._._._._._._._._._._._._._._._._._._._.*/
When you save the myfile.c file, it will automatically update the Last Modified field accordingly as shown below.
$ vi myfile.c/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.* File Name : myfile.c* Purpose :* Creation Date : 20-12-2008* Last Modified : Sat 20 Dec 2008 09:37:30 AM PST* Created By :_._._._._._._._._._._._._._._._._._._._._.*/
Explanation of the autocmd commands inside ~/.vimrc
$ cat -n ~/.vimrc 1 autocmd bufnewfile *.c so /home/jsmith/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"
Final Note:
- Verify whether autocmd is enabled in Vi / Vim – Execute :version from vi / vim. If autocommand feature is enabled, it will display +autocmd.
- Autocommand help – Execute :help au from vi / vim, to get quick help on vim autocmd features.
- Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
- Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
- Add header and footer to some file
- Steps to add Custom Tabs to the Product Admin
- 7 Steps to Write Your Own Custom Rule using FXCOP
- How to add a Custom Pull to Refresh in your iOS Application
- How to add custom scripts
- Hexo-blogs-to-add-search-and-the-python-scripts-record-automatically
- Vi and Vim Macro Tutorial: How To Record and Play
- Adding Meaning to your steps轨迹导论
- How to add custom fields to Customer Registration and Account page in magento
- Using a batch file to automatically build a Windows CE 5.0 and 6.0 project
- add backtrace to your program
- How to write your own custom Form
- note : create zip and add file to it
- Automatically roll back your ling to sql integration tests
- magento add custom column to order grid
- Add/Remove ISAPI Filter to/from IIS Automatically Using Delphi
- linux下手动创建Oracle数据库
- dos系统的一些事(来自网络,自己整理编辑)
- 论文摘要 (转)
- 无法将类型“string”隐式转换为 “System.Windows.Forms.DataGridViewTextBoxColumn 解决
- Linux下启动Oracle服务和监听程序
- Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
- Eclipse插件开发:关于警告“Discouraged access: *****”
- YUV420转YUV444 , YUV420转RGB
- VC++中全局变量、全局常量、全局结构体的定义
- Cassandra与RDBMS的设计差别
- 跨浏览器解析XML文件(二)
- iframe 调用 父页面 javascript方法
- 几个国外破解网址
- Welcome & Welcome Back !