Configure Vim to automatically add author information in source code

Source: Internet
Author: User

Author: Yang Shuo,Hua Qing vision embedded college lecturer.

Every programmer has his/her own favorite code editing and viewing tools. I believe many programmers in Linux like vim, although Vim does not look as powerful as sourceinsight's desktop-based editor, however, Vim's efficiency, conciseness, and powerful configurability all attract countless loyal fans.

Many editors support the ability to automatically add author information in the source code. sourceinsight has this function. Vim does not have this function by default, but we can implement this function as long as we make some configuration.

In Linux, we need to modify the vim configuration file vimrc, which is located:
/Etc/Vim/vimrc

In the window, the vim configuration file is located in _ vimrc under the installation directory.

Then add the following information to the configuration file to automatically add the author information to the source code:
"Map the F4 function key on the keyboard to the shortcut key for adding the author information.
Map <F4> MS: Call titledet () <CR>'s
Function addtitle ()
Call append (0, "/************************************** **************************************** *")
"Call append (1 ,"#")
Call append (1, "* Author: Yangshuo ")
"Call append (3 ,"#")
Call append (2, "* Email: ys@farsight.com.cn ")
"Call append (5 ,"#")
Call append (3, "* Last modified:". strftime ("% Y-% m-% d % H: % m "))
"Call append (7 ,"#")
Call append (4, "* filename:". Expand ("%: t "))
"Call append (9 ,"#")
Call append (5, "* Description :")
"Call append (11 ,"#")
Call append (6, "*************************************** ***************************************/ ")
Echohl warningmsg | echo "successful in adding the copyright." | echohl none
ENDF

"Update the latest modification time and file name
Normal m'
Execute '/# * Last modified:/s @:. * $ @/= strftime (":/T % Y-% m-% d % H: % m ")@'
Normal"
Normal mk
Execute '/# * filename:/s @:. * $ @/= ":/T". Expand ("%: t ")@'
Execute "noh"
Normal 'K
Echohl warningmsg | echo "successful in updating the copy right." | echohl none
Endfunction

"Determine whether the last modified word exists in the first 10 lines of code,
"If not, it indicates that the author information has not been added and needs to be added;
"If yes, you only need to update it.
Function titledet ()
Let n = 1
While n <10
Let line = Getline (N)
If line = ~ '^/#/S * Last/smodified:/S *. * $'
Call updatetitle ()
Return
Endif
Let n = n + 1
Endwhile
Call addtitle ()
Endfunction

This configuration can run normally in Linux and Windows. In this way, in the source code of C/C ++/Java/C #, you only need to click F4 to automatically add the author information. You only need to modify the above Code for configuring the author information to add your own information.

This is the result of running on my computer. check whether it is handsome:

Hurry up and add this configuration to your vimrc, so that you can easily add your relevant information in the source file to make our code more standard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.