Vim Tips Wiki
(Breakdown explanation of the regex used because otherwise it is practically indecipherable. Used tt and nowiki tags rather than pre to avoid a box around the command segments.)
(adding ability to match copyright notice in html format)
Line 5: Line 5:
 
autocmd BufWritePre *
 
autocmd BufWritePre *
 
\ if &modified |
 
\ if &modified |
\ exe "normal :g;\\c^V169 \\?Copyright;s;\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-[0-9]\\{4\\}\\)\\?;\\2-".strftime("%Y")."^M" |
+
\ exe "normal :g#\\(\\c^V169\\|©\\) \\?Copyright#s#\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-[0-9]\\{4\\}\\)\\?#\\2-".strftime("%Y")."^M" |
 
\ endif
 
\ endif
 
</pre>
 
</pre>
   
This replaces yyyy or yyyy-yyyy with yyyy-<current year> on any line in the file containing "&copy; copyright" with or without the space in a case-insensitive manner. It only does this if the file has been modified.
+
This replaces yyyy or yyyy-yyyy with yyyy-<current year> on any line in the file containing "&copy; copyright" or "&amp;copy; copyright" with or without the space in a case-insensitive manner. It only does this if the file has been modified.
   
 
An explaination of the command string follows:
 
An explaination of the command string follows:
*<tt><nowiki>g;\\c^V169 \\?Copyright;s;</nowiki></tt> - search and replace only on a line matching a case-insensitive string. I use ';' rather than the customary '/' because it doesn't require a shift key, my finger is already there after ':', and I don't need to escape '/'. The ^V169 is to insert a &copy; symbol. The command didn't work if I just placed it in there.
+
*<tt><nowiki>g#\\(\\c^V169\\|&copy;\\) \\?Copyright#s#</nowiki></tt> - search and replace only on a line matching case-insensitive copyright notice patten. I use '#' rather than the customary '/' to avoid confusion with all the '\' characters. The ^V169 is to insert a &copy; symbol. The command didn't work if I just placed it in there.
 
*<tt><nowiki>\\(".strftime("%Y")."\\)\\@!</nowiki></tt> - only match when the entire match does not consist of the current year (e.g. if the year is 2007, don't update a notice reading &copy; 2007).
 
*<tt><nowiki>\\(".strftime("%Y")."\\)\\@!</nowiki></tt> - only match when the entire match does not consist of the current year (e.g. if the year is 2007, don't update a notice reading &copy; 2007).
 
*<tt><nowiki>\\([0-9]\\{4\\}\\)</nowiki></tt> - matches a 4-digit year and allows a backreference to it for use in the replace text.
 
*<tt><nowiki>\\([0-9]\\{4\\}\\)</nowiki></tt> - matches a 4-digit year and allows a backreference to it for use in the replace text.
 
*<tt><nowiki>\\(-[0-9]\\{4\\}\\)\\?</nowiki></tt> - <em>optionally</em> match an ending year, e.g. the "-2006" in "&copy; 2000-2006".
 
*<tt><nowiki>\\(-[0-9]\\{4\\}\\)\\?</nowiki></tt> - <em>optionally</em> match an ending year, e.g. the "-2006" in "&copy; 2000-2006".
*<tt><nowiki>;\\2-".strftime("%Y")</nowiki></tt> - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.
+
*<tt><nowiki>\\2-".strftime("%Y")</nowiki></tt> - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.
   
 
[[Category:Automated Text Insertion]]
 
[[Category:Automated Text Insertion]]

Revision as of 19:14, 10 October 2007

Especially when editing source code, there is often a copyright notice embedded in the file. Insert the following in a vimrc file to automatically update this copyright notice in ALL FILES when writing them (note that ^V must be replaced by an actual CTRL-V character, or CTRL-Q for the default Windows mapping, and ^M must be replaced by an actual CTRL-M character):

" Automatically update copyright notice with current year
autocmd BufWritePre * 
      \ if &modified |
      \   exe "normal :g#\\(\\c^V169\\|©\\) \\?Copyright#s#\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-[0-9]\\{4\\}\\)\\?#\\2-".strftime("%Y")."^M" |
      \ endif

This replaces yyyy or yyyy-yyyy with yyyy-<current year> on any line in the file containing "© copyright" or "&copy; copyright" with or without the space in a case-insensitive manner. It only does this if the file has been modified.

An explaination of the command string follows:

  • g#\\(\\c^V169\\|©\\) \\?Copyright#s# - search and replace only on a line matching case-insensitive copyright notice patten. I use '#' rather than the customary '/' to avoid confusion with all the '\' characters. The ^V169 is to insert a © symbol. The command didn't work if I just placed it in there.
  • \\(".strftime("%Y")."\\)\\@! - only match when the entire match does not consist of the current year (e.g. if the year is 2007, don't update a notice reading © 2007).
  • \\([0-9]\\{4\\}\\) - matches a 4-digit year and allows a backreference to it for use in the replace text.
  • \\(-[0-9]\\{4\\}\\)\\? - optionally match an ending year, e.g. the "-2006" in "© 2000-2006".
  • \\2-".strftime("%Y") - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.